fix(reactivity): fix shallow readonly behavior for collections (#3003)

fix #3007
This commit is contained in:
HcySunYang
2021-03-27 03:10:21 +08:00
committed by GitHub
parent 9cb21d088e
commit 68de9f408a
5 changed files with 393 additions and 35 deletions

View File

@@ -44,7 +44,7 @@ function get(
}
!isReadonly && track(rawTarget, TrackOpTypes.GET, rawKey)
const { has } = getProto(rawTarget)
const wrap = isReadonly ? toReadonly : isShallow ? toShallow : toReactive
const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
if (has.call(rawTarget, key)) {
return wrap(target.get(key))
} else if (has.call(rawTarget, rawKey)) {
@@ -151,7 +151,7 @@ function createForEach(isReadonly: boolean, isShallow: boolean) {
const observed = this as any
const target = observed[ReactiveFlags.RAW]
const rawTarget = toRaw(target)
const wrap = isReadonly ? toReadonly : isShallow ? toShallow : toReactive
const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
!isReadonly && track(rawTarget, TrackOpTypes.ITERATE, ITERATE_KEY)
return target.forEach((value: unknown, key: unknown) => {
// important: make sure the callback is
@@ -191,7 +191,7 @@ function createIterableMethod(
method === 'entries' || (method === Symbol.iterator && targetIsMap)
const isKeyOnly = method === 'keys' && targetIsMap
const innerIterator = target[method](...args)
const wrap = isReadonly ? toReadonly : isShallow ? toShallow : toReactive
const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
!isReadonly &&
track(
rawTarget,
@@ -279,6 +279,23 @@ const readonlyInstrumentations: Record<string, Function> = {
forEach: createForEach(true, false)
}
const shallowReadonlyInstrumentations: Record<string, Function> = {
get(this: MapTypes, key: unknown) {
return get(this, key, true, true)
},
get size() {
return size((this as unknown) as IterableCollections, true)
},
has(this: MapTypes, key: unknown) {
return has.call(this, key, true)
},
add: createReadonlyMethod(TriggerOpTypes.ADD),
set: createReadonlyMethod(TriggerOpTypes.SET),
delete: createReadonlyMethod(TriggerOpTypes.DELETE),
clear: createReadonlyMethod(TriggerOpTypes.CLEAR),
forEach: createForEach(true, true)
}
const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator]
iteratorMethods.forEach(method => {
mutableInstrumentations[method as string] = createIterableMethod(
@@ -296,11 +313,18 @@ iteratorMethods.forEach(method => {
false,
true
)
shallowReadonlyInstrumentations[method as string] = createIterableMethod(
method,
true,
true
)
})
function createInstrumentationGetter(isReadonly: boolean, shallow: boolean) {
const instrumentations = shallow
? shallowInstrumentations
? isReadonly
? shallowReadonlyInstrumentations
: shallowInstrumentations
: isReadonly
? readonlyInstrumentations
: mutableInstrumentations
@@ -340,6 +364,12 @@ export const readonlyCollectionHandlers: ProxyHandler<CollectionTypes> = {
get: createInstrumentationGetter(true, false)
}
export const shallowReadonlyCollectionHandlers: ProxyHandler<
CollectionTypes
> = {
get: createInstrumentationGetter(true, true)
}
function checkIdentityKeys(
target: CollectionTypes,
has: (key: unknown) => boolean,

View File

@@ -8,7 +8,8 @@ import {
import {
mutableCollectionHandlers,
readonlyCollectionHandlers,
shallowCollectionHandlers
shallowCollectionHandlers,
shallowReadonlyCollectionHandlers
} from './collectionHandlers'
import { UnwrapRef, Ref } from './ref'
@@ -159,7 +160,7 @@ export function shallowReadonly<T extends object>(
target,
true,
shallowReadonlyHandlers,
readonlyCollectionHandlers
shallowReadonlyCollectionHandlers
)
}