refactor: switch to emittery

This commit is contained in:
Derrick Hammer 2023-07-01 16:06:03 -04:00
parent d22b636889
commit 009dc4e003
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 24 additions and 28 deletions

26
npm-shrinkwrap.json generated
View File

@ -8,7 +8,7 @@
"name": "@lumeweb/kernel-swarm-client", "name": "@lumeweb/kernel-swarm-client",
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@lumeweb/libkernel": "^0.1.0-develop.8", "@lumeweb/libkernel": "^0.1.0-develop.9",
"async-mutex": "^0.4.0", "async-mutex": "^0.4.0",
"b4a": "^1.6.4" "b4a": "^1.6.4"
}, },
@ -1681,12 +1681,23 @@
} }
}, },
"node_modules/@lumeweb/libkernel": { "node_modules/@lumeweb/libkernel": {
"version": "0.1.0-develop.8", "version": "0.1.0-develop.9",
"resolved": "https://registry.npmjs.org/@lumeweb/libkernel/-/libkernel-0.1.0-develop.8.tgz", "resolved": "https://registry.npmjs.org/@lumeweb/libkernel/-/libkernel-0.1.0-develop.9.tgz",
"integrity": "sha512-n7LM4+zCSLBDtVLmYi75rRHu+Fqd7G5AsOZUKCJwAoglnT6YiWR49FSNBRt3VmY2wpfdK7P/WwSknGQfAzc/uQ==", "integrity": "sha512-izusLjMFaMpsIT4PnnxRQgRM+bKiflaUIB37C82GvfDi7peMeuI2SIK7RW+LdcUOApQA5rXcr0DE6Cj0+FnQZQ==",
"dependencies": { "dependencies": {
"@lumeweb/libweb": "0.2.0-develop.20", "@lumeweb/libweb": "0.2.0-develop.20",
"eventemitter2": "^6.4.9" "emittery": "^1.0.1"
}
},
"node_modules/@lumeweb/libkernel/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/libportal": { "node_modules/@lumeweb/libportal": {
@ -6379,11 +6390,6 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/eventemitter2": {
"version": "6.4.9",
"resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-6.4.9.tgz",
"integrity": "sha512-JEPTiaOt9f04oa6NOkc4aH+nVp5I3wEjpHbIPqfgCdD5v5bUzy7xQqwcVO2aDQgOWhI28da57HksMrzK9HlRxg=="
},
"node_modules/eventemitter3": { "node_modules/eventemitter3": {
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz",

View File

@ -20,7 +20,7 @@
"semantic-release": "semantic-release" "semantic-release": "semantic-release"
}, },
"dependencies": { "dependencies": {
"@lumeweb/libkernel": "^0.1.0-develop.8", "@lumeweb/libkernel": "^0.1.0-develop.9",
"async-mutex": "^0.4.0", "async-mutex": "^0.4.0",
"b4a": "^1.6.4" "b4a": "^1.6.4"
}, },

View File

@ -9,14 +9,7 @@ import Backoff from "backoff.js";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
// @ts-ignore // @ts-ignore
import Protomux from "protomux"; import Protomux from "protomux";
import type { UnsubscribeFn } from "emittery";
import type {
eventNS,
event,
ListenerFn,
OnOptions,
Listener,
} from "eventemitter2";
export class SwarmClient extends Client { export class SwarmClient extends Client {
private useDefaultSwarm: boolean; private useDefaultSwarm: boolean;
@ -196,14 +189,10 @@ export class Socket extends Client {
this._remotePublicKey = info.remotePublicKey; this._remotePublicKey = info.remotePublicKey;
this._rawStream = info.rawStream; this._rawStream = info.rawStream;
await this.swarm.emitAsync("setup", this); await this.swarm.emit("setup", this);
} }
on( on(event: any, listener: any): UnsubscribeFn {
event: event | eventNS,
listener: ListenerFn,
options?: boolean | OnOptions,
): this | Listener {
const [update, promise] = this.connectModule( const [update, promise] = this.connectModule(
"socketListenEvent", "socketListenEvent",
{ id: this.id, event: event }, { id: this.id, event: event },
@ -217,16 +206,17 @@ export class Socket extends Client {
this.off(event as string, listener); this.off(event as string, listener);
}); });
return super.on(event, listener, options); return super.on(event, listener);
} }
off(event: event | eventNS, listener: ListenerFn): this { off(event: any, listener: any): this {
const updates = [...this.eventUpdates[event as string]]; const updates = [...this.eventUpdates[event as string]];
this.eventUpdates[event as string] = []; this.eventUpdates[event as string] = [];
for (const func of updates) { for (const func of updates) {
func(); func();
} }
return super.off(event, listener); super.off(event, listener);
return this;
} }
write(message: string | Buffer): void { write(message: string | Buffer): void {