@@ -30,40 +30,42 @@ const getProto = <T extends CollectionTypes>(v: T): any =>
30
30
function get (
31
31
target : MapTypes ,
32
32
key : unknown ,
33
- wrap : typeof toReactive | typeof toReadonly | typeof toShallow
33
+ isReadonly = false ,
34
+ isShallow = false
34
35
) {
35
36
// #1772: readonly(reactive(Map)) should return readonly + reactive version
36
37
// of the value
37
38
target = ( target as any ) [ ReactiveFlags . RAW ]
38
39
const rawTarget = toRaw ( target )
39
40
const rawKey = toRaw ( key )
40
41
if ( key !== rawKey ) {
41
- track ( rawTarget , TrackOpTypes . GET , key )
42
+ ! isReadonly && track ( rawTarget , TrackOpTypes . GET , key )
42
43
}
43
- track ( rawTarget , TrackOpTypes . GET , rawKey )
44
+ ! isReadonly && track ( rawTarget , TrackOpTypes . GET , rawKey )
44
45
const { has } = getProto ( rawTarget )
46
+ const wrap = isReadonly ? toReadonly : isShallow ? toShallow : toReactive
45
47
if ( has . call ( rawTarget , key ) ) {
46
48
return wrap ( target . get ( key ) )
47
49
} else if ( has . call ( rawTarget , rawKey ) ) {
48
50
return wrap ( target . get ( rawKey ) )
49
51
}
50
52
}
51
53
52
- function has ( this : CollectionTypes , key : unknown ) : boolean {
53
- const target = toRaw ( this )
54
+ function has ( this : CollectionTypes , key : unknown , isReadonly = false ) : boolean {
55
+ const target = ( this as any ) [ ReactiveFlags . RAW ]
56
+ const rawTarget = toRaw ( target )
54
57
const rawKey = toRaw ( key )
55
58
if ( key !== rawKey ) {
56
- track ( target , TrackOpTypes . HAS , key )
59
+ ! isReadonly && track ( rawTarget , TrackOpTypes . HAS , key )
57
60
}
58
- track ( target , TrackOpTypes . HAS , rawKey )
59
- const has = getProto ( target ) . has
60
- return has . call ( target , key ) || has . call ( target , rawKey )
61
+ ! isReadonly && track ( rawTarget , TrackOpTypes . HAS , rawKey )
62
+ return target . has ( key ) || target . has ( rawKey )
61
63
}
62
64
63
- function size ( target : IterableCollections ) {
64
- target = toRaw ( target )
65
- track ( target , TrackOpTypes . ITERATE , ITERATE_KEY )
66
- return Reflect . get ( getProto ( target ) , 'size' , target )
65
+ function size ( target : IterableCollections , isReadonly = false ) {
66
+ target = ( target as any ) [ ReactiveFlags . RAW ]
67
+ ! isReadonly && track ( toRaw ( target ) , TrackOpTypes . ITERATE , ITERATE_KEY )
68
+ return Reflect . get ( target , 'size' , target )
67
69
}
68
70
69
71
function add ( this : SetTypes , value : unknown ) {
@@ -137,15 +139,15 @@ function clear(this: IterableCollections) {
137
139
return result
138
140
}
139
141
140
- function createForEach ( isReadonly : boolean , shallow : boolean ) {
142
+ function createForEach ( isReadonly : boolean , isShallow : boolean ) {
141
143
return function forEach (
142
144
this : IterableCollections ,
143
145
callback : Function ,
144
146
thisArg ?: unknown
145
147
) {
146
148
const observed = this
147
149
const target = toRaw ( observed )
148
- const wrap = isReadonly ? toReadonly : shallow ? toShallow : toReactive
150
+ const wrap = isReadonly ? toReadonly : isShallow ? toShallow : toReactive
149
151
! isReadonly && track ( target , TrackOpTypes . ITERATE , ITERATE_KEY )
150
152
// important: create sure the callback is
151
153
// 1. invoked with the reactive map as `this` and 3rd arg
@@ -173,19 +175,19 @@ interface IterationResult {
173
175
function createIterableMethod (
174
176
method : string | symbol ,
175
177
isReadonly : boolean ,
176
- shallow : boolean
178
+ isShallow : boolean
177
179
) {
178
180
return function (
179
181
this : IterableCollections ,
180
182
...args : unknown [ ]
181
183
) : Iterable & Iterator {
182
184
const target = ( this as any ) [ ReactiveFlags . RAW ]
183
- const rawTarget = toRaw ( this )
185
+ const rawTarget = toRaw ( target )
184
186
const isMap = rawTarget instanceof Map
185
187
const isPair = method === 'entries' || ( method === Symbol . iterator && isMap )
186
188
const isKeyOnly = method === 'keys' && isMap
187
189
const innerIterator = target [ method ] ( ...args )
188
- const wrap = isReadonly ? toReadonly : shallow ? toShallow : toReactive
190
+ const wrap = isReadonly ? toReadonly : isShallow ? toShallow : toReactive
189
191
! isReadonly &&
190
192
track (
191
193
rawTarget ,
@@ -228,7 +230,7 @@ function createReadonlyMethod(type: TriggerOpTypes): Function {
228
230
229
231
const mutableInstrumentations : Record < string , Function > = {
230
232
get ( this : MapTypes , key : unknown ) {
231
- return get ( this , key , toReactive )
233
+ return get ( this , key )
232
234
} ,
233
235
get size ( ) {
234
236
return size ( ( this as unknown ) as IterableCollections )
@@ -243,7 +245,7 @@ const mutableInstrumentations: Record<string, Function> = {
243
245
244
246
const shallowInstrumentations : Record < string , Function > = {
245
247
get ( this : MapTypes , key : unknown ) {
246
- return get ( this , key , toShallow )
248
+ return get ( this , key , false , true )
247
249
} ,
248
250
get size ( ) {
249
251
return size ( ( this as unknown ) as IterableCollections )
@@ -258,12 +260,14 @@ const shallowInstrumentations: Record<string, Function> = {
258
260
259
261
const readonlyInstrumentations : Record < string , Function > = {
260
262
get ( this : MapTypes , key : unknown ) {
261
- return get ( this , key , toReadonly )
263
+ return get ( this , key , true )
262
264
} ,
263
265
get size ( ) {
264
- return size ( ( this as unknown ) as IterableCollections )
266
+ return size ( ( this as unknown ) as IterableCollections , true )
267
+ } ,
268
+ has ( this : MapTypes , key : unknown ) {
269
+ return has . call ( this , key , true )
265
270
} ,
266
- has,
267
271
add : createReadonlyMethod ( TriggerOpTypes . ADD ) ,
268
272
set : createReadonlyMethod ( TriggerOpTypes . SET ) ,
269
273
delete : createReadonlyMethod ( TriggerOpTypes . DELETE ) ,
0 commit comments