forked from vuejs/composition-api
/
unwrap.ts
38 lines (32 loc) · 835 Bytes
/
unwrap.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
import { isRef } from './ref'
import { proxy, isFunction, isPlainObject, isArray } from '../utils'
import { isReactive } from './reactive'
export function unwrapRefProxy(value: any) {
if (
isFunction(value) ||
isRef(value) ||
isArray(value) ||
isReactive(value) ||
!isPlainObject(value) ||
!Object.isExtensible(value)
) {
return value
}
const obj: any = {}
// copy symbols over
Object.getOwnPropertySymbols(value).forEach(
(s) => (obj[s] = (value as any)[s])
)
for (const k of Object.keys(value)) {
const r = value[k]
// if is a ref, create a proxy to retrieve the value,
if (isRef(r)) {
const set = (v: any) => (r.value = v)
const get = () => r.value
proxy(obj, k, { get, set })
} else {
obj[k] = unwrapRefProxy(r)
}
}
return obj
}