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

fix(reactivity): ensure watch(Effect) can run independent of unmounted instance if created in a detatched effectScope (fix #7319) #7330

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,4 +1150,37 @@ describe('api: watch', () => {
// own update effect
expect(instance!.scope.effects.length).toBe(1)
})

test('watchEffect should keep running if created in a detatched scope', async () => {
const trigger = ref(0)
let count = 0
const Comp = {
setup() {
effectScope(true).run(() => {
watchEffect(
() => {
trigger.value
count++
},
)
watch(
trigger,
() => count++
)
})
return () => ''
}
}
const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(count).toBe(1) // only watchEffect as ran so far
trigger.value++
await nextTick()
expect(count).toBe(3) // both watchers run while component is mounted
render(null, root)
await nextTick()
trigger.value++
await nextTick()
expect(count).toBe(5) // both watchers run again event though component has been unmounted
})
})
6 changes: 4 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
isReactive,
ReactiveFlags,
EffectScheduler,
DebuggerOptions
DebuggerOptions,
getCurrentScope,
} from '@vue/reactivity'
import { SchedulerJob, queueJob } from './scheduler'
import {
Expand Down Expand Up @@ -197,7 +198,8 @@ function doWatch(
)
}

const instance = currentInstance
const instance = getCurrentScope() === currentInstance?.scope ? currentInstance : null
// const instance = currentInstance
let getter: () => any
let forceTrigger = false
let isMultiSource = false
Expand Down