File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed
Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff 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} )
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments