Skip to content

Commit

Permalink
retry in won function
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Jan 22, 2024
1 parent ad2d1c3 commit 0780545
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"Overridable",
"pako",
"payloadset",
"Promisable",
"quadkey",
"quadkeys",
"rdns",
Expand Down
49 changes: 49 additions & 0 deletions .yarn/versions/37922f8f.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
releases:
"@xyo-network/react-address": patch
"@xyo-network/react-address-history": patch
"@xyo-network/react-address-plugin": patch
"@xyo-network/react-address-render": patch
"@xyo-network/react-aggregate-price-plugin": patch
"@xyo-network/react-app-settings": patch
"@xyo-network/react-appbar": patch
"@xyo-network/react-archivist": patch
"@xyo-network/react-block": patch
"@xyo-network/react-boundwitness-plugin": patch
"@xyo-network/react-card": patch
"@xyo-network/react-coin-gecko-prices-plugin": patch
"@xyo-network/react-connected-accounts": patch
"@xyo-network/react-crypto-market-uniswap-plugin": patch
"@xyo-network/react-crypto-prices-plugin": patch
"@xyo-network/react-default-plugin": patch
"@xyo-network/react-details-plugin": patch
"@xyo-network/react-diviner": patch
"@xyo-network/react-elevation-map-quadkey-plugin": patch
"@xyo-network/react-embed": patch
"@xyo-network/react-ethereum-gas-price-blocknative-plugin": patch
"@xyo-network/react-ethereum-gas-price-etherchain-plugins": patch
"@xyo-network/react-ethereum-gas-price-etherchain-v2-plugin": patch
"@xyo-network/react-ethereum-gas-price-ethers-plugin": patch
"@xyo-network/react-ethereum-gas-price-etherscan-plugin": patch
"@xyo-network/react-ethereum-gas-price-ethgasstation-plugin": patch
"@xyo-network/react-ethereum-gas-price-payload-plugins": patch
"@xyo-network/react-ethereum-gas-price-plugin": patch
"@xyo-network/react-location-heatmap-quadkey-plugin": patch
"@xyo-network/react-location-point-map-plugin": patch
"@xyo-network/react-location-points-map-plugin": patch
"@xyo-network/react-module": patch
"@xyo-network/react-modules": patch
"@xyo-network/react-nft-score-plugin": patch
"@xyo-network/react-node": patch
"@xyo-network/react-node-context": patch
"@xyo-network/react-node-provider": patch
"@xyo-network/react-plugins": patch
"@xyo-network/react-pointer-plugin": patch
"@xyo-network/react-price-forecast-plugin": patch
"@xyo-network/react-schema-plugin": patch
"@xyo-network/react-sdk": patch
"@xyo-network/react-sentinel": patch
"@xyo-network/react-witness": patch
"@xyo-network/sdk-xyo-react": patch

undecided:
- "@xyo-network/react-indexed-results"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { delay } from '@xylabs/delay'
import { DivinerInstance } from '@xyo-network/diviner-model'
import { Payload } from '@xyo-network/payload-model'

import { ParseIndexedResults } from '../../interfaces'
import { retry } from './retry'

const divineSingleIndexedResultsInner = async <TPayload extends Payload = Payload>(
diviner: DivinerInstance,
Expand All @@ -24,14 +24,6 @@ export const divineSingleIndexedResults = async <TPayload extends Payload = Payl
retries = 0,
interval = 100,
backoff = 2,
): Promise<TPayload[] | null> => {
const results = await divineSingleIndexedResultsInner(diviner, indexedQueries, parseIndexedResults)
if (results) {
return results
}
if (retries <= 0) {
return null
}
await delay(interval)
return divineSingleIndexedResults(diviner, indexedQueries, parseIndexedResults, retries - 1, interval * backoff, backoff)
): Promise<TPayload[] | null | undefined> => {
return await retry(() => divineSingleIndexedResultsInner(diviner, indexedQueries, parseIndexedResults), { backoff, interval, retries })
}
23 changes: 23 additions & 0 deletions packages/sdk/packages/indexed-results/src/hooks/support/retry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { delay } from '@xylabs/delay'
import { Promisable } from '@xylabs/promise'
import { AnyObject } from '@xyo-network/object'

export interface RetryConfig<T = AnyObject> {
backoff?: number
complete?: (result?: T) => boolean
interval?: number
retries?: number
}

export const retry = async <T = AnyObject,>(func: () => Promisable<T | undefined>, config?: RetryConfig<T>): Promise<T | undefined> => {
const { complete = (value: T | undefined) => value !== undefined, retries = 0, interval = 100, backoff = 2 } = config ?? {}
const result = await func()
if (complete(result)) {
return result
}
if (retries <= 0) {
return undefined
}
await delay(interval)
return retry(func, { backoff, complete, interval: interval * backoff, retries: retries - 1 })
}

0 comments on commit 0780545

Please sign in to comment.