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

Unsubscribe all events #15

Merged
merged 5 commits into from Jul 27, 2021
Merged
Changes from 4 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
41 changes: 21 additions & 20 deletions src/web3-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,29 @@ 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))
} else {
if (instance._provider && instance._provider.on) {
instance._provider.on('accountsChanged', () => setProvider(provider, true))
instance._provider.on('chainChanged', () => setProvider(provider, true))
}
})
}
update(() => ({
provider,
providerType: 'String',
connected: true,
chainId,
accounts,
instance
}))
}

const setBrowserProvider = async () => {
Expand All @@ -81,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,
Expand All @@ -94,7 +95,7 @@ export const createStore = () => {
return {
setBrowserProvider,
setProvider,
close,
disconnect,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave close as an alias not to break code on upgrade so brutally. close can console.log deprecated and call disconnect

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need the log then, unless you plan to change the function name to disconnect in the future.

subscribe
}
}
Expand Down