refactor: switch to emittery

This commit is contained in:
Derrick Hammer 2023-07-03 01:22:27 -04:00
parent b2a741ab2a
commit bae26a3e01
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 43 additions and 26 deletions

45
npm-shrinkwrap.json generated
View File

@ -12,6 +12,7 @@
"@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.5",
"@lumeweb/libkernel": "0.1.0-develop.7",
"async-mutex": "^0.4.0",
"emittery": "^1.0.1",
"eventemitter2": "^6.4.9",
"hyperswarm": "^4.5.1",
"random-number-csprng": "^1.0.2"
@ -1849,17 +1850,6 @@
"@noble/hashes": "^1.3.1"
}
},
"node_modules/@lumeweb/kernel-peer-discovery-client/node_modules/emittery": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/emittery/-/emittery-1.0.1.tgz",
"integrity": "sha512-2ID6FdrMD9KDLldGesP6317G78K7km/kMcwItRtVFva7I/cSEOIaLpewaUb+YLXVwdAp3Ctfxh/V5zIl1sj7dQ==",
"engines": {
"node": ">=14.16"
},
"funding": {
"url": "https://github.com/sindresorhus/emittery?sponsor=1"
}
},
"node_modules/@lumeweb/kernel-peer-discovery-client/node_modules/sodium-native": {
"version": "3.4.1",
"resolved": "https://registry.npmjs.org/sodium-native/-/sodium-native-3.4.1.tgz",
@ -5933,12 +5923,11 @@
"peer": true
},
"node_modules/emittery": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/emittery/-/emittery-0.13.1.tgz",
"integrity": "sha512-DeWwawk6r5yR9jFgnDKYt4sLS0LmHJJi3ZOnb5/JdbYwj3nW+FxQnHIjhBKz8YLC7oRNPVM9NQ47I3CVx34eqQ==",
"peer": true,
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/emittery/-/emittery-1.0.1.tgz",
"integrity": "sha512-2ID6FdrMD9KDLldGesP6317G78K7km/kMcwItRtVFva7I/cSEOIaLpewaUb+YLXVwdAp3Ctfxh/V5zIl1sj7dQ==",
"engines": {
"node": ">=12"
"node": ">=14.16"
},
"funding": {
"url": "https://github.com/sindresorhus/emittery?sponsor=1"
@ -9335,6 +9324,18 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-runner/node_modules/emittery": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/emittery/-/emittery-0.13.1.tgz",
"integrity": "sha512-DeWwawk6r5yR9jFgnDKYt4sLS0LmHJJi3ZOnb5/JdbYwj3nW+FxQnHIjhBKz8YLC7oRNPVM9NQ47I3CVx34eqQ==",
"peer": true,
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sindresorhus/emittery?sponsor=1"
}
},
"node_modules/jest-runtime": {
"version": "29.5.0",
"resolved": "https://registry.npmjs.org/jest-runtime/-/jest-runtime-29.5.0.tgz",
@ -9467,6 +9468,18 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-watcher/node_modules/emittery": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/emittery/-/emittery-0.13.1.tgz",
"integrity": "sha512-DeWwawk6r5yR9jFgnDKYt4sLS0LmHJJi3ZOnb5/JdbYwj3nW+FxQnHIjhBKz8YLC7oRNPVM9NQ47I3CVx34eqQ==",
"peer": true,
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sindresorhus/emittery?sponsor=1"
}
},
"node_modules/jest-worker": {
"version": "29.5.0",
"resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-29.5.0.tgz",

View File

@ -30,6 +30,7 @@
"@lumeweb/kernel-peer-discovery-client": "^0.0.2-develop.5",
"@lumeweb/libkernel": "0.1.0-develop.7",
"async-mutex": "^0.4.0",
"emittery": "^1.0.1",
"eventemitter2": "^6.4.9",
"hyperswarm": "^4.5.1",
"random-number-csprng": "^1.0.2"

View File

@ -10,11 +10,11 @@ import type {
// @ts-ignore
import Hyperswarm from "hyperswarm";
import randomNumber from "random-number-csprng";
import EventEmitter, { OnOptions } from "eventemitter2";
import { Mutex } from "async-mutex";
import { logErr } from "@lumeweb/libkernel";
import Emittery from "emittery";
export default class HyperswarmWeb extends EventEmitter.default {
export default class HyperswarmWeb extends Emittery {
private _options: any;
private _discovery: PeerDiscoveryClient;
private _queuedEmActions: [string, any][] = [];
@ -75,6 +75,7 @@ export default class HyperswarmWeb extends EventEmitter.default {
this._relays.clear();
}
// @ts-ignore
on(
eventName: string | symbol,
listener: (...args: any[]) => void,
@ -85,9 +86,8 @@ export default class HyperswarmWeb extends EventEmitter.default {
onSelf(
eventName: string | symbol,
listener: (...args: any[]) => void,
options?: boolean | OnOptions,
): Hyperswarm {
return super.on(eventName, listener, options);
return super.on(eventName, listener);
}
addListener(
@ -97,6 +97,7 @@ export default class HyperswarmWeb extends EventEmitter.default {
return this.on(eventName, listener);
}
// @ts-ignore
off(
eventName: string | symbol,
listener: (...args: any[]) => void,
@ -118,14 +119,16 @@ export default class HyperswarmWeb extends EventEmitter.default {
return this.off(eventName, listener);
}
// @ts-ignore
emit(eventName: string | symbol, ...args: any[]): boolean {
return this._processOrQueueAction("emit", ...arguments);
}
emitSelf(eventName: string | symbol, ...args: any[]): boolean {
return super.emit(eventName, ...args);
async emitSelf(eventName: string | symbol, ...args: any): Promise<void> {
return super.emit(eventName, { ...args });
}
// @ts-ignore
once(eventName: string | symbol, listener: (...args: any[]) => void): this {
return this._processOrQueueAction("once", ...arguments);
}
@ -216,10 +219,10 @@ export default class HyperswarmWeb extends EventEmitter.default {
keyPair: this._options.keyPair,
});
this._activeRelay.dht._protocol._stream.once("close", () => {
this._activeRelay.dht._protocol._stream.once("close", async () => {
this._activeRelay = undefined;
this._ready = false;
this.emitSelf("close");
await this.emitSelf("close");
});
} while (relays.length > 0 && !this._activeRelay);
}
@ -229,14 +232,14 @@ export default class HyperswarmWeb extends EventEmitter.default {
throw new Error("Failed to find an available relay");
}
this.emitSelf("init");
await this.emitSelf("init");
this._processQueuedActions();
await this._activeRelay.dht.ready();
this._connectionMutex.release();
this._ready = true;
this.emit("ready");
await this.emit("ready");
}
private async isServerAvailable(connection: string): Promise<boolean> {