-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add waitFor() for polling check ready status
- Loading branch information
Showing
3 changed files
with
85 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { waitFor } from './waitFor'; | ||
|
||
describe('waitFor()', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
it('resolves after second check', () => { | ||
const check = jest.fn(); | ||
check.mockReturnValueOnce(false); | ||
check.mockReturnValueOnce(true); | ||
|
||
const result = waitFor(check); | ||
jest.runAllTimers(); | ||
|
||
expect(check).toHaveBeenCalledTimes(2); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
return expect(result).resolves.toEqual(true); | ||
}); | ||
|
||
it('rejects when check throws error', () => { | ||
const check = jest.fn(); | ||
check.mockReturnValueOnce(false); | ||
check.mockImplementationOnce(() => { | ||
throw new Error('check error'); | ||
}); | ||
|
||
const result = waitFor(check); | ||
jest.runAllTimers(); | ||
|
||
expect(check).toHaveBeenCalledTimes(2); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
return expect(result).rejects.toEqual(new Error('check error')); | ||
}); | ||
}); |
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,48 @@ | ||
import { wait } from './wait'; | ||
|
||
/** | ||
* check function that returns check result | ||
* falsy values means not ready, truthy values will be used to resolve the promise | ||
*/ | ||
type WaitForCheckFun<T> = () => T; | ||
|
||
interface WaitForOptions { | ||
/** | ||
* the interval(ms) for each check, default to 200ms | ||
*/ | ||
checkInterval?: number; | ||
/** | ||
* throw a timeout error and reject the promise after given timeout(ms), | ||
* default is no timeout | ||
*/ | ||
timeout?: number; | ||
} | ||
|
||
export function waitFor<T>(check: WaitForCheckFun<T>, options?: WaitForOptions): Promise<T> { | ||
const { checkInterval = 200, timeout } = options ?? {}; | ||
|
||
const { afterReady, setReady, setFailed } = wait<T>(); | ||
|
||
const startTimestamp = Date.now(); | ||
const doCheck = () => { | ||
try { | ||
const result = check(); | ||
if (result) { | ||
setReady(result); | ||
return; | ||
} | ||
|
||
if (timeout && Date.now() - startTimestamp > timeout) { | ||
setFailed((new Error('timeout') as unknown) as T); | ||
return; | ||
} | ||
|
||
setTimeout(doCheck, checkInterval); | ||
} catch (err) { | ||
setFailed(err); | ||
} | ||
}; | ||
doCheck(); | ||
|
||
return afterReady(); | ||
} |