-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): CHECKOUT-4412 Retry if unable to load asset chunk
- Loading branch information
Showing
5 changed files
with
89 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
import { RetryOptions } from './retry'; | ||
|
||
export type RetryOptions = RetryOptions; | ||
|
||
export * from './emptyData'; | ||
export { default as retry } from './retry'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import retry from './retry'; | ||
|
||
describe('retry()', () => { | ||
it('retries async call for specified number of times', async () => { | ||
const error = new Error('Request timeout'); | ||
const call = jest.fn(() => Promise.reject(error)); | ||
|
||
try { | ||
await retry(() => call(), { count: 3, interval: 1 }); | ||
} catch (thrown) { | ||
expect(call) | ||
.toHaveBeenCalledTimes(3); | ||
expect(thrown) | ||
.toEqual(error); | ||
} | ||
}); | ||
|
||
it('stops retrying async call if it succeeds', async () => { | ||
let times = 0; | ||
|
||
const response = 'Foobar'; | ||
const error = new Error('Request timeout'); | ||
const call = jest.fn(() => { | ||
times++; | ||
|
||
return times === 2 ? | ||
Promise.resolve(response) : | ||
Promise.reject(error); | ||
}); | ||
|
||
const output = await retry(() => call(), { interval: 1 }); | ||
|
||
expect(call) | ||
.toHaveBeenCalledTimes(2); | ||
expect(output) | ||
.toEqual(response); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const DEFAULT_OPTIONS = { | ||
count: 5, | ||
interval: 1000, | ||
}; | ||
|
||
export interface RetryOptions { | ||
count?: number; | ||
interval?: number; | ||
} | ||
|
||
export default async function retry<T>( | ||
fn: () => Promise<T>, | ||
options?: RetryOptions | ||
): Promise<T> { | ||
const { count, interval } = { ...DEFAULT_OPTIONS, ...options }; | ||
|
||
try { | ||
return await fn(); | ||
} catch (error) { | ||
if (count === 1) { | ||
throw error; | ||
} | ||
|
||
await new Promise(resolve => setTimeout(resolve, interval)); | ||
|
||
return retry(fn, { interval, count: count - 1 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters