import { isObject } from '@kirei/shared' ; import { stop , effect , isRef , isReactive } from '@vue/reactivity' ; import type { Ref } from '@vue/reactivity' ; import { isMap , isSet , isFunction } from '@vue/shared' ; /**
 * Function to stop a reactive watcher
 */ type StopWatcher = ( ) => void ; /**
 */ type WatchTarget < T = any > = Ref < T > | T | ( ( ) => T ) ; /**
 * Infers value types from an array of WatchTargets
 */ type InferWatchValues < T > = { [ K in keyof T ] : T [ K ] extends WatchTarget < infer V > ? V : never ; } ; /**
 * Optional configuration to pass to a watcher
 */ interface WatchOptions { /**
     * If watcher should be run immediately, with undefined value(s)
     */ immediate ? : boolean ; /**
     * If watch should traverse every child of the watched target(s)
     */ deep ? : boolean ; } param target returns export function watchEffect ( target ) { if ( ! isFunction ( target ) ) { throw new TypeError ( `watchEffect expected function as argument, got ${ typeof target }.` ) ; } const fx = effect ( target , { lazy : false } ) ; return stop . bind ( null , fx ) ; } param target param callback param options returns export function watch < T extends WatchTarget [ ] > ( target , callback , options ? ) ; export function watch < T extends WatchTarget > ( target , callback , options ? ) ; export function watch < T > ( target , callback , options ? ) { let fn ; let value ; let deep = ! ! options . deep ; if ( ! isFunction ( callback ) ) { throw new TypeError ( `Unexpected type in "callback", expected function got ${ typeof callback }` ) ; } if ( Array . isArray ( target ) ) { fn = ( ) => target . map ( t => { if ( isRef ( t ) ) { return t . value ; } else if ( isFunction ( t ) ) { return t ( ) ; } else if ( isReactive ( t ) ) { return traverse ( t ) ; } throw new TypeError ( `Unexpected type, cannot watch ${ typeof target }` ) ; } ) ; value = [ ] ; } else if ( isRef ( target ) ) { fn = ( ) => target . value ; } else if ( isFunction ( target ) ) { fn = target ; } else if ( isReactive ( target ) ) { fn = ( ) => target ; deep = true ; } else { throw new TypeError ( `Unexpected type, cannot watch ${ typeof target }` ) ; } if ( deep && callback ) { const getter = fn ; fn = ( ) => traverse ( getter ( ) ) ; } const { immediate } = options ?? { } ; const fx = effect ( fn , { lazy : true , scheduler ( run ) { const newValue = run ( ) ; callback ( newValue as T , value as T ) ; value = newValue ; } } ) ; if ( immediate ) { fx . options . scheduler ( fx ) ; } else { value = fx ( ) ; } return stop . bind ( null , fx ) ; } param value param seen returns function traverse < T = any > ( value , seen = new Set ( ) ) { if ( ! isObject ( value ) || seen . has ( value ) ) { return value ; } seen . add ( value ) ; if ( isRef ( value ) ) { traverse ( value . value , seen ) ; } else if ( Array . isArray ( value ) || isSet ( value ) || isMap ( value ) ) { value . forEach ( v => traverse ( v , seen ) ) ; } else { for ( const key in value ) { traverse ( value [ key ] , seen ) ; } } return value ; }