Skip to content

Commit

Permalink
Merge pull request #185 from getAlby/fix/on-connected-callback
Browse files Browse the repository at this point in the history
fix: trigger onConnected callback on subscribe if already connected
  • Loading branch information
rolznz authored Jan 9, 2024
2 parents 4b50240 + 3aa4a63 commit 838a194
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,6 @@ import {disconnect} from '@getalby/bitcoin-connect';
disconnect();
```

#### Check connection status

```ts
import {isConnected} from '@getalby/bitcoin-connect';

const isConnected = isConnected();
```

#### Events

##### onConnected
Expand Down
10 changes: 9 additions & 1 deletion dev/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,12 @@ <h3>Pay an invoice</h3>
<button id="pay-invoice">Pay 1 sat to Alby team</button>
<h3>Make an invoice</h3>
<button id="make-invoice">Make a 1 sat invoice</button>
<h3>Try it yourself</h3>
<p>
Open devtools and use <b>window.bitcoinConnectSandbox</b> to access the
Bitcoin Connect API
</p>
</div>
<script></script>
<script type="module">
import {LightningAddress} from 'https://cdn.skypack.dev/@getalby/lightning-tools';
import {
Expand Down Expand Up @@ -384,5 +388,9 @@ <h3>Make an invoice</h3>

document.addEventListener('bc:onpaid', (e) => console.log('PAID!', e));
</script>
<script type="module">
import * as bitcoinConnectSandbox from '../../src/index.ts';
window.bitcoinConnectSandbox = bitcoinConnectSandbox;
</script>
</body>
</html>
23 changes: 20 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ type LaunchPaymentModalArgs = {
};

/**
* Listen to onConnected events which will fire when a user connects to a wallet
* @param callback includes the webln provider that was connected
* 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 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
*/
export function onConnected(callback: (provider: WebLNProvider) => void) {
if (store.getState().connected) {
callback(store.getState().provider!);
}

const zustandUnsubscribe = store.subscribe(async (state, prevState) => {
if (state.connected && !prevState.connected) {
if (!state.provider) {
Expand All @@ -60,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();
Expand Down Expand Up @@ -160,8 +173,12 @@ export async function requestProvider(): Promise<WebLNProvider> {

/**
* @returns true if user is connected to a wallet and WebLN is enabled
* @deprecated will be removed in v4.
*/
export function isConnected() {
console.warn(
'Bitcoin Connect: isConnected is deprecated and will be removed in the next major version'
);
return store.getState().connected;
}

Expand Down

0 comments on commit 838a194

Please sign in to comment.