import { computed , comparer , Lambda , reaction , Reaction } from 'mobx' ; import { registerPlugin } from '../__core/plugin' ; type WatchHandler < T > = ( newVal : T , oldVal : T , disposer : Lambda ) => void ; type DefineWatch < State = any > = { [ K in keyof Partial < State > ] : WatchHandler < State [ K ] > | { handler : WatchHandler < State [ K ] > ; immediate ? : boolean ; deep ? : boolean ; } ; } & { [ key ] : WatchHandler < any > | { handler : WatchHandler < any > ; immediate ? : boolean ; deep ? : boolean ; } ; } ; declare module '../types' { interface IModelOptions < State = { } , Effects = { } , Computed = { } , Ref = { } , Watch = { } > { watch ? : Watch & ThisType < CombineObject < State , Effects , Computed , Ref > > ; } function model < State , Effects , Computed , Ref , Watch extends DefineWatch < State > > ( options ) ; } function setupWatch ( ins , watch = { } ) { for ( const key in watch ) { const value = watch [ key ] ; let handler ; let fireImmediately = false ; let equals = comparer . default ; if ( typeof value === 'function' ) { handler = value . bind ( ins ) ; } else if ( typeof value === 'object' && typeof value . handler === 'function' ) { handler = value . handler . bind ( ins ) ; fireImmediately = value . immediate || false ; equals = value . deep ? comparer . structural : comparer . default ; } else { return ; } const disposer = reaction ( ( ) => { const segments = key . split ( '.' ) ; let obj = ins ; for ( let i = 0 ; i < segments . length ; i ++ ) { obj = obj [ segments [ i ] ] ; } return obj ; } , ( newValue , oldValue ) => { handler ( newValue , oldValue , disposer ) ; } , { equals : equals , fireImmediately , } ) ; } } registerPlugin ( 'watch' , setupWatch , 'internal' ) ;