【看得见的思考】nextTick 的实现
function queueFlush() { if (!isFlushing && !isFlushPending) { isFlushPending = true; } if (isFlushing) { return; } isFlushing = true; let job; if (DEV) { seen = seen || new Map(); } // Sort queue before flush. // This ensures that: // 1. Components are updated from parent to child. (because parent is always // created before the child so its render effect will have smaller // priority number) // 2. If a component is unmounted during a parent component's update, // its update can be skipped. // Jobs can never be null before flush starts, since they are only invalidated // during execution of another flushed job. queue.sort((a, b) => getId(a!) - getId(b!)) while ((job = queue.shift()) !== undefined) { if (job === null) { continue; } if (DEV) { checkRecursiveUpdates(seen!, job); } callWithErrorHandling(job, null, ErrorCodes.SCHEDULER); } flushPostFlushCbs(seen); isFlushing = false; // some postFlushCb queued jobs! // keep flushing until it drains. if (queue.length || postFlushCbs.length) { queueFlush(); } }
内容来源: cuixiaorui/mini-vue