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(runtime-core): $watch effects can register effects with wrong instance (fix: #2381) #2495

Merged
merged 1 commit into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 61 additions & 3 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ import {
computed,
nextTick,
ref,
h
defineComponent,
getCurrentInstance,
ComponentInternalInstance,
ComponentPublicInstance
} from '../src/index'
import { render, nodeOps, serializeInner, TestElement } from '@vue/runtime-test'
import {
render,
nodeOps,
serializeInner,
TestElement,
h
} from '@vue/runtime-test'
import {
ITERATE_KEY,
DebuggerEvent,
TrackOpTypes,
TriggerOpTypes,
triggerRef,
shallowRef
shallowRef,
Ref
} from '@vue/reactivity'

// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
Expand Down Expand Up @@ -799,4 +809,52 @@ describe('api: watch', () => {
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})

// https://github.com/vuejs/vue-next/issues/2381
test('$watch should always register its effects with itw own instance', async () => {
let instance: ComponentInternalInstance | null
let _show: Ref<boolean>

const Child = defineComponent({
render: () => h('div'),
mounted() {
instance = getCurrentInstance()
},
unmounted() {}
})

const Comp = defineComponent({
setup() {
const comp = ref<ComponentPublicInstance | undefined>()
const show = ref(true)
_show = show
return { comp, show }
},
render() {
return this.show
? h(Child, {
ref: vm => void (this.comp = vm as ComponentPublicInstance)
})
: null
},
mounted() {
// this call runs while Comp is currentInstance, but
// the effect for this `$watch` should nontheless be registered with Child
this.comp!.$watch(() => this.show, () => void 0)
}
})

render(h(Comp), nodeOps.createElement('div'))

expect(instance!).toBeDefined()
expect(instance!.effects).toBeInstanceOf(Array)
expect(instance!.effects!.length).toBe(1)

_show!.value = false

await nextTick()
await nextTick()

expect(instance!.effects![0].active).toBe(false)
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function doWatch(
scheduler
})

recordInstanceBoundEffect(runner)
recordInstanceBoundEffect(runner, instance)

// initial run
if (cb) {
Expand Down
9 changes: 6 additions & 3 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,12 @@ function createSetupContext(instance: ComponentInternalInstance): SetupContext {

// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordInstanceBoundEffect(effect: ReactiveEffect) {
if (currentInstance) {
;(currentInstance.effects || (currentInstance.effects = [])).push(effect)
export function recordInstanceBoundEffect(
effect: ReactiveEffect,
instance = currentInstance
) {
if (instance) {
;(instance.effects || (instance.effects = [])).push(effect)
}
}

Expand Down