import { observable , IProps , state , attachLogic } from '@goldfishjs/reactive-connect' ; import { AppStore , createComponent , ComponentStore } from '@goldfishjs/core' ; import { cloneDeep } from '@goldfishjs/utils' ; import appendFn from './appendFn' ; import integrateSetupFunctionResult , { ISetupFunction } from './integrateSetupFunctionResult' ; import ComponentSetup , { SetupComponentInstance } from './setup/ComponentSetup' ; import integrateLifeCycleMethods from './integrateLifeCycleMethods' ; const lifeCycleMethods = [ 'onInit' , 'deriveDataFromProps' , 'didMount' , 'didUpdate' , 'didUnmount' , ] ; export default function setupComponent < P extends IProps , D = Record < string , any > > ( passInFn ) ; export default function setupComponent < P , D = Record < string , any > > ( passInProps , passInFn ) ; export default function setupComponent < P , D = Record < string , any > > ( passInProps , dftData , passInFn ) ; export default function setupComponent < P extends Record < string , any > , D = any > ( arg1 , arg2 ? , arg3 ? ) { let props = undefined ; let dftData = undefined ; let fn = undefined ; if ( typeof arg3 === 'function' ) { props = arg1 as P ; dftData = arg2 as D ; fn = arg3 as ISetupFunction ; } else if ( typeof arg2 === 'function' ) { props = arg1 as P ; fn = arg2 as ISetupFunction ; } else if ( typeof arg1 === 'function' ) { fn = arg1 as ISetupFunction ; } let options = { } ; if ( props ) { options . props = props ; } if ( dftData ) { options . data = dftData ; } type View = SetupComponentInstance & { $setup ? : ComponentSetup ; } ; let view ; @ observable class BizComponentStore extends ComponentStore < any , AppStore > { @ state props = ( cloneDeep ( props ) || { } ) as P ; private stopWatchDeepList : ( ( ) => void ) [ ] = [ ] ; public constructor ( ) { super ( ) ; if ( ! fn ) { throw new Error ( 'Please pass in the setup Function.' ) ; } const setup = view . $setup ! ; setup . wrap ( ( ) => { this . stopWatchDeepList = integrateSetupFunctionResult < 'component' > ( fn ! , setup , view , this ) ; } ) ; } public destroy ( ) { super . destroy ( ) ; this . stopWatchDeepList . forEach ( stop => stop ( ) ) ; this . stopWatchDeepList = [ ] ; } } options = createComponent < AppStore , BizComponentStore , P , D > ( BizComponentStore , options , { beforeCreateStore : ( v ) => { const setup = new ComponentSetup ( ) ; v . $setup = setup ; view = v ; setup . iterateMethods ( ( fns , name ) => { appendFn ( v , name , fns as Function [ ] ) ; } ) ; } , } ) as any ; const lifeCycleMethodsOptions = integrateLifeCycleMethods < 'component' > ( lifeCycleMethods ) ; lifeCycleMethods . forEach ( m => { attachLogic ( options , m , 'after' , lifeCycleMethodsOptions [ m ] as any ) ; } ) ; return options ; }