From 8b1325f631c5b9f9858aac318d6a7f92a1940ff5 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Sep 2023 08:14:59 -0400 Subject: [PATCH] refactor: rewrite listen --- src/service/registry.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/service/registry.ts b/src/service/registry.ts index b5c9257..2459ba5 100644 --- a/src/service/registry.ts +++ b/src/service/registry.ts @@ -209,13 +209,24 @@ export class RegistryService { return null; } - public listen(pk: Uint8Array): EventEmitter { + public listen( + pk: Uint8Array, + cb: (sre: SignedRegistryEntry) => void, + ): ((() => void) | EventEmitter)[] { const key = new Multihash(pk).toString(); if (!this.streams[key]) { this.streams[key] = new EventEmitter(); this.sendRegistryRequest(pk); } - return this.streams[key]; + const stream = this.streams[key] as EventEmitter; + + const done = () => { + stream.off("event", cb); + }; + + stream.on("event", cb); + + return [done, stream]; } public deserializeRegistryEntry(event: Uint8Array): SignedRegistryEntry {