import { AnnotationsMap , getDebugName , makeObservable } from 'mobx' ; import { useState } from 'react' ; export function useObservableRef < T extends Record < any , any > > ( init , observed , update , name ? ) ; export function useObservableRef < T extends Record < any , any > > ( init , observed , update , bind ? , name ? ) ; export function useObservableRef < T extends Record < any , any > , U extends Record < any , any > > ( init , observed , update , bind ? , name ? ) ; export function useObservableRef < T extends Record < any , any > > ( init , observed , bind ? , name ? ) ; export function useObservableRef < T extends Record < any , any > > ( init , observed , update , bind ? , name ? ) ; export function useObservableRef < T extends Record < any , any > > ( init , observed , update ? , bind ? , name ? ) { if ( typeof bind === 'string' ) { name = bind ; bind = undefined ; } if ( Array . isArray ( update ) ) { bind = update ; update = undefined ; } if ( update === undefined ) { update = typeof init === 'function' ? init ( ) : init ; } const [ state ] = useState ( ( ) => { let state = typeof init === 'function' ? init ( ) : init ; if ( update ) { Object . assign ( state , update ) ; update = undefined ; } state = makeObservable ( state , observed , { deep : false , name } ) ; if ( bind ) { bindFunctions ( state , bind as [ ] ) ; } return state ; } ) ; if ( update ) { Object . assign ( state , update ) ; if ( bind ) { bind = bind . filter ( key => ( key as any ) in ( update as T ) ) ; if ( bind . length > 0 ) { bindFunctions ( state , bind ) ; } } } return state ; } function bindFunctions < T > ( object , keys ) { for ( const key of keys ) { const value = object [ key ] ; if ( typeof value === 'function' ) { object [ key ] = value . bind ( object ) ; } } }