Skip to content

Commit

Permalink
fix(watch): stop instance-bound watchers in post render queue
Browse files Browse the repository at this point in the history
so that changes triggered in beforeUnmount get correct value in callback

fix #1525
  • Loading branch information
yyx990803 committed Jul 7, 2020
1 parent 41db49d commit 58b0706
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
10 changes: 3 additions & 7 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
callWithErrorHandling,
callWithAsyncErrorHandling
} from './errorHandling'
import { onBeforeUnmount } from './apiLifecycle'
import { queuePostRenderEffect } from './renderer'
import { warn } from './warning'

Expand Down Expand Up @@ -134,7 +133,8 @@ export function watch<T = any>(
function doWatch(
source: WatchSource | WatchSource[] | WatchEffect,
cb: WatchCallback | null,
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ,
instance = currentInstance
): WatchStopHandle {
if (__DEV__ && !cb) {
if (immediate !== undefined) {
Expand All @@ -160,8 +160,6 @@ function doWatch(
)
}

const instance = currentInstance

let getter: () => any
if (isArray(source)) {
getter = () =>
Expand Down Expand Up @@ -316,9 +314,7 @@ export function instanceWatch(
const getter = isString(source)
? () => publicThis[source]
: source.bind(publicThis)
const stop = watch(getter, cb.bind(publicThis), options)
onBeforeUnmount(stop, this)
return stop
return doWatch(getter, cb.bind(publicThis), options, this)
}

function traverse(value: unknown, seen: Set<unknown> = new Set()) {
Expand Down
12 changes: 7 additions & 5 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1996,17 +1996,19 @@ function baseCreateRenderer(
if (bum) {
invokeArrayFns(bum)
}
if (effects) {
for (let i = 0; i < effects.length; i++) {
stop(effects[i])
}
}
// update may be null if a component is unmounted before its async
// setup has resolved.
if (update) {
stop(update)
unmount(subTree, instance, parentSuspense, doRemove)
}
if (effects) {
queuePostRenderEffect(() => {
for (let i = 0; i < effects.length; i++) {
stop(effects[i])
}
}, parentSuspense)
}
// unmounted hook
if (um) {
queuePostRenderEffect(um, parentSuspense)
Expand Down

0 comments on commit 58b0706

Please sign in to comment.