*Add onSelf and emitSelf methods to use local event emitter

*Emit local init before processing queued actions
This commit is contained in:
Derrick Hammer 2023-02-17 22:22:46 -05:00
parent 8427cbdff6
commit 42f0c1b60d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 15 additions and 1 deletions

View File

@ -12,7 +12,7 @@ import { load } from "@lumeweb/libkernel-universal";
// @ts-ignore
import Hyperswarm from "hyperswarm";
import randomNumber from "random-number-csprng";
import EventEmitter from "eventemitter2";
import EventEmitter, { OnOptions } from "eventemitter2";
import { Mutex } from "async-mutex";
export default class HyperswarmWeb extends EventEmitter {
@ -83,6 +83,14 @@ export default class HyperswarmWeb extends EventEmitter {
return this._processOrQueueAction("on", ...arguments);
}
onSelf(
eventName: string | symbol,
listener: (...args: any[]) => void,
options?: boolean | OnOptions
): Hyperswarm {
return super.on(eventName, listener, options);
}
addListener(
eventName: string | symbol,
listener: (...args: any[]) => void
@ -108,6 +116,10 @@ export default class HyperswarmWeb extends EventEmitter {
return this._processOrQueueAction("emit", ...arguments);
}
emitSelf(eventName: string | symbol, ...args: any[]): boolean {
return super.emit(eventName, ...args);
}
once(eventName: string | symbol, listener: (...args: any[]) => void): this {
return this._processOrQueueAction("once", ...arguments);
}
@ -205,6 +217,8 @@ export default class HyperswarmWeb extends EventEmitter {
throw new Error("Failed to find an available relay");
}
this.emitSelf("init");
this._processQueuedActions();
await this._activeRelay.dht.ready();
this._connectionMutex.release();