vue3-yuanma/packages/reactivity/src/ref.ts

90 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-08-16 13:42:46 +00:00
import { track, trigger } from './effect'
import { OperationTypes } from './operations'
import { isObject } from '@vue/shared'
import { reactive, isReactive } from './reactive'
2019-10-14 15:21:09 +00:00
import { ComputedRef } from './computed'
2019-10-22 15:26:48 +00:00
import { CollectionTypes } from './collectionHandlers'
2019-08-16 13:42:46 +00:00
2019-10-09 18:01:53 +00:00
export interface Ref<T = any> {
_isRef: true
value: UnwrapRef<T>
2019-08-16 13:42:46 +00:00
}
2019-10-22 15:26:48 +00:00
const convert = <T extends unknown>(val: T): T =>
isObject(val) ? reactive(val) : val
2019-08-16 13:42:46 +00:00
export function ref<T extends Ref>(raw: T): T
export function ref<T>(raw: T): Ref<T>
export function ref<T = any>(): Ref<T>
export function ref(raw?: unknown) {
if (isRef(raw)) {
return raw
}
2019-08-16 13:42:46 +00:00
raw = convert(raw)
2019-10-22 15:26:48 +00:00
const r = {
_isRef: true,
2019-08-16 13:42:46 +00:00
get value() {
track(r, OperationTypes.GET, 'value')
2019-08-16 13:42:46 +00:00
return raw
},
set value(newVal) {
raw = convert(newVal)
trigger(
r,
OperationTypes.SET,
'value',
__DEV__ ? { newValue: newVal } : void 0
)
2019-08-16 13:42:46 +00:00
}
}
2019-10-22 15:26:48 +00:00
return r as Ref
2019-08-16 13:42:46 +00:00
}
2019-10-22 15:26:48 +00:00
export function isRef(r: any): r is Ref {
return r ? r._isRef === true : false
2019-08-20 13:38:00 +00:00
}
export function toRefs<T extends object>(
object: T
): { [K in keyof T]: Ref<T[K]> } {
if (__DEV__ && !isReactive(object)) {
console.warn(`toRefs() expects a reactive object but received a plain one.`)
}
2019-08-20 13:38:00 +00:00
const ret: any = {}
for (const key in object) {
ret[key] = toProxyRef(object, key)
}
return ret
}
function toProxyRef<T extends object, K extends keyof T>(
object: T,
key: K
): Ref<T[K]> {
return {
_isRef: true,
get value(): any {
2019-08-20 13:38:00 +00:00
return object[key]
},
set value(newVal) {
object[key] = newVal
}
}
2019-08-16 13:42:46 +00:00
}
// Recursively unwraps nested value bindings.
export type UnwrapRef<T> = {
cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
ref: T extends Ref<infer V> ? UnwrapRef<V> : T
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
object: { [K in keyof T]: UnwrapRef<T[K]> }
}[T extends ComputedRef<any>
? 'cRef'
2019-10-14 15:21:09 +00:00
: T extends Ref
2019-10-22 15:52:29 +00:00
? 'ref'
: T extends Array<any>
? 'array'
: T extends Function | CollectionTypes
? 'ref' // bail out on types that shouldn't be unwrapped
: T extends object ? 'object' : 'ref']