Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KABBOUCHI committed Jul 7, 2022
1 parent f988e87 commit ef526d7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,20 @@ await castDecoder.getSpell(
```js
import { retry } from '@instadapp/utils'

retry(() => asyncCall(), {
retry(() => asyncCall(), {
timeouts: [5_000, 10_000, 15_000], // timeouts for each retry attempt in ms
delay: 300 // delay between retries in ms
})
```

### wait

```js
import { wait } from '@instadapp/utils'

await wait(300)
```

### JsonRpcRetryProvider

```js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@instadapp/utils",
"version": "0.1.9",
"version": "0.1.10",
"description": "",
"repository": "instadapp/utils",
"license": "MIT",
Expand Down
15 changes: 10 additions & 5 deletions src/promises/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ export function retry (
options: RetryOptions,
retriesLeft?: number
) {
if (!retriesLeft) {
retriesLeft = options.timeouts.length
}

return new Promise((resolve, reject) => {
const { timeouts } = options

if (typeof retriesLeft === 'undefined' || retriesLeft === null) {
retriesLeft = timeouts.length
}

// Find the timeout for this specific iteration
const timeout = timeouts[timeouts.length - retriesLeft]

Expand All @@ -34,7 +35,7 @@ export function retry (
if (retriesLeft - 1 > 0) {
// Delay the new attempt slightly
return wait(options.delay || 300)
.then(retry.bind(null, retriesLeft - 1, operation, options))
.then(retry.bind(null, operation, options, retriesLeft - 1))
.then(resolve)
.catch(reject)
}
Expand All @@ -43,3 +44,7 @@ export function retry (
})
})
}

export {
wait
}
2 changes: 1 addition & 1 deletion test/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const defaultAbiFetcher = new AbiFetcher({
}
})

describe('abi', () => {
describe.skip('abi', () => {
test('can fetch abi', async () => {
const abi = await defaultAbiFetcher.get('0x0000000000000000000000000000000000001010', 'polygon')

Expand Down
2 changes: 1 addition & 1 deletion test/cast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ beforeAll(async () => {
await dsa.setInstance(21723)
})

describe('cast', () => {
describe.skip('cast', () => {
test('can get encoded spells from encoded data', () => {
const spells = dsa.Spell()

Expand Down
28 changes: 28 additions & 0 deletions test/promises.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, describe, test } from 'vitest'
import { retry, wait } from '../src'

describe('promises', () => {
test('retry', async () => {
let called = 0
async function testfn () {
called++
await wait(2)
throw new Error('test error')
}

await expect(retry(testfn, { timeouts: [1, 2, 3], delay: 1 })).rejects.toThrow('test error')
expect(called).toBe(3)
})

test('retry', async () => {
let called = 0
async function testfn () {
called++
await wait(10)
throw new Error('test error')
}

await expect(retry(testfn, { timeouts: [1, 2, 3], delay: 1 })).rejects.toThrow('operation timed out')
expect(called).toBe(3)
})
})

0 comments on commit ef526d7

Please sign in to comment.