From 3aa4a63251fe0914a05cd8d7801d206feadf0468 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Tue, 9 Jan 2024 20:34:24 +0700 Subject: [PATCH] fix: immediately call onConnecting callback if provider is already connecting when subscription is created --- src/api.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/api.ts b/src/api.ts index 44c6b6b..58b8dc9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -41,10 +41,10 @@ type LaunchPaymentModalArgs = { }; /** - * Listen to onConnected events which will fire when a wallet is connected (either + * Subscribe to onConnected events which will fire when a wallet is connected (either * the user connects to a new wallet or when Bitcoin Connect boots and connects to a previously-connected wallet). * - * If a provider is already available, the callback will be immediately fired. + * If a provider is already available when the subscription is created, the callback will be immediately fired. * @param callback includes the webln provider that was (or is already) connected * @returns unsubscribe function */ @@ -67,11 +67,17 @@ export function onConnected(callback: (provider: WebLNProvider) => void) { } /** - * Listen to onConnecting events which will fire when a user is connecting to their wallet + * Subscribe to onConnecting events which will fire when a user is connecting to their wallet + * + * If a provider is already being connected to when the subscription is created, the callback will be immediately fired. * @param callback * @returns unsubscribe function */ export function onConnecting(callback: () => void) { + if (store.getState().connecting) { + callback(); + } + const zustandUnsubscribe = store.subscribe(async (state, prevState) => { if (state.connecting && !prevState.connecting) { callback();