File tree 3 files changed +34
-4
lines changed
3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -377,3 +377,7 @@ function triggerEffect(
377
377
}
378
378
}
379
379
}
380
+
381
+ export function getDepFromReactive ( object : any , key : string | number | symbol ) {
382
+ return targetMap . get ( object ) ?. get ( key )
383
+ }
Original file line number Diff line number Diff line change 1
1
import {
2
2
activeEffect ,
3
+ getDepFromReactive ,
3
4
shouldTrack ,
4
5
trackEffects ,
5
6
triggerEffects
@@ -53,16 +54,17 @@ export function trackRefValue(ref: RefBase<any>) {
53
54
54
55
export function triggerRefValue ( ref : RefBase < any > , newVal ?: any ) {
55
56
ref = toRaw ( ref )
56
- if ( ref . dep ) {
57
+ const dep = ref . dep
58
+ if ( dep ) {
57
59
if ( __DEV__ ) {
58
- triggerEffects ( ref . dep , {
60
+ triggerEffects ( dep , {
59
61
target : ref ,
60
62
type : TriggerOpTypes . SET ,
61
63
key : 'value' ,
62
64
newValue : newVal
63
65
} )
64
66
} else {
65
- triggerEffects ( ref . dep )
67
+ triggerEffects ( dep )
66
68
}
67
69
}
68
70
}
@@ -228,6 +230,10 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
228
230
set value ( newVal ) {
229
231
this . _object [ this . _key ] = newVal
230
232
}
233
+
234
+ get dep ( ) : Dep | undefined {
235
+ return getDepFromReactive ( toRaw ( this . _object ) , this . _key )
236
+ }
231
237
}
232
238
233
239
export type ToRef < T > = IfAny < T , Ref < T > , [ T ] extends [ Ref ] ? T : Ref < T > >
Original file line number Diff line number Diff line change @@ -30,7 +30,8 @@ import {
30
30
triggerRef ,
31
31
shallowRef ,
32
32
Ref ,
33
- effectScope
33
+ effectScope ,
34
+ toRef
34
35
} from '@vue/reactivity'
35
36
36
37
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
@@ -926,6 +927,25 @@ describe('api: watch', () => {
926
927
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
927
928
} )
928
929
930
+ test ( 'should force trigger on triggerRef with toRef from reactive' , async ( ) => {
931
+ const foo = reactive ( { bar : 1 } )
932
+ const bar = toRef ( foo , 'bar' )
933
+ const spy = jest . fn ( )
934
+
935
+ watchEffect ( ( ) => {
936
+ bar . value
937
+ spy ( )
938
+ } )
939
+
940
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
941
+
942
+ triggerRef ( bar )
943
+
944
+ await nextTick ( )
945
+ // should trigger now
946
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 )
947
+ } )
948
+
929
949
// #2125
930
950
test ( 'watchEffect should not recursively trigger itself' , async ( ) => {
931
951
const spy = vi . fn ( )
You can’t perform that action at this time.
0 commit comments