Commit dbbffd17 authored by Moody Salem's avatar Moody Salem

fix: improve cancel/retry detection

parent 85d8b1e5
...@@ -5,7 +5,7 @@ import { useActiveWeb3React } from '../../hooks/web3' ...@@ -5,7 +5,7 @@ import { useActiveWeb3React } from '../../hooks/web3'
import { useMulticall2Contract } from '../../hooks/useContract' import { useMulticall2Contract } from '../../hooks/useContract'
import useDebounce from '../../hooks/useDebounce' import useDebounce from '../../hooks/useDebounce'
import chunkArray from '../../utils/chunkArray' import chunkArray from '../../utils/chunkArray'
import { CancelledError, retry, RetryableError } from '../../utils/retry' import { retry, RetryableError } from '../../utils/retry'
import { useBlockNumber } from '../application/hooks' import { useBlockNumber } from '../application/hooks'
import { AppDispatch, AppState } from '../index' import { AppDispatch, AppState } from '../index'
import { import {
...@@ -217,7 +217,7 @@ export default function Updater(): null { ...@@ -217,7 +217,7 @@ export default function Updater(): null {
} }
}) })
.catch((error: any) => { .catch((error: any) => {
if (error instanceof CancelledError) { if (error.isCancelledError) {
console.debug('Cancelled fetch for blockNumber', latestBlockNumber) console.debug('Cancelled fetch for blockNumber', latestBlockNumber)
return return
} }
......
...@@ -10,6 +10,7 @@ function waitRandom(min: number, max: number): Promise<void> { ...@@ -10,6 +10,7 @@ function waitRandom(min: number, max: number): Promise<void> {
* This error is thrown if the function is cancelled before completing * This error is thrown if the function is cancelled before completing
*/ */
export class CancelledError extends Error { export class CancelledError extends Error {
public isCancelledError: true = true
constructor() { constructor() {
super('Cancelled') super('Cancelled')
} }
...@@ -18,7 +19,9 @@ export class CancelledError extends Error { ...@@ -18,7 +19,9 @@ export class CancelledError extends Error {
/** /**
* Throw this error if the function should retry * Throw this error if the function should retry
*/ */
export class RetryableError extends Error {} export class RetryableError extends Error {
public isRetryableError: true = true
}
/** /**
* Retries the function that returns the promise until the promise successfully resolves up to n retries * Retries the function that returns the promise until the promise successfully resolves up to n retries
...@@ -48,7 +51,7 @@ export function retry<T>( ...@@ -48,7 +51,7 @@ export function retry<T>(
if (completed) { if (completed) {
break break
} }
if (n <= 0 || !(error instanceof RetryableError)) { if (n <= 0 || !error.isRetryableError) {
reject(error) reject(error)
completed = true completed = true
break break
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment