Skip to content

Commit 3b0a56a

Browse files
authoredJun 4, 2024··
fix(reactivity): pass oldValue in debug info when triggering refs (#8210)
fix vuejs/pinia#2061
1 parent be1e9bf commit 3b0a56a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed
 

‎packages/reactivity/__tests__/computed.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ describe('reactivity/computed', () => {
258258
])
259259
})
260260

261-
it('debug: onTrigger', () => {
261+
it('debug: onTrigger (reactive)', () => {
262262
let events: DebuggerEvent[] = []
263263
const onTrigger = vi.fn((e: DebuggerEvent) => {
264264
events.push(e)
@@ -618,4 +618,29 @@ describe('reactivity/computed', () => {
618618
expect(serializeInner(root)).toBe('Hello World World World World')
619619
expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
620620
})
621+
622+
it('debug: onTrigger (ref)', () => {
623+
let events: DebuggerEvent[] = []
624+
const onTrigger = vi.fn((e: DebuggerEvent) => {
625+
events.push(e)
626+
})
627+
const obj = ref(1)
628+
const c = computed(() => obj.value, { onTrigger })
629+
630+
// computed won't trigger compute until accessed
631+
c.value
632+
633+
obj.value++
634+
635+
expect(c.value).toBe(2)
636+
expect(onTrigger).toHaveBeenCalledTimes(1)
637+
expect(events[0]).toEqual({
638+
effect: c.effect,
639+
target: toRaw(obj),
640+
type: TriggerOpTypes.SET,
641+
key: 'value',
642+
oldValue: 1,
643+
newValue: 2,
644+
})
645+
})
621646
})

‎packages/reactivity/src/ref.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export function triggerRefValue(
6969
ref: RefBase<any>,
7070
dirtyLevel: DirtyLevels = DirtyLevels.Dirty,
7171
newVal?: any,
72+
oldVal?: any,
7273
) {
7374
ref = toRaw(ref)
7475
const dep = ref.dep
@@ -82,6 +83,7 @@ export function triggerRefValue(
8283
type: TriggerOpTypes.SET,
8384
key: 'value',
8485
newValue: newVal,
86+
oldValue: oldVal,
8587
}
8688
: void 0,
8789
)
@@ -177,9 +179,10 @@ class RefImpl<T> {
177179
this.__v_isShallow || isShallow(newVal) || isReadonly(newVal)
178180
newVal = useDirectValue ? newVal : toRaw(newVal)
179181
if (hasChanged(newVal, this._rawValue)) {
182+
const oldVal = this._rawValue
180183
this._rawValue = newVal
181184
this._value = useDirectValue ? newVal : toReactive(newVal)
182-
triggerRefValue(this, DirtyLevels.Dirty, newVal)
185+
triggerRefValue(this, DirtyLevels.Dirty, newVal, oldVal)
183186
}
184187
}
185188
}

0 commit comments

Comments
 (0)
Please sign in to comment.