*Add addRelay interface method

This commit is contained in:
Derrick Hammer 2022-07-19 22:58:40 -04:00
parent 9b4ac8ff93
commit afc332b75e
1 changed files with 70 additions and 63 deletions

View File

@ -1,10 +1,10 @@
import { import {
callModule as callModuleKernel, callModule as callModuleKernel,
connectModule as connectModuleKernel, connectModule as connectModuleKernel,
} from "libkernel"; } from "libkernel";
import { import {
callModule as callModuleModule, callModule as callModuleModule,
connectModule as connectModuleModule, connectModule as connectModuleModule,
} from "libkmodule"; } from "libkmodule";
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { DataFn, ErrTuple } from "libskynet"; import { DataFn, ErrTuple } from "libskynet";
@ -13,82 +13,89 @@ import { Buffer } from "buffer";
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
let callModule: typeof callModuleModule, let callModule: typeof callModuleModule,
connectModule: typeof connectModuleModule; connectModule: typeof connectModuleModule;
if (window.document) { if (window.document) {
callModule = callModuleKernel; callModule = callModuleKernel;
connectModule = connectModuleKernel; connectModule = connectModuleKernel;
} else { } else {
callModule = callModuleModule; callModule = callModuleModule;
connectModule = connectModuleModule; connectModule = connectModuleModule;
} }
export class DHT { export class DHT {
public async connect(pubkey: string): Promise<Socket> { public async connect(pubkey: string): Promise<Socket> {
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey }); const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
if (err) { if (err) {
throw err; throw err;
}
return new Socket(resp.id);
} }
return new Socket(resp.id);
}
async ready(): Promise<ErrTuple> { async ready(): Promise<ErrTuple> {
return callModule(DHT_MODULE, "ready"); return callModule(DHT_MODULE, "ready");
}
public async addRelay(pubkey: string): Promise<void> {
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey });
if (err) {
throw err;
} }
}
} }
export class Socket extends EventEmitter { export class Socket extends EventEmitter {
private id: number; private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {}; private eventUpdates: { [event: string]: DataFn[] } = {};
constructor(id: number) { constructor(id: number) {
super(); super();
this.id = id; this.id = id;
}
on(eventName: string, listener: (...args: any[]) => void): this {
const [update, promise] = connectModule(
DHT_MODULE,
"listenSocketEvent",
{ id: this.id, event: eventName },
(data: any) => {
this.emit(eventName, data);
}
);
this.trackEvent(eventName, update);
promise.then(() => {
this.off(eventName, listener);
});
return super.on(eventName, listener);
}
off(type: string, listener: any): this {
const updates = [...this.eventUpdates[type]];
this.eventUpdates[type] = [];
for (const func of updates) {
func({ action: "off" });
} }
return super.off(type, listener);
}
on(eventName: string, listener: (...args: any[]) => void): this { write(message: string | Buffer): void {
const [update, promise] = connectModule( callModule(DHT_MODULE, "write", { id: this.id, message });
DHT_MODULE, }
"listenSocketEvent",
{ id: this.id, event: eventName },
(data: any) => {
this.emit(eventName, data);
}
);
this.trackEvent(eventName, update);
promise.then(() => { end(): void {
this.off(eventName, listener); callModule(DHT_MODULE, "close", { id: this.id });
}); }
return super.on(eventName, listener); private ensureEvent(event: string): void {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
} }
}
off(type: string, listener: any): this { private trackEvent(event: string, update: DataFn): void {
const updates = [...this.eventUpdates[type]]; this.ensureEvent(event as string);
this.eventUpdates[type] = []; this.eventUpdates[event].push(update);
for (const func of updates) { }
func({ action: "off" });
}
return super.off(type, listener);
}
write(message: string | Buffer): void {
callModule(DHT_MODULE, "write", { id: this.id, message });
}
end(): void {
callModule(DHT_MODULE, "close", { id: this.id });
}
private ensureEvent(event: string): void {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
}
}
private trackEvent(event: string, update: DataFn): void {
this.ensureEvent(event as string);
this.eventUpdates[event].push(update);
}
} }