File tree 2 files changed +40
-15
lines changed
2 files changed +40
-15
lines changed Original file line number Diff line number Diff line change @@ -22,11 +22,19 @@ describe('reactivity/ref', () => {
22
22
it ( 'should be reactive' , ( ) => {
23
23
const a = ref ( 1 )
24
24
let dummy
25
+ let calls = 0
25
26
effect ( ( ) => {
27
+ calls ++
26
28
dummy = a . value
27
29
} )
30
+ expect ( calls ) . toBe ( 1 )
28
31
expect ( dummy ) . toBe ( 1 )
29
32
a . value = 2
33
+ expect ( calls ) . toBe ( 2 )
34
+ expect ( dummy ) . toBe ( 2 )
35
+ // same value should not trigger
36
+ a . value = 2
37
+ expect ( calls ) . toBe ( 2 )
30
38
expect ( dummy ) . toBe ( 2 )
31
39
} )
32
40
@@ -174,6 +182,22 @@ describe('reactivity/ref', () => {
174
182
expect ( dummy ) . toBe ( 2 )
175
183
} )
176
184
185
+ test ( 'shallowRef force trigger' , ( ) => {
186
+ const sref = shallowRef ( { a : 1 } )
187
+ let dummy
188
+ effect ( ( ) => {
189
+ dummy = sref . value . a
190
+ } )
191
+ expect ( dummy ) . toBe ( 1 )
192
+
193
+ sref . value . a = 2
194
+ expect ( dummy ) . toBe ( 1 ) // should not trigger yet
195
+
196
+ // force trigger
197
+ sref . value = sref . value
198
+ expect ( dummy ) . toBe ( 2 )
199
+ } )
200
+
177
201
test ( 'isRef' , ( ) => {
178
202
expect ( isRef ( ref ( 1 ) ) ) . toBe ( true )
179
203
expect ( isRef ( computed ( ( ) => 1 ) ) ) . toBe ( true )
Original file line number Diff line number Diff line change 1
1
import { track , trigger } from './effect'
2
2
import { TrackOpTypes , TriggerOpTypes } from './operations'
3
- import { isObject } from '@vue/shared'
4
- import { reactive , isProxy } from './reactive'
3
+ import { isObject , hasChanged } from '@vue/shared'
4
+ import { reactive , isProxy , toRaw } from './reactive'
5
5
import { ComputedRef } from './computed'
6
6
import { CollectionTypes } from './collectionHandlers'
7
7
@@ -43,27 +43,28 @@ export function shallowRef(value?: unknown) {
43
43
return createRef ( value , true )
44
44
}
45
45
46
- function createRef ( value : unknown , shallow = false ) {
47
- if ( isRef ( value ) ) {
48
- return value
49
- }
50
- if ( ! shallow ) {
51
- value = convert ( value )
46
+ function createRef ( rawValue : unknown , shallow = false ) {
47
+ if ( isRef ( rawValue ) ) {
48
+ return rawValue
52
49
}
50
+ let value = shallow ? rawValue : convert ( rawValue )
53
51
const r = {
54
52
_isRef : true ,
55
53
get value ( ) {
56
54
track ( r , TrackOpTypes . GET , 'value' )
57
55
return value
58
56
} ,
59
57
set value ( newVal ) {
60
- value = shallow ? newVal : convert ( newVal )
61
- trigger (
62
- r ,
63
- TriggerOpTypes . SET ,
64
- 'value' ,
65
- __DEV__ ? { newValue : newVal } : void 0
66
- )
58
+ if ( shallow || hasChanged ( toRaw ( newVal ) , rawValue ) ) {
59
+ rawValue = newVal
60
+ value = shallow ? newVal : convert ( newVal )
61
+ trigger (
62
+ r ,
63
+ TriggerOpTypes . SET ,
64
+ 'value' ,
65
+ __DEV__ ? { newValue : newVal } : void 0
66
+ )
67
+ }
67
68
}
68
69
}
69
70
return r
You can’t perform that action at this time.
0 commit comments