-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/test: convert setRafTimeout to ts, add tests
- Loading branch information
Coltin Kifer
committed
Sep 8, 2024
1 parent
bd93301
commit 4ab6aae
Showing
5 changed files
with
75 additions
and
23 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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
type CallbackType = (now: number) => void; | ||
|
||
/** | ||
* Calls requestAnimationFrame with a timeout delay | ||
* @param {Function} callback cb function | ||
* @param {number} timeout timeout in ms | ||
* @returns {void} void | ||
*/ | ||
export default function setRafTimeout(callback: CallbackType, timeout: number = 0): void { | ||
let currTime = -1; | ||
|
||
const shouldUpdate = (now: number) => { | ||
if (currTime < 0) { | ||
currTime = now; | ||
} | ||
|
||
if (now - currTime > timeout) { | ||
callback(now); | ||
currTime = -1; | ||
} else { | ||
// requestAnimationFrame is availble across most browsers | ||
requestAnimationFrame(shouldUpdate); | ||
} | ||
}; | ||
|
||
requestAnimationFrame(shouldUpdate); | ||
} |
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,22 @@ | ||
import setRafTimeout from '../src/setRafTimeout'; | ||
|
||
describe('setRafTimeout', () => { | ||
it('should call setRafTimeout', () => { | ||
vi.useFakeTimers(); | ||
const spy = vi.spyOn(window, 'requestAnimationFrame'); | ||
const cb = vi.fn(); | ||
setRafTimeout(cb); | ||
vi.runAllTimers(); | ||
expect(cb).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call requestAnimationFrame with a 1 second delay', () => { | ||
vi.useFakeTimers(); | ||
const spy = vi.spyOn(window, 'requestAnimationFrame'); | ||
const cb = vi.fn(); | ||
setRafTimeout(cb, 1000); | ||
expect(cb).not.toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
}); |
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