vue3-yuanma/packages/observer/src/value.ts

89 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-05-29 17:36:53 +08:00
import { track, trigger } from './effect'
import { OperationTypes } from './operations'
2019-05-29 18:44:50 +08:00
import { isObject } from '@vue/shared'
import { observable } from './index'
2019-05-29 17:36:53 +08:00
2019-05-29 18:44:50 +08:00
export const knownValues = new WeakSet()
2019-05-29 17:36:53 +08:00
export interface Value<T> {
value: T
}
2019-05-29 22:11:33 +08:00
type UnwrapValue<T, U = T> = T extends Value<infer V> ? V : T extends {} ? U : T
// A utility type that recursively unwraps value bindings nested inside an
// observable object. Unfortunately TS cannot do recursive types, but this
// should be enough for practical use cases...
export type UnwrapBindings<T> = {
[key in keyof T]: UnwrapValue<
T[key],
{
[k2 in keyof T[key]]: UnwrapValue<
T[key][k2],
{
[k3 in keyof T[key][k2]]: UnwrapValue<
T[key][k2][k3],
{
[k4 in keyof T[key][k2][k3]]: UnwrapValue<
T[key][k2][k3][k4],
{
[k5 in keyof T[key][k2][k3][k4]]: UnwrapValue<
T[key][k2][k3][k4][k5],
{
[k6 in keyof T[key][k2][k3][k4][k5]]: UnwrapValue<
T[key][k2][k3][k4][k5][k6],
{
[k7 in keyof T[key][k2][k3][k4][k5][k6]]: UnwrapValue<
T[key][k2][k3][k4][k5][k6][k7],
{
[k8 in keyof T[key][k2][k3][k4][k5][k6][k7]]: UnwrapValue<
T[key][k2][k3][k4][k5][k6][k7][k8],
{
[k9 in keyof T[key][k2][k3][k4][k5][k6][k7][k8]]: UnwrapValue<
T[key][k2][k3][k4][k5][k6][k7][k8][k9],
{
[k10 in keyof T[key][k2][k3][k4][k5][k6][k7][k8][k9]]: UnwrapValue<
T[key][k2][k3][k4][k5][k6][k7][k8][k9][k10]
>
}
>
}
>
}
>
}
>
}
>
}
>
}
>
}
>
}
>
}
2019-05-29 18:44:50 +08:00
const convert = (val: any): any => (isObject(val) ? observable(val) : val)
2019-05-29 17:36:53 +08:00
export function value<T>(raw: T): Value<T> {
2019-05-29 18:44:50 +08:00
raw = convert(raw)
2019-05-29 17:36:53 +08:00
const v = {
get value() {
track(v, OperationTypes.GET, '')
return raw
},
set value(newVal) {
2019-05-29 18:44:50 +08:00
raw = convert(newVal)
2019-05-29 17:36:53 +08:00
trigger(v, OperationTypes.SET, '')
}
}
knownValues.add(v)
return v
}
export function isValue(v: any): boolean {
return knownValues.has(v)
}