*Properly overload eventemitter3 methods
This commit is contained in:
parent
176a95f026
commit
2bcd37e271
|
@ -5,10 +5,11 @@
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
||||||
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git"
|
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
|
||||||
|
"eventemitter3": "^5.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.19",
|
||||||
"prettier": "^2.8.3",
|
"prettier": "^2.8.3",
|
||||||
"pretty": "^2.0.0",
|
"pretty": "^2.0.0",
|
||||||
"typescript": "^4.9.5"
|
"typescript": "^4.9.5"
|
||||||
|
|
34
src/index.ts
34
src/index.ts
|
@ -2,6 +2,8 @@ import { Buffer } from "buffer";
|
||||||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||||
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
|
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
|
||||||
|
|
||||||
|
import type { EventEmitter } from "eventemitter3";
|
||||||
|
|
||||||
export class SwarmClient extends Client {
|
export class SwarmClient extends Client {
|
||||||
private useDefaultSwarm: boolean;
|
private useDefaultSwarm: boolean;
|
||||||
private id: number = 0;
|
private id: number = 0;
|
||||||
|
@ -63,31 +65,39 @@ export class Socket extends Client {
|
||||||
super();
|
super();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
on<T extends EventEmitter.EventNames<string | symbol>>(
|
||||||
on(eventName: string, listener: (...args: any[]) => void): this {
|
event: T,
|
||||||
|
fn: EventEmitter.EventListener<string | symbol, T>,
|
||||||
|
context?: any
|
||||||
|
): this {
|
||||||
const [update, promise] = this.connectModule(
|
const [update, promise] = this.connectModule(
|
||||||
"listenSocketEvent",
|
"listenSocketEvent",
|
||||||
{ id: this.id, event: eventName },
|
{ id: this.id, event: event },
|
||||||
(data: any) => {
|
(data: any) => {
|
||||||
this.emit(eventName, data);
|
this.emit(event, data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.trackEvent(eventName, update);
|
this.trackEvent(event as string, update);
|
||||||
|
|
||||||
promise.then(() => {
|
promise.then(() => {
|
||||||
this.off(eventName, listener);
|
this.off(event as string, fn);
|
||||||
});
|
});
|
||||||
|
|
||||||
return super.on(eventName, listener) as this;
|
return super.on(event, fn, context) as this;
|
||||||
}
|
}
|
||||||
|
|
||||||
off(type: string, listener: any): this {
|
off<T extends EventEmitter.EventNames<string | symbol>>(
|
||||||
const updates = [...this.eventUpdates[type]];
|
event: T,
|
||||||
this.eventUpdates[type] = [];
|
fn?: EventEmitter.EventListener<string | symbol, T>,
|
||||||
|
context?: any,
|
||||||
|
once?: boolean
|
||||||
|
): this {
|
||||||
|
const updates = [...this.eventUpdates[event as string]];
|
||||||
|
this.eventUpdates[event as string] = [];
|
||||||
for (const func of updates) {
|
for (const func of updates) {
|
||||||
func({ action: "off" });
|
func();
|
||||||
}
|
}
|
||||||
return super.off(type, listener);
|
return super.off(event, fn, context, once);
|
||||||
}
|
}
|
||||||
|
|
||||||
write(message: string | Buffer): void {
|
write(message: string | Buffer): void {
|
||||||
|
|
Loading…
Reference in New Issue