Last active
March 11, 2019 03:11
-
-
Save fritx/f9b9b2bb2e29b993d2088c5a93db55b7 to your computer and use it in GitHub Desktop.
Vue.nextTick 2.4.4
This file contains hidden or 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
// ... | |
/** | |
* Defer a task to execute it asynchronously. | |
*/ | |
export const nextTick = (function () { | |
const callbacks = [] | |
let pending = false | |
let timerFunc | |
function nextTickHandler () { | |
pending = false | |
const copies = callbacks.slice(0) | |
callbacks.length = 0 | |
for (let i = 0; i < copies.length; i++) { | |
copies[i]() | |
} | |
} | |
// the nextTick behavior leverages the microtask queue, which can be accessed | |
// via either native Promise.then or MutationObserver. | |
// MutationObserver has wider support, however it is seriously bugged in | |
// UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It | |
// completely stops working after triggering a few times... so, if native | |
// Promise is available, we will use it: | |
/* istanbul ignore if */ | |
if (typeof Promise !== 'undefined' && isNative(Promise)) { | |
var p = Promise.resolve() | |
var logError = err => { console.error(err) } | |
timerFunc = () => { | |
p.then(nextTickHandler).catch(logError) | |
// in problematic UIWebViews, Promise.then doesn't completely break, but | |
// it can get stuck in a weird state where callbacks are pushed into the | |
// microtask queue but the queue isn't being flushed, until the browser | |
// needs to do some other work, e.g. handle a timer. Therefore we can | |
// "force" the microtask queue to be flushed by adding an empty timer. | |
if (isIOS) setTimeout(noop) | |
} | |
} else if (!isIE && typeof MutationObserver !== 'undefined' && ( | |
isNative(MutationObserver) || | |
// PhantomJS and iOS 7.x | |
MutationObserver.toString() === '[object MutationObserverConstructor]' | |
)) { | |
// use MutationObserver where native Promise is not available, | |
// e.g. PhantomJS, iOS7, Android 4.4 | |
var counter = 1 | |
var observer = new MutationObserver(nextTickHandler) | |
var textNode = document.createTextNode(String(counter)) | |
observer.observe(textNode, { | |
characterData: true | |
}) | |
timerFunc = () => { | |
counter = (counter + 1) % 2 | |
textNode.data = String(counter) | |
} | |
} else { | |
// fallback to setTimeout | |
/* istanbul ignore next */ | |
timerFunc = () => { | |
setTimeout(nextTickHandler, 0) | |
} | |
} | |
return function queueNextTick (cb?: Function, ctx?: Object) { | |
let _resolve | |
callbacks.push(() => { | |
if (cb) { | |
try { | |
cb.call(ctx) | |
} catch (e) { | |
handleError(e, ctx, 'nextTick') | |
} | |
} else if (_resolve) { | |
_resolve(ctx) | |
} | |
}) | |
if (!pending) { | |
pending = true | |
timerFunc() | |
} | |
if (!cb && typeof Promise !== 'undefined') { | |
return new Promise((resolve, reject) => { | |
_resolve = resolve | |
}) | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment