|
| 1 | +import { reactive, Ref, UnwrapRef } from '.' |
| 2 | +import { isArray, isPlainObject, warn } from '../utils' |
| 3 | +import { readonlySet } from '../utils/sets' |
| 4 | + |
| 5 | +export function isReadonly(obj: any): boolean { |
| 6 | + return readonlySet.has(obj) |
| 7 | +} |
| 8 | + |
| 9 | +type Primitive = string | number | boolean | bigint | symbol | undefined | null |
| 10 | +type Builtin = Primitive | Function | Date | Error | RegExp |
| 11 | + |
| 12 | +// prettier-ignore |
| 13 | +export type DeepReadonly<T> = T extends Builtin |
| 14 | + ? T |
| 15 | + : T extends Map<infer K, infer V> |
| 16 | + ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> |
| 17 | + : T extends ReadonlyMap<infer K, infer V> |
| 18 | + ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> |
| 19 | + : T extends WeakMap<infer K, infer V> |
| 20 | + ? WeakMap<DeepReadonly<K>, DeepReadonly<V>> |
| 21 | + : T extends Set<infer U> |
| 22 | + ? ReadonlySet<DeepReadonly<U>> |
| 23 | + : T extends ReadonlySet<infer U> |
| 24 | + ? ReadonlySet<DeepReadonly<U>> |
| 25 | + : T extends WeakSet<infer U> |
| 26 | + ? WeakSet<DeepReadonly<U>> |
| 27 | + : T extends Promise<infer U> |
| 28 | + ? Promise<DeepReadonly<U>> |
| 29 | + : T extends {} |
| 30 | + ? { readonly [K in keyof T]: DeepReadonly<T[K]> } |
| 31 | + : Readonly<T> |
| 32 | + |
| 33 | +// only unwrap nested ref |
| 34 | +type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T> |
| 35 | + |
| 36 | +/** |
| 37 | + * **In @vue/composition-api, `reactive` only provides type-level readonly check** |
| 38 | + * |
| 39 | + * Creates a readonly copy of the original object. Note the returned copy is not |
| 40 | + * made reactive, but `readonly` can be called on an already reactive object. |
| 41 | + */ |
| 42 | +export function readonly<T extends object>( |
| 43 | + target: T |
| 44 | +): DeepReadonly<UnwrapNestedRefs<T>> { |
| 45 | + return target as any |
| 46 | +} |
| 47 | + |
| 48 | +export function shallowReadonly<T extends object>(obj: T): Readonly<T> |
| 49 | +export function shallowReadonly(obj: any): any { |
| 50 | + if (!(isPlainObject(obj) || isArray(obj)) || !Object.isExtensible(obj)) { |
| 51 | + return obj |
| 52 | + } |
| 53 | + |
| 54 | + const readonlyObj = {} |
| 55 | + |
| 56 | + const source = reactive({}) |
| 57 | + const ob = (source as any).__ob__ |
| 58 | + |
| 59 | + for (const key of Object.keys(obj)) { |
| 60 | + let val = obj[key] |
| 61 | + let getter: (() => any) | undefined |
| 62 | + let setter: ((x: any) => void) | undefined |
| 63 | + const property = Object.getOwnPropertyDescriptor(obj, key) |
| 64 | + if (property) { |
| 65 | + if (property.configurable === false) { |
| 66 | + continue |
| 67 | + } |
| 68 | + getter = property.get |
| 69 | + setter = property.set |
| 70 | + if ( |
| 71 | + (!getter || setter) /* not only have getter */ && |
| 72 | + arguments.length === 2 |
| 73 | + ) { |
| 74 | + val = obj[key] |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Object.defineProperty(readonlyObj, key, { |
| 79 | + enumerable: true, |
| 80 | + configurable: true, |
| 81 | + get: function getterHandler() { |
| 82 | + const value = getter ? getter.call(obj) : val |
| 83 | + ob.dep.depend() |
| 84 | + return value |
| 85 | + }, |
| 86 | + set(v) { |
| 87 | + if (__DEV__) { |
| 88 | + warn(`Set operation on key "${key}" failed: target is readonly.`) |
| 89 | + } |
| 90 | + }, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + readonlySet.set(readonlyObj, true) |
| 95 | + |
| 96 | + return readonlyObj |
| 97 | +} |
0 commit comments