From cd54e39b3b4cb316de7cb1c74bafce0178223a6b Mon Sep 17 00:00:00 2001 From: byterose <77129089+byterose@users.noreply.github.com> Date: Mon, 26 Jul 2021 16:37:00 +0200 Subject: [PATCH 1/5] Unsubscribe all events __networkChanged__ will emit the network ID as a decimal string __chainChanged__ will emit the chain ID as a hexadecimal string I commented out the __chainChanged__ listener since only one chain / network listener is needed. --- src/web3-store.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/web3-store.js b/src/web3-store.js index 94c5770..02047ef 100644 --- a/src/web3-store.js +++ b/src/web3-store.js @@ -41,28 +41,31 @@ export const createStore = () => { if (getWindowEthereum()) getWindowEthereum().autoRefreshOnNetworkChange = false } - const setProvider = async provider => { + const setProvider = async (provider, callback?) => { init() const instance = new Web3(provider) const chainId = await instance.eth.getChainId() // no account with ganache const accounts = /127/.test(provider) ? [] : await instance.eth.getAccounts() - if (instance._provider && instance._provider.on) { - instance._provider.on('accountsChanged', () => setProvider(provider)) - instance._provider.on('chainChanged', () => setProvider(provider)) - instance._provider.on('networkChanged', () => setProvider(provider)) - } - update(previous => { - // TODO if (previous.instance) unsubcribe all events - return { - provider, - providerType: 'String', - connected: true, - chainId, - accounts, - instance, + if (callback) { + instance._provider.removeListener('accountsChanged', () => setProvider(provider, true)) + // instance._provider.removeListener('chainChanged', () => setProvider(provider, true)) + instance._provider.removeListener('networkChanged', () => setProvider(provider, true)) + } else { + if (instance._provider && instance._provider.on) { + instance._provider.on('accountsChanged', () => setProvider(provider, true)) + // instance._provider.on('chainChanged', () => setProvider(provider, true)) + instance._provider.on('networkChanged', () => setProvider(provider, true)) } - }) + } + update(() => ({ + provider, + providerType: 'String', + connected: true, + chainId, + accounts, + instance + })) } const setBrowserProvider = async () => { From d185a4e6089bf4ec526b68d647c25a0f1fbd3f4a Mon Sep 17 00:00:00 2001 From: byterose <77129089+byterose@users.noreply.github.com> Date: Mon, 26 Jul 2021 16:38:42 +0200 Subject: [PATCH 2/5] Format code --- src/web3-store.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web3-store.js b/src/web3-store.js index 02047ef..6b71300 100644 --- a/src/web3-store.js +++ b/src/web3-store.js @@ -49,8 +49,8 @@ export const createStore = () => { const accounts = /127/.test(provider) ? [] : await instance.eth.getAccounts() if (callback) { instance._provider.removeListener('accountsChanged', () => setProvider(provider, true)) - // instance._provider.removeListener('chainChanged', () => setProvider(provider, true)) - instance._provider.removeListener('networkChanged', () => setProvider(provider, true)) + // instance._provider.removeListener('chainChanged', () => setProvider(provider, true)) + instance._provider.removeListener('networkChanged', () => setProvider(provider, true)) } else { if (instance._provider && instance._provider.on) { instance._provider.on('accountsChanged', () => setProvider(provider, true)) From e7a8449706876787dcb79bacccd1d824dd397849 Mon Sep 17 00:00:00 2001 From: byterose <77129089+byterose@users.noreply.github.com> Date: Mon, 26 Jul 2021 16:49:10 +0200 Subject: [PATCH 3/5] Remove __networkChanged__ event listener I just noticed that __networkChanged__ is deprecated so I removed it. --- src/web3-store.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/web3-store.js b/src/web3-store.js index 6b71300..07123c0 100644 --- a/src/web3-store.js +++ b/src/web3-store.js @@ -49,13 +49,11 @@ export const createStore = () => { const accounts = /127/.test(provider) ? [] : await instance.eth.getAccounts() if (callback) { instance._provider.removeListener('accountsChanged', () => setProvider(provider, true)) - // instance._provider.removeListener('chainChanged', () => setProvider(provider, true)) - instance._provider.removeListener('networkChanged', () => setProvider(provider, true)) + instance._provider.removeListener('chainChanged', () => setProvider(provider, true)) } else { if (instance._provider && instance._provider.on) { instance._provider.on('accountsChanged', () => setProvider(provider, true)) - // instance._provider.on('chainChanged', () => setProvider(provider, true)) - instance._provider.on('networkChanged', () => setProvider(provider, true)) + instance._provider.on('chainChanged', () => setProvider(provider, true)) } } update(() => ({ From b63294d45a180754ea96c2807f8ed98458c9672f Mon Sep 17 00:00:00 2001 From: byterose <77129089+byterose@users.noreply.github.com> Date: Mon, 26 Jul 2021 16:53:59 +0200 Subject: [PATCH 4/5] Change __close__ method to __disconnect__ --- src/web3-store.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/web3-store.js b/src/web3-store.js index 07123c0..a34e48b 100644 --- a/src/web3-store.js +++ b/src/web3-store.js @@ -82,9 +82,9 @@ export const createStore = () => { })) } - const close = async (provider) => { - if(provider && provider.close) { - await provider.close() + const disconnect = async (provider) => { + if(provider && provider.disconnect) { + await provider.disconnect() } update(() => ({ connected: false, @@ -95,7 +95,7 @@ export const createStore = () => { return { setBrowserProvider, setProvider, - close, + disconnect, subscribe } } From 9babef30bca1c0a15ca65db54c5efffc3130352a Mon Sep 17 00:00:00 2001 From: byterose <77129089+byterose@users.noreply.github.com> Date: Tue, 27 Jul 2021 16:09:13 +0200 Subject: [PATCH 5/5] Change disconnect function name --- src/web3-store.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web3-store.js b/src/web3-store.js index a34e48b..cb96449 100644 --- a/src/web3-store.js +++ b/src/web3-store.js @@ -82,7 +82,7 @@ export const createStore = () => { })) } - const disconnect = async (provider) => { + const close = async (provider) => { if(provider && provider.disconnect) { await provider.disconnect() } @@ -95,7 +95,7 @@ export const createStore = () => { return { setBrowserProvider, setProvider, - disconnect, + close, subscribe } }