From f39a93df118729330f3c51b41e3b30f242842cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Bardaj=C3=AD=20Puig?= Date: Mon, 9 Sep 2024 16:34:29 +0200 Subject: [PATCH 1/2] Add disconnect events --- example/package-lock.json | 2 +- example/src/App.tsx | 13 ++++++++++--- package-lock.json | 36 +++++++++++++++++++++++------------- package.json | 2 +- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/example/package-lock.json b/example/package-lock.json index 1ba6668..16833a7 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -45,7 +45,7 @@ "version": "2.8.0", "license": "ISC", "dependencies": { - "@sats-connect/core": "0.2.2", + "@sats-connect/core": "0.3.0-580becb", "@sats-connect/make-default-provider-config": "0.0.5", "@sats-connect/ui": "0.0.6" }, diff --git a/example/src/App.tsx b/example/src/App.tsx index 4835074..3c9cd7a 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -63,12 +63,19 @@ function AppWithProviders({ children }: React.PropsWithChildren<{}>) { useEffect(() => { if (btcAddressInfo.length < 1) return; - const removeListener = Wallet.addListener('accountChange', (ev) => { + const removeListenerAccountChange = Wallet.addListener('accountChange', (ev) => { console.log('The account has changed.', ev); }); - return removeListener; - }); + const removeListenerDisconnect = Wallet.addListener('disconnect', (ev) => { + console.log('The wallet has disconnected.', ev); + }); + + return () => { + removeListenerAccountChange(); + removeListenerDisconnect(); + }; + }, [btcAddressInfo]); const onConnectLegacy = useCallback(() => { (async () => { diff --git a/package-lock.json b/package-lock.json index e2c5846..9c06093 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.8.0", "license": "ISC", "dependencies": { - "@sats-connect/core": "0.2.2", + "@sats-connect/core": "0.3.0-580becb", "@sats-connect/make-default-provider-config": "0.0.5", "@sats-connect/ui": "0.0.6" }, @@ -1441,12 +1441,12 @@ } }, "node_modules/@noble/hashes": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz", - "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==", + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.5.0.tgz", + "integrity": "sha512-1j6kQFb7QRru7eKN3ZDvRcP13rugwdxZqCjbiAVZfIJwgj2A65UmT4TgARXGlXgnRkORLTDTrO19ZErt7+QXgA==", "license": "MIT", "engines": { - "node": ">= 16" + "node": "^14.21.3 || >=16" }, "funding": { "url": "https://paulmillr.com/funding/" @@ -1718,9 +1718,10 @@ ] }, "node_modules/@sats-connect/core": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/@sats-connect/core/-/core-0.2.2.tgz", - "integrity": "sha512-nl3zPnV1UBllYAniDfhM/oSFGQ2qy4cCg1YwxJZ+RQMwlTMrVh2f3lJ//dIIo9RgQPrtHpwrAaaWW0VpfqDQbg==", + "version": "0.3.0-580becb", + "resolved": "https://registry.npmjs.org/@sats-connect/core/-/core-0.3.0-580becb.tgz", + "integrity": "sha512-UICC2rlQbQi+U0Vgt/aF0oZcDD00Uacax4Yc7vapGibtskMVkNy6D2PgE2C1rhwKdRyEGSZY+bvepLX+JcHaiA==", + "license": "ISC", "dependencies": { "axios": "1.7.4", "bitcoin-address-validation": "2.2.3", @@ -1992,12 +1993,14 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" }, "node_modules/axios": { "version": "1.7.4", "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", + "license": "MIT", "dependencies": { "follow-redirects": "^1.15.6", "form-data": "^4.0.0", @@ -2561,6 +2564,7 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", "dependencies": { "delayed-stream": "~1.0.0" }, @@ -2672,6 +2676,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", "engines": { "node": ">=0.4.0" } @@ -2940,15 +2945,16 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.6", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", - "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "version": "1.15.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", + "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", "funding": [ { "type": "individual", "url": "https://github.com/sponsors/RubenVerborgh" } ], + "license": "MIT", "engines": { "node": ">=4.0" }, @@ -2990,6 +2996,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "license": "MIT", "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -4626,6 +4633,7 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -4634,6 +4642,7 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", "dependencies": { "mime-db": "1.52.0" }, @@ -5110,7 +5119,8 @@ "node_modules/proxy-from-env": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" }, "node_modules/punycode": { "version": "2.3.1", diff --git a/package.json b/package.json index 570c961..e0b3427 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ ] }, "dependencies": { - "@sats-connect/core": "0.2.2", + "@sats-connect/core": "0.3.0-580becb", "@sats-connect/make-default-provider-config": "0.0.5", "@sats-connect/ui": "0.0.6" }, From 4ecf4c6f3331f09eb5370aca3ca59a95074f7f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Bardaj=C3=AD=20Puig?= Date: Mon, 9 Sep 2024 18:16:04 +0200 Subject: [PATCH 2/2] Update example app to handle disconnection events --- example/src/App.tsx | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 3c9cd7a..0f71dea 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -60,6 +60,19 @@ function AppWithProviders({ children }: React.PropsWithChildren<{}>) { const isConnected = btcAddressInfo.length + stxAddressInfo.length > 0; + const clearAppData = useCallback(() => { + setBtcAddressInfo([]); + setStxAddressInfo([]); + queryClient.clear(); + }, [queryClient, setBtcAddressInfo, setStxAddressInfo]); + + const onDisconnect = useCallback(() => { + (async () => { + await Wallet.disconnect(); + clearAppData(); + })().catch(console.error); + }, [queryClient, setBtcAddressInfo, setStxAddressInfo]); + useEffect(() => { if (btcAddressInfo.length < 1) return; @@ -68,7 +81,8 @@ function AppWithProviders({ children }: React.PropsWithChildren<{}>) { }); const removeListenerDisconnect = Wallet.addListener('disconnect', (ev) => { - console.log('The wallet has disconnected.', ev); + console.log('The wallet has been disconnected. Event:', ev); + clearAppData(); }); return () => { @@ -119,15 +133,6 @@ function AppWithProviders({ children }: React.PropsWithChildren<{}>) { })().catch(console.error); }, [setBtcAddressInfo, setStxAddressInfo]); - const onDisconnect = useCallback(() => { - (async () => { - await Wallet.disconnect(); - setBtcAddressInfo([]); - setStxAddressInfo([]); - queryClient.clear(); - })().catch(console.error); - }, [queryClient, setBtcAddressInfo, setStxAddressInfo]); - const connectionContextValue = useMemo( () => ({ network, btcAddressInfo, stxAddressInfo, onDisconnect }), [network, btcAddressInfo, stxAddressInfo, onDisconnect],