Skip to content

Commit

Permalink
Merge pull request #163 from secretkeylabs/edu-ENG-4748-event-listeners
Browse files Browse the repository at this point in the history
Implement addListener example
  • Loading branch information
m-aboelenein authored Aug 14, 2024
2 parents 2050a4a + feb2c3a commit 88107a5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
4 changes: 2 additions & 2 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SendStx,
} from './components';
import { useLocalStorage } from './hooks';
import { useCallback } from 'react';
import { useCallback, useEffect } from 'react';
import GetBtcBalance from './components/GetBtcBalance';
import GetRunesBalance from './components/GetRunesBalance';
import { Container, ConnectButtonsContainer, Header, Logo, Body, Button } from './App.styles';
Expand All @@ -31,6 +31,15 @@ function AppWithProviders() {

const isConnected = btcAddressInfo.length + stxAddressInfo.length > 0;

useEffect(() => {
if (btcAddressInfo.length < 1) return;

const removeListener = Wallet.addListener('accountChange', (ev) => {
console.log('The account has changed.', ev);
});

return removeListener;
});
const onConnectLegacy = useCallback(() => {
(async () => {
const response = await Wallet.request('getAccounts', {
Expand Down
50 changes: 36 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sats-connect",
"version": "2.6.0",
"version": "2.7.0",
"main": "dist/index.mjs",
"files": [
"dist"
Expand All @@ -24,7 +24,7 @@
]
},
"dependencies": {
"@sats-connect/core": "0.1.2",
"@sats-connect/core": "0.2.1",
"@sats-connect/make-default-provider-config": "0.0.5",
"@sats-connect/ui": "0.0.6"
},
Expand Down
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
removeDefaultProvider,
RpcErrorCode,
BaseAdapter,
type AddListener,
} from '@sats-connect/core';
import {
Config,
Expand Down Expand Up @@ -107,6 +108,34 @@ class Wallet {
}
return response;
}

public addListener: AddListener = (event, cb) => {
const defaultProvider = getDefaultProvider();
if (!this.isProviderSet() && defaultProvider) {
this.providerId = defaultProvider;
}

if (!this.isProviderSet()) {
throw new Error(
'No wallet provider selected. The user must first select a wallet before adding listeners to wallet events.'
);
}

const adapter = this.defaultAdapters[this.providerId as string];

// Clients may have be using the latest version of sats-connect without
// their wallets having been updated. Until we have API versioning for the
// wallet, we can avoid having apps crash by checking whether the adapter
// actually supports `addListener`.
if (!new adapter().addListener) {
console.error(
`The wallet provider you are using does not support the addListener method. Please update your wallet provider.`
);
return () => {};
}

return new adapter().addListener(event, cb);
};
}

export * from '@sats-connect/core';
Expand Down

0 comments on commit 88107a5

Please sign in to comment.