-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util): polyfil requestAnimationFrame, requestIdleCallbackFallback
Special thanks to Co-authored-by: Paul Irish <commits@paul.irish> Co-authored-by: Merten van Gerven <merten.vg@gmail.com>
- Loading branch information
1 parent
e5e13e2
commit 6dc4b73
Showing
3 changed files
with
29 additions
and
0 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
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,26 @@ | ||
interface IndexableWindow { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key: string]: any; | ||
} | ||
|
||
export const win = globalThis as IndexableWindow; | ||
|
||
const requestAnimationFrameFallback = (callback: FrameRequestCallback): ReturnType<typeof setTimeout> => | ||
setTimeout(() => callback(Date.now()), 1000 / 60); | ||
|
||
export const requestAnimationFrame: typeof globalThis.requestAnimationFrame = | ||
win.requestAnimationFrame || | ||
win.webkitRequestAnimationFrame || | ||
win.mozRequestAnimationFrame || | ||
requestAnimationFrameFallback; | ||
|
||
const requestIdleCallbackFallback = ( | ||
callback: () => void, | ||
options?: IdleRequestOptions, | ||
): ReturnType<typeof setTimeout> => setTimeout(callback, options?.timeout ?? 2000); | ||
|
||
export const requestIdleCallback: typeof globalThis.requestIdleCallback = | ||
win.requestIdleCallback || | ||
win.webkitRequestIdleCallback || | ||
win.mozRequestIdleCallback || | ||
requestIdleCallbackFallback; |