-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(module): Add test for module & category (#3390)
- update some browser & worker module as function to facilitate test - add test case for internal/category Ref #3333
- Loading branch information
Showing
6 changed files
with
158 additions
and
33 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 |
---|---|---|
|
@@ -14,22 +14,43 @@ export { | |
requestIdleCallback, cancelIdleCallback | ||
}; | ||
|
||
const win = (() => { | ||
const root = (typeof globalThis === "object" && globalThis !== null && globalThis.Object === Object && globalThis) || | ||
/** | ||
* Get global object | ||
* @param {Array} g global object candidates | ||
Check warning on line 19 in src/module/browser.ts GitHub Actions / test (16.x)
Check warning on line 19 in src/module/browser.ts GitHub Actions / test (17.x)
Check warning on line 19 in src/module/browser.ts GitHub Actions / test (18.x)
|
||
* @returns {object} window object | ||
* @private | ||
*/ | ||
function getGlobal() { | ||
return (typeof globalThis === "object" && globalThis !== null && globalThis.Object === Object && globalThis) || | ||
(typeof global === "object" && global !== null && global.Object === Object && global) || | ||
(typeof self === "object" && self !== null && self.Object === Object && self); | ||
|
||
return root || Function("return this")(); | ||
})(); | ||
/* eslint-enable no-new-func, no-undef */ | ||
(typeof self === "object" && self !== null && self.Object === Object && self) || | ||
Function("return this")(); | ||
} | ||
|
||
// fallback for non-supported environments | ||
const hasRAF = typeof win.requestAnimationFrame === "function"; | ||
const hasRIC = typeof win.requestIdleCallback === "function"; | ||
/** | ||
* Get fallback object | ||
* @param {object} w global object | ||
* @returns {Array} fallback object array | ||
* @private | ||
*/ | ||
function getFallback(w) { | ||
const hasRAF = typeof w?.requestAnimationFrame === "function"; | ||
const hasRIC = typeof w?.requestIdleCallback === "function"; | ||
|
||
const requestAnimationFrame = hasRAF ? win.requestAnimationFrame : (cb => setTimeout(cb, 1)); | ||
const cancelAnimationFrame = hasRAF ? win.cancelAnimationFrame : (id => clearTimeout(id)); | ||
const requestIdleCallback = hasRIC ? win.requestIdleCallback : requestAnimationFrame; | ||
const cancelIdleCallback = hasRIC ? win.cancelIdleCallback : cancelAnimationFrame; | ||
return [ | ||
hasRAF ? w.requestAnimationFrame : (cb => setTimeout(cb, 1)), | ||
hasRAF ? w.cancelAnimationFrame : (id => clearTimeout(id)), | ||
hasRIC ? w.requestIdleCallback : requestAnimationFrame, | ||
hasRIC ? w.cancelIdleCallback : cancelAnimationFrame | ||
]; | ||
} | ||
|
||
const win = getGlobal(); | ||
const doc = win?.document; | ||
|
||
const [ | ||
requestAnimationFrame, | ||
cancelAnimationFrame, | ||
requestIdleCallback, | ||
cancelIdleCallback | ||
] = getFallback(win); |
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