Skip to content

Commit dd940ad

Browse files
Aaron BarnardAaron Barnard
authored andcommitted
Make sure address sub is called only when changed
1 parent f7d9fad commit dd940ad

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/stores.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,17 @@ function createWalletStateSliceStore(options: {
154154
const { parameter, initialState } = options
155155
const { subscribe, set } = writable(initialState)
156156

157+
let currentState: string | number | null | undefined
158+
const unsubscribe = subscribe(store => {
159+
currentState = store
160+
})
161+
157162
return {
158163
subscribe,
159-
reset: () => set(undefined),
164+
reset: () => {
165+
unsubscribe()
166+
set(undefined)
167+
},
160168
setStateSyncer: (stateSyncer: StateSyncer) => {
161169
validateType({ name: 'stateSyncer', value: stateSyncer, type: 'object' })
162170

@@ -177,14 +185,22 @@ function createWalletStateSliceStore(options: {
177185
})
178186

179187
if (onChange) {
180-
onChange(set)
188+
onChange(newVal => {
189+
if (newVal || currentState !== initialState) {
190+
set(newVal)
191+
}
192+
})
181193
return
182194
}
183195

184196
if (get) {
185197
const interval: any = setInterval(() => {
186198
get()
187-
.then(set)
199+
.then(newVal => {
200+
if (newVal || currentState !== initialState) {
201+
set(newVal)
202+
}
203+
})
188204
.catch((err: any) => {
189205
console.warn(
190206
`Error getting ${parameter} from state syncer: ${err}`

0 commit comments

Comments
 (0)