-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf8c09b
commit 66dc041
Showing
2 changed files
with
40 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 维护一个事件队列,等同步事件执行完毕后清空callbacks队列 | ||
// Promise(微任务) MutationObserver(微任务) setImmediate(宏任务) setTimeout(宏任务) | ||
// 在异步队列中,微任务优先级更高,所以做了一个优化。 | ||
// Vue.$nextTick(cb) | ||
|
||
const callbacks = [] | ||
|
||
function flushCallbacks() { | ||
callbacks.forEach(cb => cb()) | ||
} | ||
|
||
export default function nextTick(cb) { | ||
callbacks.push(cb) | ||
|
||
const timerFunc = () => { | ||
flushCallbacks() | ||
} | ||
|
||
if (Promise) { | ||
return Promise.resolve().then(flushCallbacks) | ||
} | ||
|
||
if (MutationObserver) { | ||
const observer = new MutationObserver(timerFunc) | ||
const textNode = document.createTextNode('1') | ||
observer.observe(textNode, { characterData: true }) | ||
textNode.textContent = '2' | ||
return | ||
} | ||
|
||
if (setImmediate) { | ||
return setImmediate(timerFunc) | ||
} | ||
|
||
setTimeout(timerFunc, 0) | ||
} |
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