import { isTracking , trackEffects , triggerEffects } from './effect' ; import { TrackOpTypes , TriggerOpTypes } from './operations' ; import { isArray , isObject , hasChanged } from '@vue/shared' ; import { reactive , isProxy , toRaw , isReactive } from './reactive' ; import { CollectionTypes } from './collectionHandlers' ; import { createDep , Dep } from './dep' ; export declare const RefSymbol ; export interface Ref < T = any > { 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 _shallow ? : boolean ; } type RefBase < T > = { dep ? : Dep ; value : T ; } ; export function trackRefValue ( ref ) { if ( isTracking ( ) ) { ref = toRaw ( ref ) ; if ( ! ref . dep ) { ref . dep = createDep ( ) ; } if ( __DEV__ ) { trackEffects ( ref . dep , { target : ref , type : TrackOpTypes . GET , key : 'value' } ) ; } else { trackEffects ( ref . dep ) ; } } } export function triggerRefValue ( ref , newVal ? ) { ref = toRaw ( ref ) ; if ( ref . dep ) { if ( __DEV__ ) { triggerEffects ( ref . dep , { target : ref , type : TriggerOpTypes . SET , key : 'value' , newValue : newVal } ) ; } else { triggerEffects ( ref . dep ) ; } } } export type ToRef < T > = [ T ] extends [ Ref ] ? T : Ref < UnwrapRef < T > > ; export type ToRefs < T = any > = { [ K in keyof T ] : T [ K ] extends Ref ? T [ K ] : Ref < UnwrapRef < T [ K ] > > ; } ; const convert = < T extends unknown > ( val ) : T => isObject ( val ) ? reactive ( val ) : val ; export function isRef < T > ( r ) ; export function isRef ( r ) { return Boolean ( r && r . __v_isRef === true ) ; } export function ref < T extends object > ( value ) ; export function ref < T > ( value ) ; export function ref < T = any > ( ) ; export function ref ( value ? ) { return createRef ( value ) ; } export function shallowRef < T extends object > ( value ) ; export function shallowRef < T > ( value ) ; export function shallowRef < T = any > ( ) ; export function shallowRef ( value ? ) { return createRef ( value , true ) ; } class RefImpl < T > { private _value : T ; private _rawValue : T ; public dep ? : Dep = undefined ; public readonly __v_isRef = true ; constructor ( value , public readonly _shallow = false ) { this . _rawValue = _shallow ? value : toRaw ( value ) ; this . _value = _shallow ? value : convert ( value ) ; } get value ( ) { trackRefValue ( this ) ; return this . _value ; } set value ( newVal ) { newVal = this . _shallow ? newVal : toRaw ( newVal ) ; if ( hasChanged ( newVal , this . _rawValue ) ) { this . _rawValue = newVal ; this . _value = this . _shallow ? newVal : convert ( newVal ) ; triggerRefValue ( this , newVal ) ; } } } function createRef ( rawValue , shallow = false ) { if ( isRef ( rawValue ) ) { return rawValue ; } return new RefImpl ( rawValue , shallow ) ; } export function triggerRef ( ref ) { triggerRefValue ( ref , __DEV__ ? ref . value : void 0 ) ; } export function unref < T > ( ref ) { return isRef ( ref ) ? ( ref . value as any ) : ref ; } const shallowUnwrapHandlers = { get : ( target , key , receiver ) => unref ( Reflect . get ( target , key , receiver ) ) , set : ( target , key , value , receiver ) => { const oldValue = target [ key ] ; if ( isRef ( oldValue ) && ! isRef ( value ) ) { oldValue . value = value ; return true ; } else { return Reflect . set ( target , key , value , receiver ) ; } } } ; export function proxyRefs < T extends object > ( objectWithRefs ) { return isReactive ( objectWithRefs ) ? objectWithRefs : new Proxy ( objectWithRefs , shallowUnwrapHandlers ) ; } export type CustomRefFactory < T > = ( track : ( ) => void , trigger : ( ) => void ) => { get : ( ) => T ; set : ( value : T ) => void ; } ; class CustomRefImpl < T > { public dep ? : Dep = undefined ; private readonly _get : ReturnType < CustomRefFactory < T > > [ 'get' ] ; private readonly _set : ReturnType < CustomRefFactory < T > > [ 'set' ] ; public readonly __v_isRef = true ; constructor ( factory ) { const { get , set } = factory ( ( ) => trackRefValue ( this ) , ( ) => triggerRefValue ( this ) ) ; this . _get = get ; this . _set = set ; } get value ( ) { return this . _get ( ) ; } set value ( newVal ) { this . _set ( newVal ) ; } } export function customRef < T > ( factory ) { return new CustomRefImpl ( factory ) as any ; } export function toRefs < T extends object > ( object ) { if ( __DEV__ && ! isProxy ( object ) ) { console . warn ( `toRefs() expects a reactive object but received a plain one.` ) ; } const ret = isArray ( object ) ? new Array ( object . length ) : { } ; for ( const key in object ) { ret [ key ] = toRef ( object , key ) ; } return ret ; } class ObjectRefImpl < T extends object , K extends keyof T > { public readonly __v_isRef = true ; constructor ( private readonly _object , private readonly _key ) { } get value ( ) { return this . _object [ this . _key ] ; } set value ( newVal ) { this . _object [ this . _key ] = newVal ; } } export function toRef < T extends object , K extends keyof T > ( object , key ) { const val = object [ key ] ; return isRef ( val ) ? val : ( new ObjectRefImpl ( object , key ) as any ) ; } type BaseTypes = string | number | boolean ; /**
 * 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/reactivity' {
 *   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 { } export type ShallowUnwrapRef < T > = { [ K in keyof T ] : T [ K ] extends Ref < infer V > ? V : T [ K ] extends Ref < infer V > | undefined ? unknown extends V ? undefined : V | undefined : T [ K ] ; } ; export type UnwrapRef < T > = T extends Ref < infer V > ? UnwrapRefSimple < V > : UnwrapRefSimple < T > ; export type UnwrapRefSimple < T > = T extends Function | CollectionTypes | BaseTypes | Ref | RefUnwrapBailTypes [ keyof RefUnwrapBailTypes ] ? T : T extends Array < any > ? { [ K in keyof T ] : UnwrapRefSimple < T [ K ] > ; } : T extends object ? { [ P in keyof T ] : P extends symbol ? T [ P ] : UnwrapRef < T [ P ] > ; } : T ;