File tree 2 files changed +30
-2
lines changed
2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -258,7 +258,7 @@ describe('reactivity/computed', () => {
258
258
] )
259
259
} )
260
260
261
- it ( 'debug: onTrigger' , ( ) => {
261
+ it ( 'debug: onTrigger (reactive) ' , ( ) => {
262
262
let events : DebuggerEvent [ ] = [ ]
263
263
const onTrigger = vi . fn ( ( e : DebuggerEvent ) => {
264
264
events . push ( e )
@@ -618,4 +618,29 @@ describe('reactivity/computed', () => {
618
618
expect ( serializeInner ( root ) ) . toBe ( 'Hello World World World World' )
619
619
expect ( COMPUTED_SIDE_EFFECT_WARN ) . toHaveBeenWarned ( )
620
620
} )
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
+ } )
621
646
} )
Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ export function triggerRefValue(
69
69
ref : RefBase < any > ,
70
70
dirtyLevel : DirtyLevels = DirtyLevels . Dirty ,
71
71
newVal ?: any ,
72
+ oldVal ?: any ,
72
73
) {
73
74
ref = toRaw ( ref )
74
75
const dep = ref . dep
@@ -82,6 +83,7 @@ export function triggerRefValue(
82
83
type : TriggerOpTypes . SET ,
83
84
key : 'value' ,
84
85
newValue : newVal ,
86
+ oldValue : oldVal ,
85
87
}
86
88
: void 0 ,
87
89
)
@@ -177,9 +179,10 @@ class RefImpl<T> {
177
179
this . __v_isShallow || isShallow ( newVal ) || isReadonly ( newVal )
178
180
newVal = useDirectValue ? newVal : toRaw ( newVal )
179
181
if ( hasChanged ( newVal , this . _rawValue ) ) {
182
+ const oldVal = this . _rawValue
180
183
this . _rawValue = newVal
181
184
this . _value = useDirectValue ? newVal : toReactive ( newVal )
182
- triggerRefValue ( this , DirtyLevels . Dirty , newVal )
185
+ triggerRefValue ( this , DirtyLevels . Dirty , newVal , oldVal )
183
186
}
184
187
}
185
188
}
You can’t perform that action at this time.
0 commit comments