From 9def43e3be6f807a751d72d5fe661e76adcc3104 Mon Sep 17 00:00:00 2001 From: liaoliao666 <1076988944@qq.com> Date: Tue, 29 Dec 2020 15:10:04 +0800 Subject: [PATCH 1/5] fix(baseHandlers): createSetter will called two times when set the same proxy value --- packages/reactivity/src/baseHandlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 5f57a3c9f90..409fbc709b5 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -153,7 +153,7 @@ function createSetter(shallow = false) { if (target === toRaw(receiver)) { if (!hadKey) { trigger(target, TriggerOpTypes.ADD, key, value) - } else if (hasChanged(value, oldValue)) { + } else if (hasChanged(value, toRaw(oldValue))) { trigger(target, TriggerOpTypes.SET, key, value, oldValue) } } From 5a113f5ef1739951588808a7136620f418976773 Mon Sep 17 00:00:00 2001 From: liaoliao666 <1076988944@qq.com> Date: Tue, 29 Dec 2020 23:05:59 +0800 Subject: [PATCH 2/5] feat(effect.spec): new test case --- packages/reactivity/__tests__/effect.spec.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/reactivity/__tests__/effect.spec.ts b/packages/reactivity/__tests__/effect.spec.ts index 6be4d8e25dd..40049d987cc 100644 --- a/packages/reactivity/__tests__/effect.spec.ts +++ b/packages/reactivity/__tests__/effect.spec.ts @@ -811,4 +811,15 @@ describe('reactivity/effect', () => { expect(dummy).toBe(0) expect(record).toBeUndefined() }) + + it('should trigger once effect when set the equal proxy', () => { + const obj = reactive({ foo: 1 }) + const observed: any = reactive({ obj }) + const fnSpy = jest.fn(() => observed.obj) + + effect(fnSpy) + + observed.obj = obj + expect(fnSpy).toHaveBeenCalledTimes(1) + }) }) From 3e40eeabab3ff0084681e6b1f81b0a0f65a0bb6a Mon Sep 17 00:00:00 2001 From: liaoliao666 <1076988944@qq.com> Date: Tue, 29 Dec 2020 23:25:36 +0800 Subject: [PATCH 3/5] fix(baseHandlers): more edge cases --- packages/reactivity/__tests__/effect.spec.ts | 12 +++++++++++- packages/reactivity/src/baseHandlers.ts | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/__tests__/effect.spec.ts b/packages/reactivity/__tests__/effect.spec.ts index 40049d987cc..34a0976030a 100644 --- a/packages/reactivity/__tests__/effect.spec.ts +++ b/packages/reactivity/__tests__/effect.spec.ts @@ -6,7 +6,8 @@ import { TrackOpTypes, TriggerOpTypes, DebuggerEvent, - markRaw + markRaw, + shallowReactive } from '../src/index' import { ITERATE_KEY } from '../src/effect' @@ -821,5 +822,14 @@ describe('reactivity/effect', () => { observed.obj = obj expect(fnSpy).toHaveBeenCalledTimes(1) + + const obj2 = reactive({ foo: 1 }) + const observed2: any = shallowReactive({ obj2 }) + const fnSpy2 = jest.fn(() => observed2.obj2) + + effect(fnSpy2) + + observed2.obj2 = obj2 + expect(fnSpy2).toHaveBeenCalledTimes(1) }) }) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 409fbc709b5..4e18b89cf50 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -134,8 +134,9 @@ function createSetter(shallow = false) { receiver: object ): boolean { const oldValue = (target as any)[key] + value = toRaw(value) + if (!shallow) { - value = toRaw(value) if (!isArray(target) && isRef(oldValue) && !isRef(value)) { oldValue.value = value return true From 514bd17520e6dfc5024a556cf4ccebc0a84fd690 Mon Sep 17 00:00:00 2001 From: liaoliao666 <1076988944@qq.com> Date: Tue, 29 Dec 2020 23:33:30 +0800 Subject: [PATCH 4/5] fix(baseHandlers): should keep reactive properties reactive --- packages/reactivity/src/baseHandlers.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 4e18b89cf50..26feda4ed51 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -134,9 +134,8 @@ function createSetter(shallow = false) { receiver: object ): boolean { const oldValue = (target as any)[key] - value = toRaw(value) - if (!shallow) { + value = toRaw(value) if (!isArray(target) && isRef(oldValue) && !isRef(value)) { oldValue.value = value return true @@ -154,7 +153,7 @@ function createSetter(shallow = false) { if (target === toRaw(receiver)) { if (!hadKey) { trigger(target, TriggerOpTypes.ADD, key, value) - } else if (hasChanged(value, toRaw(oldValue))) { + } else if (hasChanged(toRaw(value), toRaw(oldValue))) { trigger(target, TriggerOpTypes.SET, key, value, oldValue) } } From 0e7a70c4accb4164b2713da0a17d654e37a3a445 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 29 Mar 2021 17:46:11 -0400 Subject: [PATCH 5/5] Update baseHandlers.ts --- packages/reactivity/src/baseHandlers.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 26feda4ed51..9f7e34912e9 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -133,9 +133,10 @@ function createSetter(shallow = false) { value: unknown, receiver: object ): boolean { - const oldValue = (target as any)[key] + let oldValue = (target as any)[key] if (!shallow) { value = toRaw(value) + oldValue = toRaw(oldValue) if (!isArray(target) && isRef(oldValue) && !isRef(value)) { oldValue.value = value return true @@ -153,7 +154,7 @@ function createSetter(shallow = false) { if (target === toRaw(receiver)) { if (!hadKey) { trigger(target, TriggerOpTypes.ADD, key, value) - } else if (hasChanged(toRaw(value), toRaw(oldValue))) { + } else if (hasChanged(value, oldValue)) { trigger(target, TriggerOpTypes.SET, key, value, oldValue) } }