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(watch): should not leak this.proxy to setup() #3603

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 17 additions & 1 deletion packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ describe('api: watch', () => {
expect(instance!.effects![0].active).toBe(false)
})

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

Expand All @@ -878,6 +878,22 @@ describe('api: watch', () => {
expect(source).toHaveBeenCalledWith(instance)
})

test('should not leak `this.proxy` to setup()', () => {
const source = jest.fn()

const Comp = defineComponent({
render() {},
setup() {
watch(source, () => {})
}
})

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

expect(source).toBeCalledWith(undefined)
})

// #2728
test('pre watcher callbacks should not track dependencies', async () => {
const a = ref(0)
Expand Down
6 changes: 2 additions & 4 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ function doWatch(
} else if (isReactive(s)) {
return traverse(s)
} else if (isFunction(s)) {
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
instance && (instance.proxy as any)
])
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
} else {
__DEV__ && warnInvalidSource(s)
}
Expand All @@ -193,7 +191,7 @@ function doWatch(
// getter with cb
getter = () =>
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
instance && (instance.proxy as any)
arguments[3] && arguments[3].proxy
])
} else {
// no cb -> simple effect
Expand Down