1
1
import { getBlocknative } from './services'
2
2
import { writable , derived , get } from 'svelte/store'
3
- import { wait , makeCancelable } from './utilities'
3
+ import { wait , makeCancelable , createInterval } from './utilities'
4
4
import { validateWalletInterface , validateType } from './validation'
5
5
import {
6
6
WritableStore ,
@@ -70,7 +70,7 @@ export const state = derived(
70
70
)
71
71
72
72
// keep track of intervals that are syncing state so they can be cleared
73
- let currentSyncerIntervals : ( number | undefined ) [ ] = [ ]
73
+ let currentSyncerIntervals : ( { clear : ( ) => void } | undefined ) [ ] = [ ]
74
74
75
75
export const walletInterface : WalletInterfaceStore = createWalletInterfaceStore (
76
76
null
@@ -79,7 +79,8 @@ export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
79
79
walletInterface . subscribe ( ( walletInterface : WalletInterface | null ) => {
80
80
// clear all current intervals if they exist
81
81
currentSyncerIntervals . forEach (
82
- ( interval : number | undefined ) => interval && clearInterval ( interval )
82
+ ( interval : { clear : ( ) => void } | undefined ) =>
83
+ interval && interval . clear ( )
83
84
)
84
85
85
86
const currentState = get ( state )
@@ -138,7 +139,7 @@ export function resetWalletState(options?: {
138
139
url : undefined
139
140
} ) )
140
141
141
- disconnected &&
142
+ ! disconnected &&
142
143
currentInterface . disconnect &&
143
144
currentInterface . disconnect ( )
144
145
@@ -175,8 +176,10 @@ function createWalletInterfaceStore(
175
176
return {
176
177
subscribe,
177
178
update,
178
- set : ( walletInterface : WalletInterface ) => {
179
- validateWalletInterface ( walletInterface )
179
+ set : ( walletInterface : WalletInterface | null ) => {
180
+ if ( walletInterface ) {
181
+ validateWalletInterface ( walletInterface )
182
+ }
180
183
set ( walletInterface )
181
184
}
182
185
}
@@ -228,11 +231,11 @@ function createWalletStateSliceStore(options: {
228
231
}
229
232
230
233
if ( get ) {
231
- const interval : any = setInterval ( ( ) => {
234
+ const interval : any = createInterval ( ( ) => {
232
235
get ( )
233
236
. then ( newVal => {
234
237
if ( newVal || currentState !== initialState ) {
235
- set ( newVal )
238
+ interval . status . active && set ( newVal )
236
239
}
237
240
} )
238
241
. catch ( ( err : any ) => {
0 commit comments