Compare commits

...

2 Commits

4 changed files with 48 additions and 11 deletions

3
dist/index.d.ts vendored
View File

@ -6,6 +6,7 @@ export default class HyperswarmWeb extends EventEmitter {
private _relays; private _relays;
private _activeRelay; private _activeRelay;
private _discovery; private _discovery;
private _queuedEmActions;
constructor(opts?: any); constructor(opts?: any);
ready(): Promise<void>; ready(): Promise<void>;
private ensureConnection; private ensureConnection;
@ -21,5 +22,7 @@ export default class HyperswarmWeb extends EventEmitter {
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this; removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
emit(eventName: string | symbol, ...args: any[]): boolean; emit(eventName: string | symbol, ...args: any[]): boolean;
once(eventName: string | symbol, listener: (...args: any[]) => void): this; once(eventName: string | symbol, listener: (...args: any[]) => void): this;
private _processOrQueueAction;
private _processQueuedActions;
} }
//# sourceMappingURL=index.d.ts.map //# sourceMappingURL=index.d.ts.map

2
dist/index.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAW5C,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,OAAO,YAAY,MAAM,eAAe,CAAC;AAEzC,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,YAAY;IACrD,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,YAAY,CAAa;IACjC,OAAO,CAAC,UAAU,CAAsB;gBAC5B,IAAI,GAAE,GAAQ;IAO1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAIR,gBAAgB;YAgDhB,iBAAiB;IAYzB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ7D,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OA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= new Set(); _relays = new Set();
_activeRelay; _activeRelay;
_discovery; _discovery;
_queuedEmActions = [];
constructor(opts = {}) { constructor(opts = {}) {
super(); super();
opts.custodial = false; opts.custodial = false;
@ -52,6 +53,7 @@ export default class HyperswarmWeb extends EventEmitter {
if (!this._activeRelay) { if (!this._activeRelay) {
throw new Error("Failed to find an available relay"); throw new Error("Failed to find an available relay");
} }
this._processQueuedActions();
await this._activeRelay.dht.ready(); await this._activeRelay.dht.ready();
} }
async isServerAvailable(connection) { async isServerAvailable(connection) {
@ -89,21 +91,34 @@ export default class HyperswarmWeb extends EventEmitter {
this._relays.clear(); this._relays.clear();
} }
on(eventName, listener) { on(eventName, listener) {
return this._activeRelay?.on(eventName, listener); return this._processOrQueueAction("on", arguments);
} }
addListener(eventName, listener) { addListener(eventName, listener) {
return this.on(eventName, listener); return this.on(eventName, listener);
} }
off(eventName, listener) { off(eventName, listener) {
return this._activeRelay?.off(eventName, listener); return this._processOrQueueAction("off", arguments);
} }
removeListener(eventName, listener) { removeListener(eventName, listener) {
return this.on(eventName, listener); return this.off(eventName, listener);
} }
emit(eventName, ...args) { emit(eventName, ...args) {
return this._activeRelay?.emit(eventName, ...args); return this._processOrQueueAction("emit", arguments);
} }
once(eventName, listener) { once(eventName, listener) {
return this._activeRelay?.once(eventName, listener); return this._processOrQueueAction("once", arguments);
}
_processOrQueueAction(method, ...args) {
if (this._activeRelay) {
return this._activeRelay[method](...args);
}
this._queuedEmActions.push([method, args]);
return this;
}
_processQueuedActions() {
for (const action of this._queuedEmActions) {
this._activeRelay[action[0]](...action[1]);
}
this._queuedEmActions = [];
} }
} }

View File

@ -19,6 +19,7 @@ export default class HyperswarmWeb extends EventEmitter {
private _relays: Set<string> = new Set(); private _relays: Set<string> = new Set();
private _activeRelay: Hyperswarm; private _activeRelay: Hyperswarm;
private _discovery: PeerDiscoveryClient; private _discovery: PeerDiscoveryClient;
private _queuedEmActions: [string, any][] = [];
constructor(opts: any = {}) { constructor(opts: any = {}) {
super(); super();
opts.custodial = false; opts.custodial = false;
@ -75,6 +76,7 @@ export default class HyperswarmWeb extends EventEmitter {
throw new Error("Failed to find an available relay"); throw new Error("Failed to find an available relay");
} }
this._processQueuedActions();
await this._activeRelay.dht.ready(); await this._activeRelay.dht.ready();
} }
@ -124,7 +126,7 @@ export default class HyperswarmWeb extends EventEmitter {
eventName: string | symbol, eventName: string | symbol,
listener: (...args: any[]) => void listener: (...args: any[]) => void
): Hyperswarm { ): Hyperswarm {
return this._activeRelay?.on(eventName, listener); return this._processOrQueueAction("on", arguments);
} }
addListener( addListener(
eventName: string | symbol, eventName: string | symbol,
@ -137,20 +139,37 @@ export default class HyperswarmWeb extends EventEmitter {
eventName: string | symbol, eventName: string | symbol,
listener: (...args: any[]) => void listener: (...args: any[]) => void
): Hyperswarm { ): Hyperswarm {
return this._activeRelay?.off(eventName, listener); return this._processOrQueueAction("off", arguments);
} }
removeListener( removeListener(
eventName: string | symbol, eventName: string | symbol,
listener: (...args: any[]) => void listener: (...args: any[]) => void
): this { ): this {
return this.on(eventName, listener); return this.off(eventName, listener);
} }
emit(eventName: string | symbol, ...args: any[]): boolean { emit(eventName: string | symbol, ...args: any[]): boolean {
return this._activeRelay?.emit(eventName, ...args); return this._processOrQueueAction("emit", arguments);
} }
once(eventName: string | symbol, listener: (...args: any[]) => void): this { once(eventName: string | symbol, listener: (...args: any[]) => void): this {
return this._activeRelay?.once(eventName, listener); return this._processOrQueueAction("once", arguments);
}
private _processOrQueueAction(method: string, ...args: any[]) {
if (this._activeRelay) {
return this._activeRelay[method](...args);
}
this._queuedEmActions.push([method, args]);
return this;
}
private _processQueuedActions(): void {
for (const action of this._queuedEmActions) {
this._activeRelay[action[0]](...action[1]);
}
this._queuedEmActions = [];
} }
} }