Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement addListener example #163

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading