-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from rwjblue/autorun-microtask
Use microtask queue to flush autoruns...
- Loading branch information
Showing
9 changed files
with
280 additions
and
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export interface IPlatform { | ||
setTimeout(fn: Function, ms: number): any; | ||
clearTimeout(id: any): void; | ||
next(): any; | ||
clearNext(timerId: any): void; | ||
now(): number; | ||
} | ||
|
||
const SET_TIMEOUT = setTimeout; | ||
const NOOP = () => {}; | ||
|
||
export function buildPlatform(flush: () => void): IPlatform { | ||
let next; | ||
let clearNext = NOOP; | ||
|
||
if (typeof MutationObserver === 'function') { | ||
let iterations = 0; | ||
let observer = new MutationObserver(flush); | ||
let node = document.createTextNode(''); | ||
observer.observe(node, { characterData: true }); | ||
|
||
next = () => { | ||
iterations = ++iterations % 2; | ||
node.data = '' + iterations; | ||
return iterations; | ||
}; | ||
|
||
} else if (typeof Promise === 'function') { | ||
const autorunPromise = Promise.resolve(); | ||
next = () => autorunPromise.then(flush); | ||
|
||
} else { | ||
next = () => SET_TIMEOUT(flush, 0); | ||
} | ||
|
||
return { | ||
setTimeout(fn, ms) { | ||
return SET_TIMEOUT(fn, ms); | ||
}, | ||
|
||
clearTimeout(timerId: number) { | ||
return clearTimeout(timerId); | ||
}, | ||
|
||
now() { | ||
return Date.now(); | ||
}, | ||
|
||
next, | ||
clearNext, | ||
}; | ||
} |
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
Oops, something went wrong.