-
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.
Use microtask queue to flush autoruns.
* Replace the private `_platform` option, with a new `_buildPlatform` * Add interfaces for the constructor args for the `Backburner` class. * Move the main implementation of `Backburner.prototype.end` into a private method (`_end`) that is aware of if the end is from an autorun or manual run... * Extract steps to schedule an autorun out into stand alone private method (`_scheduleAutorun`) * Leverage microtask queue (via either `promise.then(...)` or `MutationObserver`) to schedule an autorun * Leverage microtask queue to advance from one queue to the next
- 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.