Compare commits

..

No commits in common. "e61bd06454394251e2f8a133ed7a27331acb4bb4" and "bd82046a94f898c81e21b9489a7dbc30153afa8d" have entirely different histories.

4 changed files with 11 additions and 48 deletions

3
dist/index.d.ts vendored
View File

@ -6,7 +6,6 @@ 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;
@ -22,7 +21,5 @@ 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;IACxC,OAAO,CAAC,gBAAgB,CAAuB;gBACnC,IAAI,GAAE,GAAQ;IAO1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAIR,gBAAgB;YAiDhB,iBAAiB;IAYzB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OA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AI;CAG3E"}

25
dist/index.js vendored
View File

@ -11,7 +11,6 @@ export default class HyperswarmWeb extends EventEmitter {
_relays = new Set(); _relays = new Set();
_activeRelay; _activeRelay;
_discovery; _discovery;
_queuedEmActions = [];
constructor(opts = {}) { constructor(opts = {}) {
super(); super();
opts.custodial = false; opts.custodial = false;
@ -53,7 +52,6 @@ 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) {
@ -91,34 +89,21 @@ export default class HyperswarmWeb extends EventEmitter {
this._relays.clear(); this._relays.clear();
} }
on(eventName, listener) { on(eventName, listener) {
return this._processOrQueueAction("on", arguments); return this._activeRelay?.on(eventName, listener);
} }
addListener(eventName, listener) { addListener(eventName, listener) {
return this.on(eventName, listener); return this.on(eventName, listener);
} }
off(eventName, listener) { off(eventName, listener) {
return this._processOrQueueAction("off", arguments); return this._activeRelay?.off(eventName, listener);
} }
removeListener(eventName, listener) { removeListener(eventName, listener) {
return this.off(eventName, listener); return this.on(eventName, listener);
} }
emit(eventName, ...args) { emit(eventName, ...args) {
return this._processOrQueueAction("emit", arguments); return this._activeRelay?.emit(eventName, ...args);
} }
once(eventName, listener) { once(eventName, listener) {
return this._processOrQueueAction("once", arguments); return this._activeRelay?.once(eventName, listener);
}
_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,7 +19,6 @@ 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;
@ -76,7 +75,6 @@ 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();
} }
@ -126,7 +124,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._processOrQueueAction("on", arguments); return this._activeRelay?.on(eventName, listener);
} }
addListener( addListener(
eventName: string | symbol, eventName: string | symbol,
@ -139,37 +137,20 @@ export default class HyperswarmWeb extends EventEmitter {
eventName: string | symbol, eventName: string | symbol,
listener: (...args: any[]) => void listener: (...args: any[]) => void
): Hyperswarm { ): Hyperswarm {
return this._processOrQueueAction("off", arguments); return this._activeRelay?.off(eventName, listener);
} }
removeListener( removeListener(
eventName: string | symbol, eventName: string | symbol,
listener: (...args: any[]) => void listener: (...args: any[]) => void
): this { ): this {
return this.off(eventName, listener); return this.on(eventName, listener);
} }
emit(eventName: string | symbol, ...args: any[]): boolean { emit(eventName: string | symbol, ...args: any[]): boolean {
return this._processOrQueueAction("emit", arguments); return this._activeRelay?.emit(eventName, ...args);
} }
once(eventName: string | symbol, listener: (...args: any[]) => void): this { once(eventName: string | symbol, listener: (...args: any[]) => void): this {
return this._processOrQueueAction("once", arguments); return this._activeRelay?.once(eventName, listener);
}
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 = [];
} }
} }