From e065e6e1213ef96af83f5dec85f40cc05bca44e7 Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Tue, 2 May 2023 12:02:46 +0800 Subject: [PATCH 1/3] fix: Add old value to triggerEffects' dependency --- packages/reactivity/src/ref.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index a5224d7f281..a329b023f3b 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -52,7 +52,7 @@ export function trackRefValue(ref: RefBase) { } } -export function triggerRefValue(ref: RefBase, newVal?: any) { +export function triggerRefValue(ref: RefBase, newVal?: any, oldVal?: any) { ref = toRaw(ref) const dep = ref.dep if (dep) { @@ -61,7 +61,8 @@ export function triggerRefValue(ref: RefBase, newVal?: any) { target: ref, type: TriggerOpTypes.SET, key: 'value', - newValue: newVal + newValue: newVal, + oldValue: oldVal }) } else { triggerEffects(dep) @@ -153,9 +154,10 @@ class RefImpl { this.__v_isShallow || isShallow(newVal) || isReadonly(newVal) newVal = useDirectValue ? newVal : toRaw(newVal) if (hasChanged(newVal, this._rawValue)) { + const oldVal = this._rawValue this._rawValue = newVal this._value = useDirectValue ? newVal : toReactive(newVal) - triggerRefValue(this, newVal) + triggerRefValue(this, newVal, oldVal) } } } From 428c680e5065e6da19e91cef2265d56a50dd13bb Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Tue, 2 May 2023 12:03:19 +0800 Subject: [PATCH 2/3] chore: Add test --- .../reactivity/__tests__/computed.spec.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/reactivity/__tests__/computed.spec.ts b/packages/reactivity/__tests__/computed.spec.ts index 51157944355..8fd5d9f9004 100644 --- a/packages/reactivity/__tests__/computed.spec.ts +++ b/packages/reactivity/__tests__/computed.spec.ts @@ -255,7 +255,7 @@ describe('reactivity/computed', () => { ]) }) - it('debug: onTrigger', () => { + it('debug: onTrigger (reactive)', () => { let events: DebuggerEvent[] = [] const onTrigger = vi.fn((e: DebuggerEvent) => { events.push(e) @@ -290,4 +290,29 @@ describe('reactivity/computed', () => { oldValue: 2 }) }) + + it('debug: onTrigger (ref)', () => { + let events: DebuggerEvent[] = [] + const onTrigger = vi.fn((e: DebuggerEvent) => { + events.push(e) + }) + const obj = ref(1) + const c = computed(() => obj.value, { onTrigger }) + + // computed won't trigger compute until accessed + c.value + + obj.value++ + + expect(c.value).toBe(2) + expect(onTrigger).toHaveBeenCalledTimes(1) + expect(events[0]).toEqual({ + effect: c.effect, + target: toRaw(obj), + type: TriggerOpTypes.SET, + key: 'value', + oldValue: 1, + newValue: 2 + }) + }) }) From 7ee0c2f50053f9a8acea146362025a11bdcbb4f6 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:13:31 +0000 Subject: [PATCH 3/3] [autofix.ci] apply automated fixes --- packages/reactivity/__tests__/computed.spec.ts | 2 +- packages/reactivity/src/ref.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/__tests__/computed.spec.ts b/packages/reactivity/__tests__/computed.spec.ts index cc0ad1e535b..10c09109fdb 100644 --- a/packages/reactivity/__tests__/computed.spec.ts +++ b/packages/reactivity/__tests__/computed.spec.ts @@ -640,7 +640,7 @@ describe('reactivity/computed', () => { type: TriggerOpTypes.SET, key: 'value', oldValue: 1, - newValue: 2 + newValue: 2, }) }) }) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 74d65bc2564..e47b8aa5582 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -69,7 +69,7 @@ export function triggerRefValue( ref: RefBase, dirtyLevel: DirtyLevels = DirtyLevels.Dirty, newVal?: any, - oldVal?: any + oldVal?: any, ) { ref = toRaw(ref) const dep = ref.dep @@ -83,7 +83,7 @@ export function triggerRefValue( type: TriggerOpTypes.SET, key: 'value', newValue: newVal, - oldValue: oldVal + oldValue: oldVal, } : void 0, )