Compare commits
4 Commits
176a95f026
...
d4df7d45c3
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | d4df7d45c3 | |
Derrick Hammer | 875ba862f0 | |
Derrick Hammer | fe04cbe10d | |
Derrick Hammer | 2bcd37e271 |
|
@ -2,6 +2,7 @@
|
|||
import { Buffer } from "buffer";
|
||||
import { Client } from "@lumeweb/libkernel-universal";
|
||||
import { ErrTuple } from "@siaweb/libweb";
|
||||
import type { EventEmitter } from "eventemitter3";
|
||||
export declare class SwarmClient extends Client {
|
||||
private useDefaultSwarm;
|
||||
private id;
|
||||
|
@ -9,7 +10,7 @@ export declare class SwarmClient extends Client {
|
|||
constructor(useDefaultDht?: boolean);
|
||||
connect(pubkey: string | Uint8Array): Promise<Socket>;
|
||||
init(): Promise<ErrTuple>;
|
||||
ready(): Promise<ErrTuple>;
|
||||
ready(): Promise<void>;
|
||||
addRelay(pubkey: string): Promise<void>;
|
||||
removeRelay(pubkey: string): Promise<void>;
|
||||
clearRelays(): Promise<void>;
|
||||
|
@ -20,8 +21,8 @@ export declare class Socket extends Client {
|
|||
private id;
|
||||
private eventUpdates;
|
||||
constructor(id: number);
|
||||
on(eventName: string, listener: (...args: any[]) => void): this;
|
||||
off(type: string, listener: any): this;
|
||||
on<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn: EventEmitter.EventListener<string | symbol, T>, context?: any): this;
|
||||
off<T extends EventEmitter.EventNames<string | symbol>>(event: T, fn?: EventEmitter.EventListener<string | symbol, T>, context?: any, once?: boolean): this;
|
||||
write(message: string | Buffer): void;
|
||||
end(): void;
|
||||
private ensureEvent;
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,MAAM,EAAW,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAoB,QAAQ,EAAE,MAAM,gBAAgB,CA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AAK,EAAE,CAAC,EACR,EAAE,CAAC,EAAE,YAAY,CAAC,aAAa,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,EACnD,OAAO,CAAC,EAAE,GAAG,EACb,IAAI,CAAC,EAAE,OAAO,GACb,IAAI;IASP,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAUX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB;AAID,eAAO,MAAM,YAAY,+BAA4C,CAAC"}
|
|
@ -25,7 +25,10 @@ export class SwarmClient extends Client {
|
|||
return this.callModuleReturn("init", { swarm: this.swarm });
|
||||
}
|
||||
async ready() {
|
||||
return this.callModuleReturn("ready", { swarm: this.swarm });
|
||||
await this.callModuleReturn("ready", { swarm: this.swarm });
|
||||
this.connectModule("listenConnections", { swarm: this.swarm }, (socketId) => {
|
||||
this.emit("connection", createSocket(socketId));
|
||||
});
|
||||
}
|
||||
async addRelay(pubkey) {
|
||||
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
|
||||
|
@ -50,23 +53,23 @@ export class Socket extends Client {
|
|||
super();
|
||||
this.id = id;
|
||||
}
|
||||
on(eventName, listener) {
|
||||
const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: eventName }, (data) => {
|
||||
this.emit(eventName, data);
|
||||
on(event, fn, context) {
|
||||
const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: event }, (data) => {
|
||||
this.emit(event, data);
|
||||
});
|
||||
this.trackEvent(eventName, update);
|
||||
this.trackEvent(event, update);
|
||||
promise.then(() => {
|
||||
this.off(eventName, listener);
|
||||
this.off(event, fn);
|
||||
});
|
||||
return super.on(eventName, listener);
|
||||
return super.on(event, fn, context);
|
||||
}
|
||||
off(type, listener) {
|
||||
const updates = [...this.eventUpdates[type]];
|
||||
this.eventUpdates[type] = [];
|
||||
off(event, fn, context, once) {
|
||||
const updates = [...this.eventUpdates[event]];
|
||||
this.eventUpdates[event] = [];
|
||||
for (const func of updates) {
|
||||
func({ action: "off" });
|
||||
func();
|
||||
}
|
||||
return super.off(type, listener);
|
||||
return super.off(event, fn, context, once);
|
||||
}
|
||||
write(message) {
|
||||
this.callModule("write", { id: this.id, message });
|
||||
|
|
|
@ -5,10 +5,11 @@
|
|||
"main": "dist/index.js",
|
||||
"dependencies": {
|
||||
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
||||
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git"
|
||||
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
|
||||
"eventemitter3": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.11.18",
|
||||
"@types/node": "^18.11.19",
|
||||
"prettier": "^2.8.3",
|
||||
"pretty": "^2.0.0",
|
||||
"typescript": "^4.9.5"
|
||||
|
|
46
src/index.ts
46
src/index.ts
|
@ -2,6 +2,8 @@ import { Buffer } from "buffer";
|
|||
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
|
||||
|
||||
import type { EventEmitter } from "eventemitter3";
|
||||
|
||||
export class SwarmClient extends Client {
|
||||
private useDefaultSwarm: boolean;
|
||||
private id: number = 0;
|
||||
|
@ -31,8 +33,16 @@ export class SwarmClient extends Client {
|
|||
async init(): Promise<ErrTuple> {
|
||||
return this.callModuleReturn("init", { swarm: this.swarm });
|
||||
}
|
||||
async ready(): Promise<ErrTuple> {
|
||||
return this.callModuleReturn("ready", { swarm: this.swarm });
|
||||
async ready(): Promise<void> {
|
||||
await this.callModuleReturn("ready", { swarm: this.swarm });
|
||||
|
||||
this.connectModule(
|
||||
"listenConnections",
|
||||
{ swarm: this.swarm },
|
||||
(socketId: any) => {
|
||||
this.emit("connection", createSocket(socketId));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public async addRelay(pubkey: string): Promise<void> {
|
||||
|
@ -63,31 +73,39 @@ export class Socket extends Client {
|
|||
super();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
on(eventName: string, listener: (...args: any[]) => void): this {
|
||||
on<T extends EventEmitter.EventNames<string | symbol>>(
|
||||
event: T,
|
||||
fn: EventEmitter.EventListener<string | symbol, T>,
|
||||
context?: any
|
||||
): this {
|
||||
const [update, promise] = this.connectModule(
|
||||
"listenSocketEvent",
|
||||
{ id: this.id, event: eventName },
|
||||
{ id: this.id, event: event },
|
||||
(data: any) => {
|
||||
this.emit(eventName, data);
|
||||
this.emit(event, data);
|
||||
}
|
||||
);
|
||||
this.trackEvent(eventName, update);
|
||||
this.trackEvent(event as string, update);
|
||||
|
||||
promise.then(() => {
|
||||
this.off(eventName, listener);
|
||||
this.off(event as string, fn);
|
||||
});
|
||||
|
||||
return super.on(eventName, listener) as this;
|
||||
return super.on(event, fn, context) as this;
|
||||
}
|
||||
|
||||
off(type: string, listener: any): this {
|
||||
const updates = [...this.eventUpdates[type]];
|
||||
this.eventUpdates[type] = [];
|
||||
off<T extends EventEmitter.EventNames<string | symbol>>(
|
||||
event: T,
|
||||
fn?: EventEmitter.EventListener<string | symbol, T>,
|
||||
context?: any,
|
||||
once?: boolean
|
||||
): this {
|
||||
const updates = [...this.eventUpdates[event as string]];
|
||||
this.eventUpdates[event as string] = [];
|
||||
for (const func of updates) {
|
||||
func({ action: "off" });
|
||||
func();
|
||||
}
|
||||
return super.off(type, listener);
|
||||
return super.off(event, fn, context, once);
|
||||
}
|
||||
|
||||
write(message: string | Buffer): void {
|
||||
|
|
Loading…
Reference in New Issue