*Add setup method and use it in all methods
This commit is contained in:
parent
cc3b188d5d
commit
85fb05b854
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAU,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAqBhC,qBAAa,GAAG;IACd,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,EAAE,CAAS;gBAEP,aAAa,UAAO;IAInB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAS/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAMnB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;YAM3B,MAAM;IAaP,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;CAavC;AAED,qBAAa,MAAO,SAAQ,YAAY;IACtC,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAqC;gBAE7C,EAAE,EAAE,MAAM;IAKtB,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAkB/D,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI;IAStC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAIX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB"}
|
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAU,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAsBhC,qBAAa,GAAG;IACd,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,EAAE,CAAa;gBAEX,aAAa,UAAO;IAKnB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAS/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAMnB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;YAM3B,MAAM;IAaP,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;CAavC;AAED,qBAAa,MAAO,SAAQ,YAAY;IACtC,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAqC;gBAE7C,EAAE,EAAE,MAAM;IAKtB,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAkB/D,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI;IAStC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAIX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB"}
|
|
@ -1,4 +1,5 @@
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from "events";
|
||||||
|
import { create } from "domain";
|
||||||
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||||
let callModule, connectModule;
|
let callModule, connectModule;
|
||||||
async function loadLibs() {
|
async function loadLibs() {
|
||||||
|
@ -18,9 +19,10 @@ async function loadLibs() {
|
||||||
}
|
}
|
||||||
export class DHT {
|
export class DHT {
|
||||||
useDefaultDht;
|
useDefaultDht;
|
||||||
id;
|
id = 0;
|
||||||
constructor(useDefaultDht = true) {
|
constructor(useDefaultDht = true) {
|
||||||
this.useDefaultDht = useDefaultDht;
|
this.useDefaultDht = useDefaultDht;
|
||||||
|
return create();
|
||||||
}
|
}
|
||||||
async connect(pubkey) {
|
async connect(pubkey) {
|
||||||
await loadLibs();
|
await loadLibs();
|
||||||
|
|
20
src/index.ts
20
src/index.ts
|
@ -1,6 +1,7 @@
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from "events";
|
||||||
import { DataFn, ErrTuple } from "libskynet";
|
import { DataFn, ErrTuple } from "libskynet";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
|
import {create} from "domain";
|
||||||
|
|
||||||
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connect(pubkey: string): Promise<Socket> {
|
public async connect(pubkey: string): Promise<Socket> {
|
||||||
await loadLibs();
|
await this.setup();
|
||||||
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
|
@ -39,13 +40,13 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
async ready(): Promise<ErrTuple> {
|
async ready(): Promise<ErrTuple> {
|
||||||
await loadLibs();
|
await this.setup();
|
||||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
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> {
|
public async addRelay(pubkey: string): Promise<void> {
|
||||||
await loadLibs();
|
await this.setup();
|
||||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
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) {
|
if (err) {
|
||||||
|
@ -54,7 +55,7 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async removeRelay(pubkey: string): Promise<void> {
|
public async removeRelay(pubkey: string): Promise<void> {
|
||||||
await loadLibs();
|
await this.setup();
|
||||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
|
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
|
||||||
pubkey,
|
pubkey,
|
||||||
|
@ -66,14 +67,14 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async clearRelays(): Promise<void> {
|
public async clearRelays(): Promise<void> {
|
||||||
await loadLibs();
|
await this.setup();
|
||||||
const dht = !this.useDefaultDht ? this.id : undefined;
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
||||||
await callModule(DHT_MODULE, "clearRelays", { dht });
|
await callModule(DHT_MODULE, "clearRelays", { dht });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async create() {
|
private async create() {
|
||||||
await loadLibs();
|
await loadLibs();
|
||||||
if (this.useDefaultDht) {
|
if (this.useDefaultDht || this.id > 0) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
const [dht, err] = await callModule(DHT_MODULE, "openDht");
|
const [dht, err] = await callModule(DHT_MODULE, "openDht");
|
||||||
|
@ -85,7 +86,7 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async close(): Promise<boolean> {
|
public async close(): Promise<boolean> {
|
||||||
await loadLibs();
|
await this.setup();
|
||||||
|
|
||||||
if (this.useDefaultDht) {
|
if (this.useDefaultDht) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -97,6 +98,11 @@ export class DHT {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async setup(){
|
||||||
|
await loadLibs();
|
||||||
|
await this.create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Socket extends EventEmitter {
|
export class Socket extends EventEmitter {
|
||||||
|
|
Loading…
Reference in New Issue