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