Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

《批量异步更新策略及 nextTick 原理》.js 中的一些问题 #8

Open
zhanyuzhang opened this issue Apr 14, 2020 · 1 comment

Comments

@zhanyuzhang
Copy link

flushSchedulerQueue() 函数在遍历执行所有的 watcher 之后,应该要清空当前的 watcher 队列才对的,即 queue.length = 0,完整代码如下:

function flushSchedulerQueue () {
    let watcher, id;

    for (index = 0; index < queue.length; index++) {
        watcher = queue[index]
        id = watcher.id;
        has[id] = null;
        watcher.run();
    }

    waiting  = false;
    queue.length = 0;
}

如果不清空队列的话,在以后的 nextTick() 中就会重复执行之前的旧的 watcher ,比如,测试代码中在一秒后再执行 watch1.update() :

(function () {
    let watch1 = new Watcher();
    let watch2 = new Watcher();

    watch1.update();
    watch1.update();
    watch2.update();

    setTimeout(() => {
      watch1.update();
    }, 1000)
})();

会发现,在一秒之后,只更新了 watcher1, 但是, watcher2 也会执行。

@zhanyuzhang zhanyuzhang changed the title 《批量异步更新策略及 nextTick 原理》.js 中的一些小问题 《批量异步更新策略及 nextTick 原理》.js 中的一些问题 Apr 14, 2020
@william-xue
Copy link

蛮好 有收获

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants