|
1 | 1 | import { writable, derived, get } from 'svelte/store'
|
| 2 | +import debounce from 'lodash.debounce' |
2 | 3 | import { getBlocknative } from './services'
|
3 | 4 | import { wait, makeQuerablePromise } from './utilities'
|
4 | 5 | import { validateWalletInterface, validateType } from './validation'
|
@@ -171,26 +172,32 @@ function createBalanceStore(initialState: string | null): BalanceStore {
|
171 | 172 | cancel = syncStateWithTimeout({
|
172 | 173 | getState: stateSyncer.get,
|
173 | 174 | setState: set,
|
174 |
| - timeout: 4000, |
| 175 | + timeout: 2000, |
175 | 176 | currentBalance: get(balance)
|
176 | 177 | })
|
177 | 178 |
|
178 | 179 | if (emitterAddress !== $address) {
|
179 | 180 | const blocknative = getBlocknative()
|
| 181 | + |
180 | 182 | emitter = blocknative.account(blocknative.clientIndex, $address)
|
181 | 183 | .emitter
|
182 |
| - emitter.on('txConfirmed', () => { |
183 |
| - if (stateSyncer.get) { |
184 |
| - cancel = syncStateWithTimeout({ |
185 |
| - getState: stateSyncer.get, |
186 |
| - setState: set, |
187 |
| - timeout: 1500, |
188 |
| - currentBalance: get(balance) |
189 |
| - }) |
190 |
| - } |
191 |
| - |
192 |
| - return false |
193 |
| - }) |
| 184 | + |
| 185 | + emitter.on( |
| 186 | + 'txConfirmed', |
| 187 | + debounce(() => { |
| 188 | + if (stateSyncer.get) { |
| 189 | + cancel = syncStateWithTimeout({ |
| 190 | + getState: stateSyncer.get, |
| 191 | + setState: set, |
| 192 | + timeout: 2000, |
| 193 | + currentBalance: get(balance), |
| 194 | + pollStart: Date.now() |
| 195 | + }) |
| 196 | + } |
| 197 | + |
| 198 | + return false |
| 199 | + }, 500) |
| 200 | + ) |
194 | 201 |
|
195 | 202 | emitter.on('all', () => false)
|
196 | 203 |
|
@@ -233,59 +240,56 @@ function createBalanceStore(initialState: string | null): BalanceStore {
|
233 | 240 | }
|
234 | 241 | }
|
235 | 242 |
|
236 |
| -let timesTried: number = 0 |
237 |
| - |
238 | 243 | function syncStateWithTimeout(options: {
|
239 | 244 | getState: () => Promise<string | number | null>
|
240 | 245 | setState: (newState: string) => void
|
241 | 246 | timeout: number
|
242 | 247 | currentBalance: string
|
| 248 | + pollStart?: number |
243 | 249 | }) {
|
244 |
| - if (timesTried < 4) { |
245 |
| - timesTried++ |
246 |
| - |
247 |
| - const { getState, setState, timeout, currentBalance } = options |
248 |
| - const prom = makeQuerablePromise( |
249 |
| - new Cancelable( |
250 |
| - ( |
251 |
| - resolve: (val: string | number | null) => void, |
252 |
| - reject: (err: any) => void, |
253 |
| - onCancel: (callback: () => void) => void |
254 |
| - ) => { |
255 |
| - getState().then(resolve) |
256 |
| - |
257 |
| - onCancel(() => { |
258 |
| - balanceSyncStatus.error = |
259 |
| - 'There was a problem getting the balance of this wallet' |
260 |
| - }) |
261 |
| - } |
262 |
| - ).catch(() => {}) |
263 |
| - ) |
| 250 | + const { getState, setState, timeout, currentBalance, pollStart } = options |
264 | 251 |
|
265 |
| - balanceSyncStatus.syncing = prom |
| 252 | + if (pollStart && Date.now() - pollStart > 25000) { |
| 253 | + return () => {} |
| 254 | + } |
266 | 255 |
|
267 |
| - prom |
268 |
| - .then(async (result: string) => { |
269 |
| - if (result === currentBalance) { |
270 |
| - await wait(150) |
271 |
| - syncStateWithTimeout(options) |
272 |
| - } else { |
273 |
| - setState(result) |
274 |
| - } |
275 |
| - }) |
276 |
| - .catch(() => {}) |
| 256 | + const prom = makeQuerablePromise( |
| 257 | + new Cancelable( |
| 258 | + ( |
| 259 | + resolve: (val: string | number | null) => void, |
| 260 | + reject: (err: any) => void, |
| 261 | + onCancel: (callback: () => void) => void |
| 262 | + ) => { |
| 263 | + getState().then(resolve) |
| 264 | + |
| 265 | + onCancel(() => { |
| 266 | + balanceSyncStatus.error = |
| 267 | + 'There was a problem getting the balance of this wallet' |
| 268 | + }) |
| 269 | + } |
| 270 | + ).catch(() => {}) |
| 271 | + ) |
277 | 272 |
|
278 |
| - const timedOut = wait(timeout) |
| 273 | + balanceSyncStatus.syncing = prom |
279 | 274 |
|
280 |
| - timedOut.then(() => { |
281 |
| - if (!prom.isFulfilled()) { |
282 |
| - prom.cancel(() => {}) |
| 275 | + prom |
| 276 | + .then(async (result: string) => { |
| 277 | + if (result === currentBalance && pollStart) { |
| 278 | + await wait(350) |
| 279 | + syncStateWithTimeout(options) |
| 280 | + } else { |
| 281 | + setState(result) |
283 | 282 | }
|
284 | 283 | })
|
| 284 | + .catch(() => {}) |
285 | 285 |
|
286 |
| - return () => prom.cancel(() => {}) |
287 |
| - } else { |
288 |
| - timesTried = 0 |
289 |
| - return () => {} |
290 |
| - } |
| 286 | + const timedOut = wait(timeout) |
| 287 | + |
| 288 | + timedOut.then(() => { |
| 289 | + if (!prom.isFulfilled()) { |
| 290 | + prom.cancel(() => {}) |
| 291 | + } |
| 292 | + }) |
| 293 | + |
| 294 | + return () => prom.cancel(() => {}) |
291 | 295 | }
|
0 commit comments