vuejs / composition-api

Composition API plugin for Vue 2
https://composition-api.vuejs.org/
MIT License
4.19k stars 342 forks source link

watchEffect 的 onCleanUp 在 sync 模式下没有被调用 #944

Closed ivan-94 closed 2 years ago

ivan-94 commented 2 years ago

复现代码:

const count = ref(0)

watchEffect((onCleanup) => {
  const value = unref(count)
  console.log(value)

  onCleanup(() => {
     console.log('clean', value)
  })
}, {flush: 'sync'})

count.value++ // 期望 onCleanup 被同步执行

行为和 Vue 不一致

ivan-94 commented 2 years ago

原因是 Watcher 在 update 时,如果是 sync, 就会跳过 queueWatcher:

  update() {
    /* istanbul ignore else */
    if (this.lazy) {
      this.dirty = true
    } else if (this.sync) {
      this.run()
    } else {
      queueWatcher(this)
    }
  }

link

github-actions[bot] commented 2 years ago

Stale issue message