-
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.
- Loading branch information
Showing
5 changed files
with
86 additions
and
1 deletion.
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
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,50 @@ | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { onTimeout } from './onTimeout' | ||
|
||
describe('onTimeout', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
it('should execute the callback', () => { | ||
const callback = vi.fn() | ||
onTimeout(callback, 1000) | ||
vi.runAllTimers() | ||
expect(callback).toHaveBeenCalledOnce() | ||
}) | ||
it('should call the callback after the timeout', () => { | ||
const start = Date.now() | ||
let end = 0 | ||
const callback = vi.fn(() => { | ||
end = Date.now() | ||
}) | ||
const timeout = 100 | ||
onTimeout(callback, timeout) | ||
vi.runAllTimers() | ||
expect(callback).toHaveBeenCalled() | ||
|
||
const diff = end - start | ||
expect(diff).toBeGreaterThanOrEqual(timeout) | ||
}) | ||
it('should call the callback with the given arguments', async () => { | ||
const callback = vi.fn() | ||
const args = [1, 2, 3] | ||
onTimeout(callback, 100, ...args) | ||
vi.runAllTimers() | ||
expect(callback).toHaveBeenCalledWith(...args) | ||
}) | ||
it('return value:removeTimeout should remove the timeout', async () => { | ||
const callback = vi.fn() | ||
const timeout = 100 | ||
const start = Date.now() | ||
const { removeTimeout } = onTimeout(callback, timeout) | ||
removeTimeout() | ||
vi.runAllTimers() | ||
const end = Date.now() | ||
expect(callback).not.toHaveBeenCalled() | ||
expect(end - start).toBeLessThan(timeout) | ||
}) | ||
}) |
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,33 @@ | ||
interface ReturnType { | ||
removeTimeout: () => void | ||
} | ||
|
||
/** | ||
* wrap for setTimeout, return a function to remove the timeout | ||
* @param callback equal to setTimeout's first param | ||
* @param ms equal to setTimeout's second param | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/onTimeout.ts | ||
* @example | ||
* ```ts | ||
* const { removeTimeout } = onTimeout(() => { | ||
* console.log('timeout') | ||
* }, 1000) // print 'timeout' after 1s | ||
* | ||
* removeTimeout() // on Destroy, cancel the timeout | ||
*/ | ||
export function onTimeout(callback: (args: void) => void, ms?: number): ReturnType | ||
export function onTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): ReturnType | ||
export function onTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs) { | ||
const timeoutId = setTimeout( | ||
() => callback(...args), | ||
ms, | ||
) | ||
|
||
const removeTimeout = () => { | ||
clearTimeout(timeoutId) | ||
} | ||
|
||
return { | ||
removeTimeout, | ||
} | ||
} |
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