*Properly overload eventemitter3 methods

This commit is contained in:
Derrick Hammer 2023-02-06 03:11:42 -05:00
parent 176a95f026
commit 2bcd37e271
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 25 additions and 14 deletions

View File

@ -5,10 +5,11 @@
"main": "dist/index.js",
"dependencies": {
"@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": {
"@types/node": "^18.11.18",
"@types/node": "^18.11.19",
"prettier": "^2.8.3",
"pretty": "^2.0.0",
"typescript": "^4.9.5"

View File

@ -2,6 +2,8 @@ import { Buffer } from "buffer";
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
import type { EventEmitter } from "eventemitter3";
export class SwarmClient extends Client {
private useDefaultSwarm: boolean;
private id: number = 0;
@ -63,31 +65,39 @@ export class Socket extends Client {
super();
this.id = id;
}
on(eventName: string, listener: (...args: any[]) => void): this {
on<T extends EventEmitter.EventNames<string | symbol>>(
event: T,
fn: EventEmitter.EventListener<string | symbol, T>,
context?: any
): this {
const [update, promise] = this.connectModule(
"listenSocketEvent",
{ id: this.id, event: eventName },
{ id: this.id, event: event },
(data: any) => {
this.emit(eventName, data);
this.emit(event, data);
}
);
this.trackEvent(eventName, update);
this.trackEvent(event as string, update);
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 {
const updates = [...this.eventUpdates[type]];
this.eventUpdates[type] = [];
off<T extends EventEmitter.EventNames<string | symbol>>(
event: T,
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) {
func({ action: "off" });
func();
}
return super.off(type, listener);
return super.off(event, fn, context, once);
}
write(message: string | Buffer): void {