From a6e1ac0844a58621028a6643766866f747d07929 Mon Sep 17 00:00:00 2001 From: George Knee Date: Thu, 12 Oct 2023 13:32:44 +0100 Subject: [PATCH] attach listener first, then do a "static" check for condition --- packages/nitro-rpc-client/src/rpc-client.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/nitro-rpc-client/src/rpc-client.ts b/packages/nitro-rpc-client/src/rpc-client.ts index 4cc9d9abe..8d207a757 100644 --- a/packages/nitro-rpc-client/src/rpc-client.ts +++ b/packages/nitro-rpc-client/src/rpc-client.ts @@ -63,9 +63,7 @@ export class NitroRpcClient implements RpcClientApi { channelId: string, status: ChannelStatus ): Promise { - const ledger = await this.GetLedgerChannel(channelId); - return new Promise((resolve) => { - if (ledger.Status == status) resolve(); + const promise = new Promise((resolve) => { this.transport.Notifications.on( "ledger_channel_updated", (payload: LedgerChannelUpdatedNotification["params"]["payload"]) => { @@ -77,14 +75,16 @@ export class NitroRpcClient implements RpcClientApi { } ); }); + const ledger = await this.GetLedgerChannel(channelId); + if (ledger.Status == status) return; + return promise; } public async WaitForPaymentChannelToHaveStatus( channelId: string, status: ChannelStatus ): Promise { - const channel = await this.GetPaymentChannel(channelId); - return new Promise((resolve) => { + const promise = new Promise((resolve) => { if (channel.Status == status) resolve(); this.transport.Notifications.on( "payment_channel_updated", @@ -97,6 +97,10 @@ export class NitroRpcClient implements RpcClientApi { } ); }); + + const channel = await this.GetPaymentChannel(channelId); + if (channel.Status == status) return; + return promise; } public onPaymentChannelUpdated(