let queue = []; let has = {}; function flushSchedulerQueue() { for (let index = 0; index < queue.length; index++) { // 调用watcher的run方法 执行真正的更新操作 queue[index].run(); } // 执行完之后清空队列 queue = []; has = {}; }
// 实现异步队列机制 export function queueWatcher(watcher) { const id = watcher.id; // watcher去重 if (has[id] === undefined) { // 同步代码执行 把全部的watcher都放到队列里面去 queue.push(watcher); has[id] = true; // 进行异步调用 nextTick(flushSchedulerQueue); } }