From: Fan Pei Date: Tue, 4 Jun 2024 15:25:37 +0000 (+0900) Subject: fix(reactivity): pass oldValue in debug info when triggering refs (#8210) X-Git-Tag: v3.4.28~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3b0a56a9c4d162ec3bd725a4f2dfd776b045e727;p=thirdparty%2Fvuejs%2Fcore.git fix(reactivity): pass oldValue in debug info when triggering refs (#8210) fix vuejs/pinia#2061 --- diff --git a/packages/reactivity/__tests__/computed.spec.ts b/packages/reactivity/__tests__/computed.spec.ts index c9f47720ed..10c09109fd 100644 --- a/packages/reactivity/__tests__/computed.spec.ts +++ b/packages/reactivity/__tests__/computed.spec.ts @@ -258,7 +258,7 @@ describe('reactivity/computed', () => { ]) }) - it('debug: onTrigger', () => { + it('debug: onTrigger (reactive)', () => { let events: DebuggerEvent[] = [] const onTrigger = vi.fn((e: DebuggerEvent) => { events.push(e) @@ -618,4 +618,29 @@ describe('reactivity/computed', () => { expect(serializeInner(root)).toBe('Hello World World World World') expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned() }) + + 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, + }) + }) }) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 99170d154b..e47b8aa558 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -69,6 +69,7 @@ export function triggerRefValue( ref: RefBase, dirtyLevel: DirtyLevels = DirtyLevels.Dirty, newVal?: any, + oldVal?: any, ) { ref = toRaw(ref) const dep = ref.dep @@ -82,6 +83,7 @@ export function triggerRefValue( type: TriggerOpTypes.SET, key: 'value', newValue: newVal, + oldValue: oldVal, } : void 0, ) @@ -177,9 +179,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, DirtyLevels.Dirty, newVal) + triggerRefValue(this, DirtyLevels.Dirty, newVal, oldVal) } } }