Compare commits
No commits in common. "v0.0.2-develop.10" and "v0.0.2-develop.9" have entirely different histories.
v0.0.2-dev
...
v0.0.2-dev
|
@ -1,5 +1,3 @@
|
||||||
## [0.0.2-develop.10](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.9...v0.0.2-develop.10) (2023-07-22)
|
|
||||||
|
|
||||||
## [0.0.2-develop.9](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.8...v0.0.2-develop.9) (2023-07-12)
|
## [0.0.2-develop.9](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.8...v0.0.2-develop.9) (2023-07-12)
|
||||||
|
|
||||||
## [0.0.2-develop.8](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.7...v0.0.2-develop.8) (2023-07-08)
|
## [0.0.2-develop.8](https://git.lumeweb.com/LumeWeb/kernel-swarm-client/compare/v0.0.2-develop.7...v0.0.2-develop.8) (2023-07-08)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/kernel-swarm-client",
|
"name": "@lumeweb/kernel-swarm-client",
|
||||||
"version": "0.0.2-develop.10",
|
"version": "0.0.2-develop.9",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@lumeweb/kernel-swarm-client",
|
"name": "@lumeweb/kernel-swarm-client",
|
||||||
"version": "0.0.2-develop.10",
|
"version": "0.0.2-develop.9",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/libkernel": "^0.1.0-develop.9",
|
"@lumeweb/libkernel": "^0.1.0-develop.9",
|
||||||
"async-mutex": "^0.4.0",
|
"async-mutex": "^0.4.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/kernel-swarm-client",
|
"name": "@lumeweb/kernel-swarm-client",
|
||||||
"version": "0.0.2-develop.10",
|
"version": "0.0.2-develop.9",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
40
src/index.ts
40
src/index.ts
|
@ -6,8 +6,10 @@ import b4a from "b4a";
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Backoff from "backoff.js";
|
import Backoff from "backoff.js";
|
||||||
|
import { Mutex } from "async-mutex";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Protomux from "protomux";
|
import Protomux from "protomux";
|
||||||
|
import type { UnsubscribeFn } from "emittery";
|
||||||
|
|
||||||
export class SwarmClient extends Client {
|
export class SwarmClient extends Client {
|
||||||
private useDefaultSwarm: boolean;
|
private useDefaultSwarm: boolean;
|
||||||
|
@ -157,7 +159,9 @@ interface SocketRawStream {
|
||||||
|
|
||||||
export class Socket extends Client {
|
export class Socket extends Client {
|
||||||
private id: number;
|
private id: number;
|
||||||
private eventUpdates: { [event: string]: Map<Function, DataFn> } = {};
|
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||||
|
|
||||||
|
private syncMutex = new Mutex();
|
||||||
|
|
||||||
private swarm: SwarmClient;
|
private swarm: SwarmClient;
|
||||||
private userData?: any = null;
|
private userData?: any = null;
|
||||||
|
@ -188,12 +192,7 @@ export class Socket extends Client {
|
||||||
await this.swarm.emit("setup", this);
|
await this.swarm.emit("setup", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
on(event: any, listener: any): UnsubscribeFn {
|
||||||
on(event: any, listener: any): this {
|
|
||||||
if (this.eventUpdates[event]?.has(listener)) {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [update, promise] = this.connectModule(
|
const [update, promise] = this.connectModule(
|
||||||
"socketListenEvent",
|
"socketListenEvent",
|
||||||
{ id: this.id, event: event },
|
{ id: this.id, event: event },
|
||||||
|
@ -201,30 +200,22 @@ export class Socket extends Client {
|
||||||
this.emit(event, data);
|
this.emit(event, data);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
this.trackEvent(event as string, listener, update);
|
this.trackEvent(event as string, update);
|
||||||
|
|
||||||
promise.then(() => {
|
promise.then(() => {
|
||||||
this.off(event as string, listener);
|
this.off(event as string, listener);
|
||||||
});
|
});
|
||||||
|
|
||||||
super.on(event, listener);
|
return super.on(event, listener);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
off(event: any, listener: any): this {
|
off(event: any, listener: any): this {
|
||||||
if (listener) {
|
const updates = [...this.eventUpdates[event as string]];
|
||||||
const updates = this.eventUpdates[event];
|
this.eventUpdates[event as string] = [];
|
||||||
updates?.get(listener)?.();
|
for (const func of updates) {
|
||||||
updates?.delete(listener);
|
func();
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const updates = [...this.eventUpdates[event as string].values()];
|
|
||||||
this.eventUpdates[event as string] = new Map<Function, DataFn>();
|
|
||||||
updates.forEach((update) => update());
|
|
||||||
super.off(event, listener);
|
super.off(event, listener);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,13 +235,13 @@ export class Socket extends Client {
|
||||||
|
|
||||||
private ensureEvent(event: string): void {
|
private ensureEvent(event: string): void {
|
||||||
if (!(event in this.eventUpdates)) {
|
if (!(event in this.eventUpdates)) {
|
||||||
this.eventUpdates[event] = new Map<Function, DataFn>();
|
this.eventUpdates[event] = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private trackEvent(event: string, listener: Function, update: DataFn): void {
|
private trackEvent(event: string, update: DataFn): void {
|
||||||
this.ensureEvent(event as string);
|
this.ensureEvent(event as string);
|
||||||
this.eventUpdates[event].set(listener, update);
|
this.eventUpdates[event].push(update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,7 +250,6 @@ export const MODULE =
|
||||||
|
|
||||||
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const socketFactory = factory<Socket>(Socket, MODULE);
|
const socketFactory = factory<Socket>(Socket, MODULE);
|
||||||
const createSocket = async (...args: any): Promise<Socket> => {
|
const createSocket = async (...args: any): Promise<Socket> => {
|
||||||
const socket = socketFactory(...args);
|
const socket = socketFactory(...args);
|
||||||
|
|
Loading…
Reference in New Issue