*prettier
This commit is contained in:
parent
9071ed0592
commit
f76391d46e
36
src/index.ts
36
src/index.ts
|
@ -1,22 +1,21 @@
|
|||
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: any,
|
||||
connectModule: any;
|
||||
let callModule: any, connectModule: any;
|
||||
|
||||
async function loadLibs() {
|
||||
if (callModule && connectModule) {
|
||||
return;
|
||||
}
|
||||
if (typeof window !== "undefined" && window?.document) {
|
||||
const pkg = (await import("libkernel"));
|
||||
const pkg = await import("libkernel");
|
||||
callModule = pkg.callModule;
|
||||
connectModule = pkg.connectModule;
|
||||
} else {
|
||||
const pkg = (await import("libkmodule"));
|
||||
const pkg = await import("libkmodule");
|
||||
callModule = pkg.callModule;
|
||||
connectModule = pkg.connectModule;
|
||||
}
|
||||
|
@ -32,7 +31,7 @@ export class DHT {
|
|||
|
||||
public async connect(pubkey: string): Promise<Socket> {
|
||||
await loadLibs();
|
||||
const [resp, err] = await callModule(DHT_MODULE, "connect", {pubkey});
|
||||
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
@ -42,13 +41,13 @@ export class DHT {
|
|||
async ready(): Promise<ErrTuple> {
|
||||
await loadLibs();
|
||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||
return callModule(DHT_MODULE, "ready", {dht});
|
||||
return callModule(DHT_MODULE, "ready", { dht });
|
||||
}
|
||||
|
||||
public async addRelay(pubkey: string): Promise<void> {
|
||||
await loadLibs();
|
||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||
const [, err] = await callModule(DHT_MODULE, "addRelay", {pubkey, dht});
|
||||
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht });
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
@ -57,7 +56,10 @@ export class DHT {
|
|||
public async removeRelay(pubkey: string): Promise<void> {
|
||||
await loadLibs();
|
||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||
const [, err] = await callModule(DHT_MODULE, "removeRelay", {pubkey, dht});
|
||||
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
|
||||
pubkey,
|
||||
dht,
|
||||
});
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
@ -66,7 +68,7 @@ export class DHT {
|
|||
public async clearRelays(): Promise<void> {
|
||||
await loadLibs();
|
||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||
await callModule(DHT_MODULE, "clearRelays", {dht});
|
||||
await callModule(DHT_MODULE, "clearRelays", { dht });
|
||||
}
|
||||
|
||||
private async create() {
|
||||
|
@ -88,7 +90,7 @@ export class DHT {
|
|||
if (this.useDefaultDht) {
|
||||
return false;
|
||||
}
|
||||
const [, err] = await callModule(DHT_MODULE, "closeDht", {dht: this.id});
|
||||
const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id });
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
@ -110,7 +112,7 @@ export class Socket extends EventEmitter {
|
|||
const [update, promise] = connectModule(
|
||||
DHT_MODULE,
|
||||
"listenSocketEvent",
|
||||
{id: this.id, event: eventName},
|
||||
{ id: this.id, event: eventName },
|
||||
(data: any) => {
|
||||
this.emit(eventName, data);
|
||||
}
|
||||
|
@ -128,17 +130,17 @@ export class Socket extends EventEmitter {
|
|||
const updates = [...this.eventUpdates[type]];
|
||||
this.eventUpdates[type] = [];
|
||||
for (const func of updates) {
|
||||
func({action: "off"});
|
||||
func({ action: "off" });
|
||||
}
|
||||
return super.off(type, listener);
|
||||
}
|
||||
|
||||
write(message: string | Buffer): void {
|
||||
callModule(DHT_MODULE, "write", {id: this.id, message});
|
||||
callModule(DHT_MODULE, "write", { id: this.id, message });
|
||||
}
|
||||
|
||||
end(): void {
|
||||
callModule(DHT_MODULE, "close", {id: this.id});
|
||||
callModule(DHT_MODULE, "close", { id: this.id });
|
||||
}
|
||||
|
||||
private ensureEvent(event: string): void {
|
||||
|
|
Loading…
Reference in New Issue