2019-08-16 21:42:46 +08:00
|
|
|
import { track, trigger } from './effect'
|
|
|
|
import { OperationTypes } from './operations'
|
|
|
|
import { isObject } from '@vue/shared'
|
|
|
|
import { reactive } from './reactive'
|
2019-10-14 23:21:09 +08:00
|
|
|
import { ComputedRef } from './computed'
|
2019-08-16 21:42:46 +08:00
|
|
|
|
2019-10-10 02:01:53 +08:00
|
|
|
export interface Ref<T = any> {
|
2019-10-17 09:36:17 +08:00
|
|
|
_isRef: true
|
2019-10-10 23:34:42 +08:00
|
|
|
value: UnwrapRef<T>
|
2019-08-16 21:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const convert = (val: any): any => (isObject(val) ? reactive(val) : val)
|
|
|
|
|
2019-10-12 23:05:34 +08:00
|
|
|
export function ref<T extends Ref>(raw: T): T
|
|
|
|
export function ref<T>(raw: T): Ref<T>
|
|
|
|
export function ref(raw: any) {
|
2019-10-10 23:34:42 +08:00
|
|
|
if (isRef(raw)) {
|
|
|
|
return raw
|
|
|
|
}
|
2019-08-16 21:42:46 +08:00
|
|
|
raw = convert(raw)
|
|
|
|
const v = {
|
2019-10-17 09:36:17 +08:00
|
|
|
_isRef: true,
|
2019-08-16 21:42:46 +08:00
|
|
|
get value() {
|
|
|
|
track(v, OperationTypes.GET, '')
|
|
|
|
return raw
|
|
|
|
},
|
|
|
|
set value(newVal) {
|
|
|
|
raw = convert(newVal)
|
|
|
|
trigger(v, OperationTypes.SET, '')
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 23:05:34 +08:00
|
|
|
return v as Ref
|
2019-08-16 21:42:46 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 02:01:53 +08:00
|
|
|
export function isRef(v: any): v is Ref {
|
2019-10-17 09:36:17 +08:00
|
|
|
return v ? v._isRef === true : false
|
2019-08-20 21:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function toRefs<T extends object>(
|
|
|
|
object: T
|
|
|
|
): { [K in keyof T]: Ref<T[K]> } {
|
|
|
|
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]> {
|
2019-10-10 23:34:42 +08:00
|
|
|
return {
|
2019-10-17 09:36:17 +08:00
|
|
|
_isRef: true,
|
2019-10-10 23:34:42 +08:00
|
|
|
get value(): any {
|
2019-08-20 21:38:00 +08:00
|
|
|
return object[key]
|
|
|
|
},
|
|
|
|
set value(newVal) {
|
|
|
|
object[key] = newVal
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 21:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type BailTypes =
|
|
|
|
| Function
|
|
|
|
| Map<any, any>
|
|
|
|
| Set<any>
|
|
|
|
| WeakMap<any, any>
|
|
|
|
| WeakSet<any>
|
|
|
|
|
|
|
|
// Recursively unwraps nested value bindings.
|
2019-10-07 08:11:52 +08:00
|
|
|
export type UnwrapRef<T> = {
|
2019-10-14 23:02:49 +08:00
|
|
|
cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
|
2019-10-07 08:11:52 +08:00
|
|
|
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]> }
|
|
|
|
stop: T
|
2019-10-14 23:02:49 +08:00
|
|
|
}[T extends ComputedRef<any>
|
|
|
|
? 'cRef'
|
2019-10-14 23:21:09 +08:00
|
|
|
: T extends Ref
|
|
|
|
? 'ref'
|
|
|
|
: T extends Array<any>
|
|
|
|
? 'array'
|
|
|
|
: T extends BailTypes
|
|
|
|
? 'stop' // bail out on types that shouldn't be unwrapped
|
|
|
|
: T extends object ? 'object' : 'stop']
|