refactor: rewrite listen

This commit is contained in:
Derrick Hammer 2023-09-01 08:14:59 -04:00
parent 04c388248c
commit 8b1325f631
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 13 additions and 2 deletions

View File

@ -209,13 +209,24 @@ export class RegistryService {
return null; return null;
} }
public listen(pk: Uint8Array): EventEmitter { public listen(
pk: Uint8Array,
cb: (sre: SignedRegistryEntry) => void,
): ((() => void) | EventEmitter)[] {
const key = new Multihash(pk).toString(); const key = new Multihash(pk).toString();
if (!this.streams[key]) { if (!this.streams[key]) {
this.streams[key] = new EventEmitter(); this.streams[key] = new EventEmitter();
this.sendRegistryRequest(pk); 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 { public deserializeRegistryEntry(event: Uint8Array): SignedRegistryEntry {