readonly.ts (vue-2.7.13) | : | readonly.ts (vue-2.7.14) | ||
---|---|---|---|---|
skipping to change at line 35 | skipping to change at line 35 | |||
: T extends WeakSet<infer U> | : T extends WeakSet<infer U> | |||
? WeakSet<DeepReadonly<U>> | ? WeakSet<DeepReadonly<U>> | |||
: T extends Promise<infer U> | : T extends Promise<infer U> | |||
? Promise<DeepReadonly<U>> | ? Promise<DeepReadonly<U>> | |||
: T extends Ref<infer U> | : T extends Ref<infer U> | |||
? Readonly<Ref<DeepReadonly<U>>> | ? Readonly<Ref<DeepReadonly<U>>> | |||
: T extends {} | : T extends {} | |||
? { readonly [K in keyof T]: DeepReadonly<T[K]> } | ? { readonly [K in keyof T]: DeepReadonly<T[K]> } | |||
: Readonly<T> | : Readonly<T> | |||
const rawToReadonlyMap = new WeakMap() | const rawToReadonlyFlag = `__v_rawToReadonly` | |||
const rawToShallowReadonlyMap = new WeakMap() | const rawToShallowReadonlyFlag = `__v_rawToShallowReadonly` | |||
export function readonly<T extends object>( | export function readonly<T extends object>( | |||
target: T | target: T | |||
): DeepReadonly<UnwrapNestedRefs<T>> { | ): DeepReadonly<UnwrapNestedRefs<T>> { | |||
return createReadonly(target, false) | return createReadonly(target, false) | |||
} | } | |||
function createReadonly(target: any, shallow: boolean) { | function createReadonly(target: any, shallow: boolean) { | |||
if (!isPlainObject(target)) { | if (!isPlainObject(target)) { | |||
if (__DEV__) { | if (__DEV__) { | |||
skipping to change at line 60 | skipping to change at line 60 | |||
warn( | warn( | |||
`Vue 2 does not support readonly collection types such as Map or Set.` | `Vue 2 does not support readonly collection types such as Map or Set.` | |||
) | ) | |||
} else { | } else { | |||
warn(`value cannot be made readonly: ${typeof target}`) | warn(`value cannot be made readonly: ${typeof target}`) | |||
} | } | |||
} | } | |||
return target as any | return target as any | |||
} | } | |||
if (__DEV__ && !Object.isExtensible(target)) { | ||||
warn( | ||||
`Vue 2 does not support creating readonly proxy for non-extensible object. | ||||
` | ||||
) | ||||
} | ||||
// already a readonly object | // already a readonly object | |||
if (isReadonly(target)) { | if (isReadonly(target)) { | |||
return target as any | return target as any | |||
} | } | |||
// already has a readonly proxy | // already has a readonly proxy | |||
const map = shallow ? rawToShallowReadonlyMap : rawToReadonlyMap | const existingFlag = shallow ? rawToShallowReadonlyFlag : rawToReadonlyFlag | |||
const existingProxy = map.get(target) | const existingProxy = target[existingFlag] | |||
if (existingProxy) { | if (existingProxy) { | |||
return existingProxy | return existingProxy | |||
} | } | |||
const proxy = Object.create(Object.getPrototypeOf(target)) | const proxy = Object.create(Object.getPrototypeOf(target)) | |||
map.set(target, proxy) | def(target, existingFlag, proxy) | |||
def(proxy, ReactiveFlags.IS_READONLY, true) | def(proxy, ReactiveFlags.IS_READONLY, true) | |||
def(proxy, ReactiveFlags.RAW, target) | def(proxy, ReactiveFlags.RAW, target) | |||
if (isRef(target)) { | if (isRef(target)) { | |||
def(proxy, RefFlag, true) | def(proxy, RefFlag, true) | |||
} | } | |||
if (shallow || isShallow(target)) { | if (shallow || isShallow(target)) { | |||
def(proxy, ReactiveFlags.IS_SHALLOW, true) | def(proxy, ReactiveFlags.IS_SHALLOW, true) | |||
} | } | |||
End of changes. 4 change blocks. | ||||
5 lines changed or deleted | 12 lines changed or added |