断开,笑了笑flushSchedulerQueue后,他们已经开始说nextTick:
先扫一看时序:
function nextTick (cb, ctx) { var_resolve; callbacks.push(function () { if (cb) { try { cb.call(ctx); } catch (e) { handleError(e, ctx, nextTick); } }else if (_resolve) { _resolve(ctx); } }); if (!pending) { pending = true; timerFunc(); }// $flow-disable-line if (!cb && typeof Promise !== undefined) { return new Promise(function (resolve) { _resolve = resolve; }) } }那个方式前述也是在Vue.prototype.$nextTick里初始化的方式,特别注意这儿他将传进来的反弹表达式用两个非官方表达式包覆了呵呵,接着放进了callbacks里,那个非官方表达式已经开始继续执行时,他们用nextTick传进去的表达式才会继续执行。callbacks没啥好说的,两个字符串,接着pending置为true,初始化timerFunc:
if (typeof Promise !== undefined && isNative(Promise)) { var p = Promise.resolve(); timerFunc =function () { p.then(flushCallbacks); // In problematic UIWebViews, Promise.then doesnt completely break, but // it can get stuck in a weird state where callbacks are pushed into the // microtask queue but the queue isnt 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); } }; isUsingMicroTask = true; } else if (!isIE && typeofMutationObserver !==undefined && ( isNative(MutationObserver) || // PhantomJS and iOS 7.xMutationObserver.toString() ===[object MutationObserverConstructor] )) { // Use MutationObserver where native Promise is not available, // e.g. PhantomJS, iOS7, Android 4.4 // (#6466 MutationObserver is unreliable in IE11) var counter = 1; var observer = new MutationObserver(flushCallbacks); var textNode = document.createTextNode(String(counter)); observer.observe(textNode, { characterData: true }); timerFunc = function () { counter = (counter +1) % 2; textNode.data = String(counter); }; isUsingMicroTask = true; } else if (typeof setImmediate !== undefined && isNative(setImmediate)) { // Fallback to setImmediate. // Techinically it leverages the (macro) task queue, // but it is still a better choice than setTimeout. timerFunc = function () { setImmediate(flushCallbacks); }; } else { // Fallback to setTimeout. timerFunc = function () { setTimeout(flushCallbacks, 0); }; }timerFunc 是这么定义得,不必细看看就一句话,有microTask就用microTask,否则就用macroTask,接着他们特别注意到,前述在timerFunc里是传进去了两个叫flushCallbacks的,那个也没啥好说的,因为简单:
function flushCallbacks () { pending = false; var copies = callbacks.slice(0); callbacks.length = 0; for(var i = 0; i < copies.length; i++) { copies[i](); } }就是用来继续执行callbacks里push进去的两个个非官方表达式的。而这两个个反弹,正是两个个watcher update的地方,也是vue视图预览的地方。
终。