From d69d3bf76540c83cf3a1d39ecf45efc3f82510a8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 18 Oct 2019 15:31:28 -0400 Subject: [PATCH] fix(reactivity): revert to Reflect.get and add test cases --- packages/reactivity/__tests__/effect.spec.ts | 30 ++++++++++++++++++++ packages/reactivity/src/baseHandlers.ts | 5 ++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/__tests__/effect.spec.ts b/packages/reactivity/__tests__/effect.spec.ts index 340bf1093..32a7fbdfe 100644 --- a/packages/reactivity/__tests__/effect.spec.ts +++ b/packages/reactivity/__tests__/effect.spec.ts @@ -249,6 +249,36 @@ describe('reactivity/effect', () => { expect(dummy).toBe(newFunc) }) + it('should observe chained getters relying on this', () => { + const obj = reactive({ + a: 1, + get b() { + return this.a + } + }) + + let dummy + effect(() => (dummy = obj.b)) + expect(dummy).toBe(1) + obj.a++ + expect(dummy).toBe(2) + }) + + it('should observe methods relying on this', () => { + const obj = reactive({ + a: 1, + b() { + return this.a + } + }) + + let dummy + effect(() => (dummy = obj.b())) + expect(dummy).toBe(1) + obj.a++ + expect(dummy).toBe(2) + }) + it('should not observe set operations without a value change', () => { let hasDummy, getDummy const obj = reactive({ prop: 'value' }) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 4ebdb0a33..0043af73f 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -12,9 +12,8 @@ const builtInSymbols = new Set( ) function createGetter(isReadonly: boolean) { - return function get(target: any, key: string | symbol) { - // not using Reflect.get here for perf reasons - const res = target[key] + return function get(target: any, key: string | symbol, receiver: any) { + const res = Reflect.get(target, key, receiver) if (isSymbol(key) && builtInSymbols.has(key)) { return res }