*Refactor action queue

*Dynamically load callModule and connectModule
This commit is contained in:
Derrick Hammer 2022-07-21 00:58:24 -04:00
parent f38c6f53cf
commit 663f565c8d
1 changed files with 50 additions and 28 deletions

View File

@ -1,50 +1,59 @@
import {
callModule as callModuleKernel,
connectModule as connectModuleKernel,
} from "libkernel";
import {
callModule as callModuleModule,
connectModule as connectModuleModule,
} from "libkmodule";
import { ErrTuple } from "libskynet"; import { ErrTuple } from "libskynet";
import type { RPCRequest } from "@lumeweb/dht-rpc-client"; import type { RPCRequest } from "@lumeweb/dht-rpc-client";
const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg"; const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg";
let callModule: typeof callModuleModule, let callModule: any, connectModule: any;
connectModule: typeof connectModuleModule;
if (typeof window !== "undefined" && window?.document) { async function loadLibs() {
callModule = callModuleKernel; if (callModule && connectModule) {
connectModule = connectModuleKernel; return;
} else { }
callModule = callModuleModule;
connectModule = connectModuleModule; 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;
}
} }
type PromiseCB = () => Promise<ErrTuple>; type PromiseCB = () => Promise<ErrTuple>;
export class RpcNetwork { export class RpcNetwork {
private _actionQueue: PromiseCB[] = []; private _actionQueue: [string, any][] = [];
private _addQueue: string[] = [];
private _removeQueue: string[] = [];
get ready(): Promise<ErrTuple> { get ready(): Promise<ErrTuple> {
return callModule(RPC_MODULE, "ready"); return loadLibs().then(() => callModule(RPC_MODULE, "ready"));
} }
public addRelay(pubkey: string): void { public addRelay(pubkey: string): void {
this._actionQueue.push(() => this._addQueue.push(pubkey);
callModule(RPC_MODULE, "addRelay", { pubkey }) this._addQueue = [...new Set(this._addQueue)];
); RpcNetwork.deleteItem(this._removeQueue, pubkey);
} }
public removeRelay(pubkey: string): void { public removeRelay(pubkey: string): void {
this._actionQueue.push(() => this._removeQueue.push(pubkey);
callModule(RPC_MODULE, "removeRelay", { pubkey }) this._removeQueue = [...new Set(this._removeQueue)];
); RpcNetwork.deleteItem(this._addQueue, pubkey);
} }
public clearRelays(): void { public clearRelays(): void {
this._actionQueue.push(() => callModule(RPC_MODULE, "clearRelays")); this._actionQueue.push(["clearRelays", {}]);
}
private static deleteItem(array: Array<any>, item: string): void {
if (array.includes(item)) {
let queue = new Set(array);
queue.delete(item);
array = [...queue];
}
} }
public query( public query(
@ -62,14 +71,27 @@ export class RpcNetwork {
} }
public async processQueue(): Promise<void> { public async processQueue(): Promise<void> {
for (const promise of this._actionQueue) { await loadLibs();
for (const action of this._actionQueue) {
try { try {
const p = promise(); await callModule(RPC_MODULE, action[0], action[1]);
await p;
} catch (e: any) {} } catch (e: any) {}
} }
await Promise.allSettled(
this._removeQueue.map((item: string) =>
callModule(RPC_MODULE, "removeRelay", { pubkey: item })
)
);
await Promise.allSettled(
this._addQueue.map((item: string) =>
callModule(RPC_MODULE, "addRelay", { pubkey: item })
)
);
this._actionQueue = []; this._actionQueue = [];
this._removeQueue = [];
this._addQueue = [];
} }
} }