Skip to content

Commit

Permalink
fix(runtime-core): instanceWatch should pass this.proxy to source a…
Browse files Browse the repository at this point in the history
…s the first argument (#2753)
  • Loading branch information
edison1105 authored Feb 9, 2021
1 parent bd1240c commit ec8fd10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 21 additions & 1 deletion packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
nodeOps,
serializeInner,
TestElement,
h
h,
createApp
} from '@vue/runtime-test'
import {
ITERATE_KEY,
Expand Down Expand Up @@ -857,4 +858,23 @@ describe('api: watch', () => {

expect(instance!.effects![0].active).toBe(false)
})

test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => {
let instance: any
const source = jest.fn()

const Comp = defineComponent({
render() {},
created(this: any) {
instance = this
this.$watch(source, function() {})
}
})

const root = nodeOps.createElement('div')
createApp(Comp).mount(root)

expect(instance).toBeDefined()
expect(source).toHaveBeenCalledWith(instance)
})
})
8 changes: 6 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ function doWatch(
} else if (isReactive(s)) {
return traverse(s)
} else if (isFunction(s)) {
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
instance && (instance.proxy as any)
])
} else {
__DEV__ && warnInvalidSource(s)
}
Expand All @@ -190,7 +192,9 @@ function doWatch(
if (cb) {
// getter with cb
getter = () =>
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER)
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
instance && (instance.proxy as any)
])
} else {
// no cb -> simple effect
getter = () => {
Expand Down

0 comments on commit ec8fd10

Please sign in to comment.