From 833bb751bcb40397101cc29d1d79d68fe7532c74 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 20 Jul 2022 18:25:27 -0400 Subject: [PATCH] *Use callbacks to ensure promises process serially --- src/index.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index e619064..991fb19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,23 +22,29 @@ if (typeof window !== "undefined" && window?.document) { connectModule = connectModuleModule; } +type PromiseCB = () => Promise; + export class RpcNetwork { - private _actionQueue: Promise[] = []; + private _actionQueue: PromiseCB[] = []; get ready(): Promise { return callModule(RPC_MODULE, "ready"); } public addRelay(pubkey: string): void { - this._actionQueue.push(callModule(RPC_MODULE, "addRelay", { pubkey })); + this._actionQueue.push(() => + callModule(RPC_MODULE, "addRelay", { pubkey }) + ); } public removeRelay(pubkey: string): void { - this._actionQueue.push(callModule(RPC_MODULE, "removeRelay", { pubkey })); + this._actionQueue.push(() => + callModule(RPC_MODULE, "removeRelay", { pubkey }) + ); } public clearRelays(): void { - this._actionQueue.push(callModule(RPC_MODULE, "clearRelays")); + this._actionQueue.push(() => callModule(RPC_MODULE, "clearRelays")); } public query( @@ -56,9 +62,9 @@ export class RpcNetwork { } public async processQueue(): Promise { - for (const promise in this._actionQueue) { + for (const promise of this._actionQueue) { try { - await promise; + await promise(); } catch (e: any) {} }