*Use callbacks to ensure promises process serially

This commit is contained in:
Derrick Hammer 2022-07-20 18:25:27 -04:00
parent 07f90441d3
commit 833bb751bc
1 changed files with 12 additions and 6 deletions

View File

@ -22,23 +22,29 @@ if (typeof window !== "undefined" && window?.document) {
connectModule = connectModuleModule; connectModule = connectModuleModule;
} }
type PromiseCB = () => Promise<ErrTuple>;
export class RpcNetwork { export class RpcNetwork {
private _actionQueue: Promise<ErrTuple>[] = []; private _actionQueue: PromiseCB[] = [];
get ready(): Promise<ErrTuple> { get ready(): Promise<ErrTuple> {
return callModule(RPC_MODULE, "ready"); return callModule(RPC_MODULE, "ready");
} }
public addRelay(pubkey: string): void { 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 { public removeRelay(pubkey: string): void {
this._actionQueue.push(callModule(RPC_MODULE, "removeRelay", { pubkey })); this._actionQueue.push(() =>
callModule(RPC_MODULE, "removeRelay", { pubkey })
);
} }
public clearRelays(): void { public clearRelays(): void {
this._actionQueue.push(callModule(RPC_MODULE, "clearRelays")); this._actionQueue.push(() => callModule(RPC_MODULE, "clearRelays"));
} }
public query( public query(
@ -56,9 +62,9 @@ export class RpcNetwork {
} }
public async processQueue(): Promise<void> { public async processQueue(): Promise<void> {
for (const promise in this._actionQueue) { for (const promise of this._actionQueue) {
try { try {
await promise; await promise();
} catch (e: any) {} } catch (e: any) {}
} }