From df63f8be30220e28d17840bd472a2f9a091d7f3e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 17 Feb 2023 19:35:56 -0500 Subject: [PATCH] *Add persistent tracking of topics --- src/index.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 69c9d7e..55c98b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,8 @@ export class SwarmClient extends Client { private _ready?: Promise; + private _topics: Set = new Set(); + constructor(useDefaultDht = true, autoReconnect = false) { super(); this.useDefaultSwarm = useDefaultDht; @@ -57,6 +59,10 @@ export class SwarmClient extends Client { await this._ready; this._ready = undefined; + + for (const topic of this._topics) { + await this.join(topic); + } } async start(): Promise { @@ -97,7 +103,8 @@ export class SwarmClient extends Client { return this.callModuleReturn("getRelays", { swarm: this.swarm }); } - public async join(topic: Buffer): Promise { + public async join(topic: Buffer | Uint8Array): Promise { + this._topics.add(topic); this.callModule("join", { id: this.id, topic }); } } @@ -157,6 +164,14 @@ export class Socket extends Client { return super.on(event, fn, context) as this; } + onSelf>( + event: T, + fn: EventEmitter.EventListener, + context?: any + ): this { + return super.on(event, fn, context) as this; + } + off>( event: T, fn?: EventEmitter.EventListener, @@ -171,6 +186,15 @@ export class Socket extends Client { return super.off(event, fn, context, once); } + offSelf>( + event: T, + fn?: EventEmitter.EventListener, + context?: any, + once?: boolean + ): this { + return super.off(event, fn, context, once); + } + write(message: string | Buffer): void { this.callModule("socketWrite", { id: this.id, message }); }