Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): Pass oldValue to triggerRefValue to satisfy interface DebuggerEventExtraInfo #8210

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
})
})
8 changes: 5 additions & 3 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function trackRefValue(ref: RefBase<any>) {
}
}

export function triggerRefValue(ref: RefBase<any>, newVal?: any) {
export function triggerRefValue(ref: RefBase<any>, newVal?: any, oldVal?: any) {
ref = toRaw(ref)
const dep = ref.dep
if (dep) {
Expand All @@ -61,7 +61,8 @@ export function triggerRefValue(ref: RefBase<any>, newVal?: any) {
target: ref,
type: TriggerOpTypes.SET,
key: 'value',
newValue: newVal
newValue: newVal,
oldValue: oldVal
})
} else {
triggerEffects(dep)
Expand Down Expand Up @@ -153,9 +154,10 @@ class RefImpl<T> {
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)
}
}
}
Expand Down