mirror of https://github.com/vuejs/core.git
fix(reactivity): ensure watcher with once: true are properly removed from effect scope (#11665)
This commit is contained in:
parent
f2ea25dc54
commit
fbc0c42bcf
|
@ -206,18 +206,26 @@ export function watch(
|
||||||
getter = () => traverse(baseGetter(), depth)
|
getter = () => traverse(baseGetter(), depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const scope = getCurrentScope()
|
||||||
|
const watchHandle: WatchHandle = () => {
|
||||||
|
effect.stop()
|
||||||
|
if (scope) {
|
||||||
|
remove(scope.effects, effect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (once) {
|
if (once) {
|
||||||
if (cb) {
|
if (cb) {
|
||||||
const _cb = cb
|
const _cb = cb
|
||||||
cb = (...args) => {
|
cb = (...args) => {
|
||||||
_cb(...args)
|
_cb(...args)
|
||||||
effect.stop()
|
watchHandle()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const _getter = getter
|
const _getter = getter
|
||||||
getter = () => {
|
getter = () => {
|
||||||
_getter()
|
_getter()
|
||||||
effect.stop()
|
watchHandle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,14 +325,6 @@ export function watch(
|
||||||
effect.run()
|
effect.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
const scope = getCurrentScope()
|
|
||||||
const watchHandle: WatchHandle = () => {
|
|
||||||
effect.stop()
|
|
||||||
if (scope) {
|
|
||||||
remove(scope.effects, effect)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watchHandle.pause = effect.pause.bind(effect)
|
watchHandle.pause = effect.pause.bind(effect)
|
||||||
watchHandle.resume = effect.resume.bind(effect)
|
watchHandle.resume = effect.resume.bind(effect)
|
||||||
watchHandle.stop = watchHandle
|
watchHandle.stop = watchHandle
|
||||||
|
|
|
@ -1771,6 +1771,11 @@ describe('api: watch', () => {
|
||||||
expect(scope.effects.length).toBe(1)
|
expect(scope.effects.length).toBe(1)
|
||||||
unwatch!()
|
unwatch!()
|
||||||
expect(scope.effects.length).toBe(0)
|
expect(scope.effects.length).toBe(0)
|
||||||
|
|
||||||
|
scope.run(() => {
|
||||||
|
watch(num, () => {}, { once: true, immediate: true })
|
||||||
|
})
|
||||||
|
expect(scope.effects.length).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
// simplified case of VueUse syncRef
|
// simplified case of VueUse syncRef
|
||||||
|
|
Loading…
Reference in New Issue