*Add persistent tracking of topics

This commit is contained in:
Derrick Hammer 2023-02-17 19:35:56 -05:00
parent ae487a27e5
commit df63f8be30
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 1 deletions

View File

@ -15,6 +15,8 @@ export class SwarmClient extends Client {
private _ready?: Promise<void>; private _ready?: Promise<void>;
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
constructor(useDefaultDht = true, autoReconnect = false) { constructor(useDefaultDht = true, autoReconnect = false) {
super(); super();
this.useDefaultSwarm = useDefaultDht; this.useDefaultSwarm = useDefaultDht;
@ -57,6 +59,10 @@ export class SwarmClient extends Client {
await this._ready; await this._ready;
this._ready = undefined; this._ready = undefined;
for (const topic of this._topics) {
await this.join(topic);
}
} }
async start(): Promise<void> { async start(): Promise<void> {
@ -97,7 +103,8 @@ export class SwarmClient extends Client {
return this.callModuleReturn("getRelays", { swarm: this.swarm }); return this.callModuleReturn("getRelays", { swarm: this.swarm });
} }
public async join(topic: Buffer): Promise<void> { public async join(topic: Buffer | Uint8Array): Promise<void> {
this._topics.add(topic);
this.callModule("join", { id: this.id, 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; return super.on(event, fn, context) as this;
} }
onSelf<T extends EventEmitter.EventNames<string | symbol>>(
event: T,
fn: EventEmitter.EventListener<string | symbol, T>,
context?: any
): this {
return super.on(event, fn, context) as this;
}
off<T extends EventEmitter.EventNames<string | symbol>>( off<T extends EventEmitter.EventNames<string | symbol>>(
event: T, event: T,
fn?: EventEmitter.EventListener<string | symbol, T>, fn?: EventEmitter.EventListener<string | symbol, T>,
@ -171,6 +186,15 @@ export class Socket extends Client {
return super.off(event, fn, context, once); return super.off(event, fn, context, once);
} }
offSelf<T extends EventEmitter.EventNames<string | symbol>>(
event: T,
fn?: EventEmitter.EventListener<string | symbol, T>,
context?: any,
once?: boolean
): this {
return super.off(event, fn, context, once);
}
write(message: string | Buffer): void { write(message: string | Buffer): void {
this.callModule("socketWrite", { id: this.id, message }); this.callModule("socketWrite", { id: this.id, message });
} }