From 14f3770bf7a654dea5282694f61146f6710a8667 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 21 Jul 2022 00:58:47 -0400 Subject: [PATCH] *Update dist --- dist/index.d.ts | 3 +++ dist/index.d.ts.map | 2 +- dist/index.js | 54 +++++++++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index e260b6b..b1892ac 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -2,10 +2,13 @@ import { ErrTuple } from "libskynet"; import type { RPCRequest } from "@lumeweb/dht-rpc-client"; export declare class RpcNetwork { private _actionQueue; + private _addQueue; + private _removeQueue; get ready(): Promise; addRelay(pubkey: string): void; removeRelay(pubkey: string): void; clearRelays(): void; + private static deleteItem; query(query: string, chain: string, data?: object | any[], force?: boolean): RpcQuery; processQueue(): Promise; } diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index 02069c4..add2c45 100644 --- a/dist/index.d.ts.map +++ b/dist/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAiB1D,qBAAa,UAAU;IACrB,OAAO,CAAC,YAAY,CAAmB;IAEvC,IAAI,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC,CAE7B;IAEM,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAM9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAMjC,WAAW,IAAI,IAAI;IAInB,KAAK,CACV,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,KAAK,GAAE,OAAe,GACrB,QAAQ;IASE,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAU3C;AAED,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAe;gBAEnB,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU;IAMlD,IAAI,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,CAOzB;CACF"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAwB1D,qBAAa,UAAU;IACrB,OAAO,CAAC,YAAY,CAAuB;IAC3C,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,YAAY,CAAgB;IAEpC,IAAI,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC,CAE7B;IAEM,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAM9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAMjC,WAAW,IAAI,IAAI;IAI1B,OAAO,CAAC,MAAM,CAAC,UAAU;IAQlB,KAAK,CACV,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,KAAK,GAAE,OAAe,GACrB,QAAQ;IASE,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAuB3C;AAED,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAe;gBAEnB,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU;IAMlD,IAAI,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,CAOzB;CACF"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 442c8b4..7bcc652 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,28 +1,46 @@ -import { callModule as callModuleKernel, connectModule as connectModuleKernel, } from "libkernel"; -import { callModule as callModuleModule, connectModule as connectModuleModule, } from "libkmodule"; const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg"; let callModule, connectModule; -if (typeof window !== "undefined" && window?.document) { - callModule = callModuleKernel; - connectModule = connectModuleKernel; -} -else { - callModule = callModuleModule; - connectModule = connectModuleModule; +async function loadLibs() { + if (callModule && connectModule) { + return; + } + if (typeof window !== "undefined" && window?.document) { + 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 RpcNetwork { _actionQueue = []; + _addQueue = []; + _removeQueue = []; get ready() { - return callModule(RPC_MODULE, "ready"); + return loadLibs().then(() => callModule(RPC_MODULE, "ready")); } addRelay(pubkey) { - this._actionQueue.push(() => callModule(RPC_MODULE, "addRelay", { pubkey })); + this._addQueue.push(pubkey); + this._addQueue = [...new Set(this._addQueue)]; + RpcNetwork.deleteItem(this._removeQueue, pubkey); } removeRelay(pubkey) { - this._actionQueue.push(() => callModule(RPC_MODULE, "removeRelay", { pubkey })); + this._removeQueue.push(pubkey); + this._removeQueue = [...new Set(this._removeQueue)]; + RpcNetwork.deleteItem(this._addQueue, pubkey); } clearRelays() { - this._actionQueue.push(() => callModule(RPC_MODULE, "clearRelays")); + this._actionQueue.push(["clearRelays", {}]); + } + static deleteItem(array, item) { + if (array.includes(item)) { + let queue = new Set(array); + queue.delete(item); + array = [...queue]; + } } query(query, chain, data = {}, force = false) { return new RpcQuery(this, { @@ -33,14 +51,18 @@ export class RpcNetwork { }); } async processQueue() { - for (const promise of this._actionQueue) { + await loadLibs(); + for (const action of this._actionQueue) { try { - const p = promise(); - await p; + await callModule(RPC_MODULE, action[0], action[1]); } catch (e) { } } + await Promise.allSettled(this._removeQueue.map((item) => callModule(RPC_MODULE, "removeRelay", { pubkey: item }))); + await Promise.allSettled(this._addQueue.map((item) => callModule(RPC_MODULE, "addRelay", { pubkey: item }))); this._actionQueue = []; + this._removeQueue = []; + this._addQueue = []; } } export class RpcQuery {