import { defineReactive } from 'core/observer/index' import { isReactive, ReactiveFlags, type ShallowReactiveMarker } from './reactive' import type { IfAny } from 'types/utils' import Dep from 'core/observer/dep' import { warn, isArray, def, isServerRendering } from 'core/util' import { TrackOpTypes, TriggerOpTypes } from './operations' declare const RefSymbol: unique symbol export declare const RawSymbol: unique symbol /** * @internal */ export const RefFlag = `__v_isRef` export interface Ref { value: T /** * Type differentiator only. * We need this to be in public d.ts but don't want it to show up in IDE * autocomplete, so we use a private Symbol instead. */ [RefSymbol]: true /** * @internal */ dep?: Dep /** * @internal */ [RefFlag]: true } export function isRef(r: Ref | unknown): r is Ref export function isRef(r: any): r is Ref { return !!(r && (r as Ref).__v_isRef === true) } export function ref( value: T ): [T] extends [Ref] ? T : Ref> export function ref(value: T): Ref> export function ref(): Ref export function ref(value?: unknown) { return createRef(value, false) } declare const ShallowRefMarker: unique symbol export type ShallowRef = Ref & { [ShallowRefMarker]?: true } export function shallowRef( value: T ): T extends Ref ? T : ShallowRef export function shallowRef(value: T): ShallowRef export function shallowRef(): ShallowRef export function shallowRef(value?: unknown) { return createRef(value, true) } function createRef(rawValue: unknown, shallow: boolean) { if (isRef(rawValue)) { return rawValue } const ref: any = {} def(ref, RefFlag, true) def(ref, ReactiveFlags.IS_SHALLOW, shallow) def( ref, 'dep', defineReactive(ref, 'value', rawValue, null, shallow, isServerRendering()) ) return ref } export function triggerRef(ref: Ref) { if (__DEV__ && !ref.dep) { warn(`received object is not a triggerable ref.`) } if (__DEV__) { ref.dep && ref.dep.notify({ type: TriggerOpTypes.SET, target: ref, key: 'value' }) } else { ref.dep && ref.dep.notify() } } export function unref(ref: T | Ref): T { return isRef(ref) ? (ref.value as any) : ref } export function proxyRefs( objectWithRefs: T ): ShallowUnwrapRef { if (isReactive(objectWithRefs)) { return objectWithRefs as any } const proxy = {} const keys = Object.keys(objectWithRefs) for (let i = 0; i < keys.length; i++) { proxyWithRefUnwrap(proxy, objectWithRefs, keys[i]) } return proxy as any } export function proxyWithRefUnwrap( target: any, source: Record, key: string ) { Object.defineProperty(target, key, { enumerable: true, configurable: true, get: () => { const val = source[key] if (isRef(val)) { return val.value } else { const ob = val && val.__ob__ if (ob) ob.dep.depend() return val } }, set: value => { const oldValue = source[key] if (isRef(oldValue) && !isRef(value)) { oldValue.value = value } else { source[key] = value } } }) } export type CustomRefFactory = ( track: () => void, trigger: () => void ) => { get: () => T set: (value: T) => void } export function customRef(factory: CustomRefFactory): Ref { const dep = new Dep() const { get, set } = factory( () => { if (__DEV__) { dep.depend({ target: ref, type: TrackOpTypes.GET, key: 'value' }) } else { dep.depend() } }, () => { if (__DEV__) { dep.notify({ target: ref, type: TriggerOpTypes.SET, key: 'value' }) } else { dep.notify() } } ) const ref = { get value() { return get() }, set value(newVal) { set(newVal) } } as any def(ref, RefFlag, true) return ref } export type ToRefs = { [K in keyof T]: ToRef } export function toRefs(object: T): ToRefs { if (__DEV__ && !isReactive(object)) { warn(`toRefs() expects a reactive object but received a plain one.`) } const ret: any = isArray(object) ? new Array(object.length) : {} for (const key in object) { ret[key] = toRef(object, key) } return ret } export type ToRef = IfAny, [T] extends [Ref] ? T : Ref> export function toRef( object: T, key: K ): ToRef export function toRef( object: T, key: K, defaultValue: T[K] ): ToRef> export function toRef( object: T, key: K, defaultValue?: T[K] ): ToRef { const val = object[key] if (isRef(val)) { return val as any } const ref = { get value() { const val = object[key] return val === undefined ? (defaultValue as T[K]) : val }, set value(newVal) { object[key] = newVal } } as any def(ref, RefFlag, true) return ref } /** * This is a special exported interface for other packages to declare * additional types that should bail out for ref unwrapping. For example * \@vue/runtime-dom can declare it like so in its d.ts: * * ``` ts * declare module 'vue' { * export interface RefUnwrapBailTypes { * runtimeDOMBailTypes: Node | Window * } * } * ``` * * Note that api-extractor somehow refuses to include `declare module` * augmentations in its generated d.ts, so we have to manually append them * to the final generated d.ts in our build process. */ export interface RefUnwrapBailTypes { runtimeDOMBailTypes: Node | Window } export type ShallowUnwrapRef = { [K in keyof T]: T[K] extends Ref ? V : // if `V` is `unknown` that means it does not extend `Ref` and is undefined T[K] extends Ref | undefined ? unknown extends V ? undefined : V | undefined : T[K] } export type UnwrapRef = T extends ShallowRef ? V : T extends Ref ? UnwrapRefSimple : UnwrapRefSimple type BaseTypes = string | number | boolean type CollectionTypes = IterableCollections | WeakCollections type IterableCollections = Map | Set type WeakCollections = WeakMap | WeakSet export type UnwrapRefSimple = T extends | Function | CollectionTypes | BaseTypes | Ref | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] | { [RawSymbol]?: true } ? T : T extends Array ? { [K in keyof T]: UnwrapRefSimple } : T extends object & { [ShallowReactiveMarker]?: never } ? { [P in keyof T]: P extends symbol ? T[P] : UnwrapRef } : T