Skip to content

Commit 75ac4f2

Browse files
authored
Merge pull request #397 from blocknative/fix/state-reset
Fix: Make sure stores are reset correctly
2 parents 14a69a0 + 8487f6b commit 75ac4f2

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

src/stores.ts

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export let network: WalletStateSliceStore
5454
export let balance: BalanceStore | WalletStateSliceStore
5555
export let wallet: WritableStore
5656
export let state: ReadableStore
57+
export let walletInterface: WalletInterfaceStore
58+
59+
let currentSyncerIntervals: ({ clear: () => void } | undefined)[]
5760

5861
export function initializeStores() {
5962
address = createWalletStateSliceStore({
@@ -96,43 +99,39 @@ export function initializeStores() {
9699
}
97100
}
98101
)
99-
}
100102

101-
// keep track of intervals that are syncing state so they can be cleared
102-
let currentSyncerIntervals: ({ clear: () => void } | undefined)[] = []
103-
104-
export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
105-
null
106-
)
107-
108-
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
109-
// make sure that stores have been initialized
110-
if (state) {
111-
// clear all current intervals if they exist
112-
currentSyncerIntervals.forEach(
113-
(interval: { clear: () => void } | undefined) =>
114-
interval && interval.clear()
115-
)
116-
117-
const currentState = get(state)
118-
119-
// reset state
120-
currentState.balance && balance.reset()
121-
currentState.address && address.reset()
122-
currentState.network && network.reset()
123-
124-
if (walletInterface) {
125-
// start syncing state and save intervals
126-
currentSyncerIntervals = [
127-
address.setStateSyncer(walletInterface.address),
128-
network.setStateSyncer(walletInterface.network),
129-
balance.setStateSyncer(walletInterface.balance)
130-
]
131-
}
103+
currentSyncerIntervals = []
132104

133-
resetCheckModules()
134-
}
135-
})
105+
walletInterface = createWalletInterfaceStore(null)
106+
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
107+
// make sure that stores have been initialized
108+
if (state) {
109+
// clear all current intervals if they exist
110+
currentSyncerIntervals.forEach(
111+
(interval: { clear: () => void } | undefined) =>
112+
interval && interval.clear()
113+
)
114+
115+
const currentState = get(state)
116+
117+
// reset state
118+
currentState.balance && balance.reset()
119+
currentState.address && address.reset()
120+
currentState.network && network.reset()
121+
122+
if (walletInterface) {
123+
// start syncing state and save intervals
124+
currentSyncerIntervals = [
125+
address.setStateSyncer(walletInterface.address),
126+
network.setStateSyncer(walletInterface.network),
127+
balance.setStateSyncer(walletInterface.balance)
128+
]
129+
}
130+
131+
resetCheckModules()
132+
}
133+
})
134+
}
136135

137136
export function resetWalletState(options?: {
138137
disconnected: boolean

0 commit comments

Comments
 (0)