-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreactive.ts
32 lines (27 loc) · 1 KB
/
reactive.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
import { isObject } from "../shared/type-guard"
import { RAW } from "./enum"
import { ReactiveHandle, ReadonlyHandle, ShallowReactiveHandle, ShallowReadonlyHandle } from "./handle"
import { _Reactive as Reactive } from "./type"
import { isProxy } from "./util"
export function reactive<T extends object>(raw: T): Reactive<T> {
return new Proxy<T>(raw, new ReactiveHandle()) as Reactive<T>
}
export function readonly<T extends object>(raw: T): Reactive<T> {
return new Proxy<T>(raw, new ReadonlyHandle()) as Reactive<T>
}
export function shallowReadonly<T extends object>(raw: T): Reactive<T> {
return new Proxy<T>(raw, new ShallowReadonlyHandle()) as Reactive<T>
}
export function shallowReactive<T extends object>(raw: T): Reactive<T> {
return new Proxy<T>(raw, new ShallowReactiveHandle()) as Reactive<T>
}
export function toRaw<T>(observed: Reactive<T>): T {
if (isObject(observed)) {
if (isProxy(observed)) {
return observed[RAW] as T;
} else {
return observed
}
}
return observed
}