*Load callModule and connectModule on the fly
This commit is contained in:
parent
a321cf0859
commit
018c9f0387
36
src/index.ts
36
src/index.ts
|
@ -1,30 +1,30 @@
|
||||||
import {
|
|
||||||
callModule as callModuleKernel,
|
|
||||||
connectModule as connectModuleKernel,
|
|
||||||
} from "libkernel";
|
|
||||||
import {
|
|
||||||
callModule as callModuleModule,
|
|
||||||
connectModule as connectModuleModule,
|
|
||||||
} from "libkmodule";
|
|
||||||
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";
|
||||||
|
|
||||||
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
||||||
|
|
||||||
let callModule: typeof callModuleModule,
|
let callModule: any,
|
||||||
connectModule: typeof connectModuleModule;
|
connectModule: any;
|
||||||
|
|
||||||
if (typeof window !== "undefined" && window?.document) {
|
async function loadLibs() {
|
||||||
callModule = callModuleKernel;
|
if (callModule && connectModule) {
|
||||||
connectModule = connectModuleKernel;
|
return;
|
||||||
} else {
|
}
|
||||||
callModule = callModuleModule;
|
if (typeof window !== "undefined" && window?.document) {
|
||||||
connectModule = connectModuleModule;
|
const pkg = (await import("libkernel"));
|
||||||
|
callModule = pkg.callModule;
|
||||||
|
connectModule = pkg.connectModule;
|
||||||
|
} else {
|
||||||
|
const pkg = (await import("libkmodule"));
|
||||||
|
callModule = pkg.callModule;
|
||||||
|
connectModule = pkg.connectModule;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DHT {
|
export class DHT {
|
||||||
public async connect(pubkey: string): Promise<Socket> {
|
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) {
|
if (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
|
@ -33,10 +33,12 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
async ready(): Promise<ErrTuple> {
|
async ready(): Promise<ErrTuple> {
|
||||||
|
await loadLibs();
|
||||||
return callModule(DHT_MODULE, "ready");
|
return callModule(DHT_MODULE, "ready");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async addRelay(pubkey: string): Promise<void> {
|
public async addRelay(pubkey: string): Promise<void> {
|
||||||
|
await loadLibs();
|
||||||
const [, err] = await callModule(DHT_MODULE, "addRelay", {pubkey});
|
const [, err] = await callModule(DHT_MODULE, "addRelay", {pubkey});
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
|
@ -44,6 +46,7 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async removeRelay(pubkey: string): Promise<void> {
|
public async removeRelay(pubkey: string): Promise<void> {
|
||||||
|
await loadLibs();
|
||||||
const [, err] = await callModule(DHT_MODULE, "removeRelay", {pubkey});
|
const [, err] = await callModule(DHT_MODULE, "removeRelay", {pubkey});
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
|
@ -51,6 +54,7 @@ export class DHT {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async clearRelays(): Promise<void> {
|
public async clearRelays(): Promise<void> {
|
||||||
|
await loadLibs();
|
||||||
await callModule(DHT_MODULE, "clearRelays");
|
await callModule(DHT_MODULE, "clearRelays");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue