generated from antfu/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactors queueMicroTask code and adds utils (#6)
* chore: refactors queueMicroTask code and adds utils * chore: changes necessary comments and error handling --------- Co-authored-by: Harsh Kumar Choudhary <harsh.choudhary@HARSH-CHOUDHARY-MAC.local>
- Loading branch information
Showing
7 changed files
with
114 additions
and
128 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 |
---|---|---|
@@ -1,3 +1 @@ | ||
import { IdleQueue } from './idleQueue' | ||
|
||
export { IdleQueue } | ||
export { IdleQueue } from './idleQueue' |
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,16 @@ | ||
declare global { | ||
interface Window { | ||
safari: any | ||
} | ||
} | ||
|
||
export const isBrowser: boolean | ||
= typeof window !== 'undefined' | ||
&& typeof document !== 'undefined' | ||
&& typeof navigator !== 'undefined' | ||
|
||
export const isSafari: boolean | ||
= isBrowser | ||
&& 'safari' in window | ||
&& typeof window.safari === 'object' | ||
&& 'pushNotification' in window.safari |
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,3 @@ | ||
export function now(): number { | ||
return Date.now() | ||
} |
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,41 @@ | ||
import { isBrowser } from './env' | ||
|
||
type Microtask = () => void | ||
|
||
function createQueueMicrotaskViaPromises(): (microtask: Microtask) => void { | ||
return (microtask: Microtask) => { | ||
Promise.resolve().then(microtask) | ||
} | ||
} | ||
|
||
function createQueueMicrotaskViaMutationObserver(): (microtask: Microtask) => void { | ||
let mutationCounter = 0 | ||
let microtaskQueue: Microtask[] = [] | ||
const observer = new MutationObserver(() => { | ||
microtaskQueue.forEach(microtask => microtask()) | ||
microtaskQueue = [] | ||
}) | ||
const node = document.createTextNode('') | ||
observer.observe(node, { characterData: true }) | ||
|
||
return (microtask: Microtask) => { | ||
microtaskQueue.push(microtask) | ||
|
||
// MutationObserver is a fallback for when native microtasks are unavailable | ||
node.data = String(++mutationCounter % 2) | ||
} | ||
} | ||
|
||
/** | ||
/** | ||
* Schedules a microtask using the best available browser method | ||
* (queueMicrotask, Promises, or MutationObserver). | ||
*/ | ||
export function createQueueMicrotask(): (microtask: Microtask) => void { | ||
if (isBrowser && typeof queueMicrotask === 'function') | ||
return queueMicrotask.bind(window) | ||
else if (typeof Promise === 'function' && Promise.toString().includes('[native code]')) | ||
return createQueueMicrotaskViaPromises() | ||
else | ||
return createQueueMicrotaskViaMutationObserver() | ||
} |