File tree 2 files changed +27
-3
lines changed
2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -336,4 +336,24 @@ describe('reactivity/ref', () => {
336
336
_trigger ! ( )
337
337
expect ( dummy ) . toBe ( 2 )
338
338
} )
339
+
340
+ test ( 'should not trigger when setting value to same proxy' , ( ) => {
341
+ const obj = reactive ( { count : 0 } )
342
+
343
+ const a = ref ( obj )
344
+ const spy1 = jest . fn ( ( ) => a . value )
345
+
346
+ effect ( spy1 )
347
+
348
+ a . value = obj
349
+ expect ( spy1 ) . toBeCalledTimes ( 1 )
350
+
351
+ const b = shallowRef ( obj )
352
+ const spy2 = jest . fn ( ( ) => b . value )
353
+
354
+ effect ( spy2 )
355
+
356
+ b . value = obj
357
+ expect ( spy2 ) . toBeCalledTimes ( 1 )
358
+ } )
339
359
} )
Original file line number Diff line number Diff line change @@ -52,12 +52,15 @@ export function shallowRef(value?: unknown) {
52
52
}
53
53
54
54
class RefImpl < T > {
55
+ private _rawValue : T
56
+
55
57
private _value : T
56
58
57
59
public readonly __v_isRef = true
58
60
59
- constructor ( private _rawValue : T , public readonly _shallow : boolean ) {
60
- this . _value = _shallow ? _rawValue : convert ( _rawValue )
61
+ constructor ( value : T , public readonly _shallow = false ) {
62
+ this . _rawValue = _shallow ? value : toRaw ( value )
63
+ this . _value = _shallow ? value : convert ( value )
61
64
}
62
65
63
66
get value ( ) {
@@ -66,7 +69,8 @@ class RefImpl<T> {
66
69
}
67
70
68
71
set value ( newVal ) {
69
- if ( hasChanged ( toRaw ( newVal ) , this . _rawValue ) ) {
72
+ newVal = this . _shallow ? newVal : toRaw ( newVal )
73
+ if ( hasChanged ( newVal , this . _rawValue ) ) {
70
74
this . _rawValue = newVal
71
75
this . _value = this . _shallow ? newVal : convert ( newVal )
72
76
trigger ( toRaw ( this ) , TriggerOpTypes . SET , 'value' , newVal )
You can’t perform that action at this time.
0 commit comments