@@ -22,13 +22,15 @@ const toReactive = <T extends unknown>(value: T): T =>
22
22
const toReadonly = < T extends unknown > ( value : T ) : T =>
23
23
isObject ( value ) ? readonly ( value ) : value
24
24
25
+ const toShallow = < T extends unknown > ( value : T ) : T => value
26
+
25
27
const getProto = < T extends CollectionTypes > ( v : T ) : any =>
26
28
Reflect . getPrototypeOf ( v )
27
29
28
30
function get (
29
31
target : MapTypes ,
30
32
key : unknown ,
31
- wrap : typeof toReactive | typeof toReadonly
33
+ wrap : typeof toReactive | typeof toReadonly | typeof toShallow
32
34
) {
33
35
target = toRaw ( target )
34
36
const rawKey = toRaw ( key )
@@ -132,15 +134,15 @@ function clear(this: IterableCollections) {
132
134
return result
133
135
}
134
136
135
- function createForEach ( isReadonly : boolean ) {
137
+ function createForEach ( isReadonly : boolean , shallow : boolean ) {
136
138
return function forEach (
137
139
this : IterableCollections ,
138
140
callback : Function ,
139
141
thisArg ?: unknown
140
142
) {
141
143
const observed = this
142
144
const target = toRaw ( observed )
143
- const wrap = isReadonly ? toReadonly : toReactive
145
+ const wrap = isReadonly ? toReadonly : shallow ? toShallow : toReactive
144
146
! isReadonly && track ( target , TrackOpTypes . ITERATE , ITERATE_KEY )
145
147
// important: create sure the callback is
146
148
// 1. invoked with the reactive map as `this` and 3rd arg
@@ -152,14 +154,18 @@ function createForEach(isReadonly: boolean) {
152
154
}
153
155
}
154
156
155
- function createIterableMethod ( method : string | symbol , isReadonly : boolean ) {
157
+ function createIterableMethod (
158
+ method : string | symbol ,
159
+ isReadonly : boolean ,
160
+ shallow : boolean
161
+ ) {
156
162
return function ( this : IterableCollections , ...args : unknown [ ] ) {
157
163
const target = toRaw ( this )
158
164
const isMap = target instanceof Map
159
165
const isPair = method === 'entries' || ( method === Symbol . iterator && isMap )
160
166
const isKeyOnly = method === 'keys' && isMap
161
167
const innerIterator = getProto ( target ) [ method ] . apply ( target , args )
162
- const wrap = isReadonly ? toReadonly : toReactive
168
+ const wrap = isReadonly ? toReadonly : shallow ? toShallow : toReactive
163
169
! isReadonly &&
164
170
track (
165
171
target ,
@@ -212,7 +218,22 @@ const mutableInstrumentations: Record<string, Function> = {
212
218
set,
213
219
delete : deleteEntry ,
214
220
clear,
215
- forEach : createForEach ( false )
221
+ forEach : createForEach ( false , false )
222
+ }
223
+
224
+ const shallowInstrumentations : Record < string , Function > = {
225
+ get ( this : MapTypes , key : unknown ) {
226
+ return get ( this , key , toShallow )
227
+ } ,
228
+ get size ( ) {
229
+ return size ( ( this as unknown ) as IterableCollections )
230
+ } ,
231
+ has,
232
+ add,
233
+ set,
234
+ delete : deleteEntry ,
235
+ clear,
236
+ forEach : createForEach ( false , true )
216
237
}
217
238
218
239
const readonlyInstrumentations : Record < string , Function > = {
@@ -227,25 +248,34 @@ const readonlyInstrumentations: Record<string, Function> = {
227
248
set : createReadonlyMethod ( TriggerOpTypes . SET ) ,
228
249
delete : createReadonlyMethod ( TriggerOpTypes . DELETE ) ,
229
250
clear : createReadonlyMethod ( TriggerOpTypes . CLEAR ) ,
230
- forEach : createForEach ( true )
251
+ forEach : createForEach ( true , false )
231
252
}
232
253
233
254
const iteratorMethods = [ 'keys' , 'values' , 'entries' , Symbol . iterator ]
234
255
iteratorMethods . forEach ( method => {
235
256
mutableInstrumentations [ method as string ] = createIterableMethod (
236
257
method ,
258
+ false ,
237
259
false
238
260
)
239
261
readonlyInstrumentations [ method as string ] = createIterableMethod (
240
262
method ,
263
+ true ,
264
+ false
265
+ )
266
+ shallowInstrumentations [ method as string ] = createIterableMethod (
267
+ method ,
268
+ true ,
241
269
true
242
270
)
243
271
} )
244
272
245
- function createInstrumentationGetter ( isReadonly : boolean ) {
246
- const instrumentations = isReadonly
247
- ? readonlyInstrumentations
248
- : mutableInstrumentations
273
+ function createInstrumentationGetter ( isReadonly : boolean , shallow : boolean ) {
274
+ const instrumentations = shallow
275
+ ? shallowInstrumentations
276
+ : isReadonly
277
+ ? readonlyInstrumentations
278
+ : mutableInstrumentations
249
279
250
280
return (
251
281
target : CollectionTypes ,
@@ -271,11 +301,15 @@ function createInstrumentationGetter(isReadonly: boolean) {
271
301
}
272
302
273
303
export const mutableCollectionHandlers : ProxyHandler < CollectionTypes > = {
274
- get : createInstrumentationGetter ( false )
304
+ get : createInstrumentationGetter ( false , false )
305
+ }
306
+
307
+ export const shallowCollectionHandlers : ProxyHandler < CollectionTypes > = {
308
+ get : createInstrumentationGetter ( false , true )
275
309
}
276
310
277
311
export const readonlyCollectionHandlers : ProxyHandler < CollectionTypes > = {
278
- get : createInstrumentationGetter ( true )
312
+ get : createInstrumentationGetter ( true , false )
279
313
}
280
314
281
315
function checkIdentityKeys (
0 commit comments