*Add removeRelay and clearRelays API methods
This commit is contained in:
parent
2c41826632
commit
179bef1f9f
149
src/index.ts
149
src/index.ts
|
@ -1,19 +1,19 @@
|
|||
import {
|
||||
callModule as callModuleKernel,
|
||||
connectModule as connectModuleKernel,
|
||||
callModule as callModuleKernel,
|
||||
connectModule as connectModuleKernel,
|
||||
} from "libkernel";
|
||||
import {
|
||||
callModule as callModuleModule,
|
||||
connectModule as connectModuleModule,
|
||||
callModule as callModuleModule,
|
||||
connectModule as connectModuleModule,
|
||||
} from "libkmodule";
|
||||
import { EventEmitter } from "events";
|
||||
import { DataFn, ErrTuple } from "libskynet";
|
||||
import { Buffer } from "buffer";
|
||||
import {EventEmitter} from "events";
|
||||
import {DataFn, ErrTuple} from "libskynet";
|
||||
import {Buffer} from "buffer";
|
||||
|
||||
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||
|
||||
let callModule: typeof callModuleModule,
|
||||
connectModule: typeof connectModuleModule;
|
||||
connectModule: typeof connectModuleModule;
|
||||
|
||||
if (typeof window !== "undefined" && window?.document) {
|
||||
callModule = callModuleKernel;
|
||||
|
@ -24,78 +24,89 @@ if (typeof window !== "undefined" && window?.document) {
|
|||
}
|
||||
|
||||
export class DHT {
|
||||
public async connect(pubkey: string): Promise<Socket> {
|
||||
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
public async connect(pubkey: string): Promise<Socket> {
|
||||
const [resp, err] = await callModule(DHT_MODULE, "connect", {pubkey});
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
return new Socket(resp.id);
|
||||
}
|
||||
return new Socket(resp.id);
|
||||
}
|
||||
|
||||
async ready(): Promise<ErrTuple> {
|
||||
return callModule(DHT_MODULE, "ready");
|
||||
}
|
||||
|
||||
public async addRelay(pubkey: string): Promise<void> {
|
||||
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey });
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
async ready(): Promise<ErrTuple> {
|
||||
return callModule(DHT_MODULE, "ready");
|
||||
}
|
||||
|
||||
public async addRelay(pubkey: string): Promise<void> {
|
||||
const [, err] = await callModule(DHT_MODULE, "addRelay", {pubkey});
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async removeRelay(pubkey: string): Promise<void> {
|
||||
const [, err] = await callModule(DHT_MODULE, "removeRelay", {pubkey});
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async clearRelays(): Promise<void> {
|
||||
await callModule(DHT_MODULE, "clearRelays");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Socket extends EventEmitter {
|
||||
private id: number;
|
||||
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||
private id: number;
|
||||
private eventUpdates: { [event: string]: DataFn[] } = {};
|
||||
|
||||
constructor(id: number) {
|
||||
super();
|
||||
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" });
|
||||
constructor(id: number) {
|
||||
super();
|
||||
this.id = id;
|
||||
}
|
||||
return super.off(type, listener);
|
||||
}
|
||||
|
||||
write(message: string | Buffer): void {
|
||||
callModule(DHT_MODULE, "write", { id: this.id, message });
|
||||
}
|
||||
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);
|
||||
|
||||
end(): void {
|
||||
callModule(DHT_MODULE, "close", { id: this.id });
|
||||
}
|
||||
promise.then(() => {
|
||||
this.off(eventName, listener);
|
||||
});
|
||||
|
||||
private ensureEvent(event: string): void {
|
||||
if (!(event in this.eventUpdates)) {
|
||||
this.eventUpdates[event] = [];
|
||||
return super.on(eventName, listener);
|
||||
}
|
||||
}
|
||||
|
||||
private trackEvent(event: string, update: DataFn): void {
|
||||
this.ensureEvent(event as string);
|
||||
this.eventUpdates[event].push(update);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue