])
})
- it('debug: onTrigger', () => {
+ it('debug: onTrigger (reactive)', () => {
let events: DebuggerEvent[] = []
const onTrigger = vi.fn((e: DebuggerEvent) => {
events.push(e)
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,
+ })
+ })
})
ref: RefBase<any>,
dirtyLevel: DirtyLevels = DirtyLevels.Dirty,
newVal?: any,
+ oldVal?: any,
) {
ref = toRaw(ref)
const dep = ref.dep
type: TriggerOpTypes.SET,
key: 'value',
newValue: newVal,
+ oldValue: oldVal,
}
: void 0,
)
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)
}
}
}