import { watchDeep } from '@goldfishjs/reactive' ; import { getMiniDataSetter } from '@goldfishjs/reactive-connect' ; import reactive from './reactive' ; import PageSetup , { SetupPageInstance , SetupPageStore } from './setup/PageSetup' ; import ComponentSetup , { SetupComponentInstance , SetupComponentStore } from './setup/ComponentSetup' ; import AppSetup , { SetupAppInstance , SetupAppStore } from './setup/AppSetup' ; type Kind = 'page' | 'component' | 'app' ; type ViewType = { page : SetupPageInstance ; component : SetupComponentInstance ; app : SetupAppInstance ; } ; type StoreType = { page : SetupPageStore ; component : SetupComponentStore ; app : SetupAppStore ; } ; type SetupType = { page : PageSetup ; component : ComponentSetup ; app : AppSetup ; } ; export interface ISetupFunction { ( ) : Record < string , any > ; } param fn param setup param viewInstance param store export default function integrateSetupFunctionResult < K extends Kind > ( fn , setup , viewInstance , store ) ; export default function integrateSetupFunctionResult ( fn , setup , viewInstance , store ) { setup . setStoreInstance ( store ) ; setup . setViewInstance ( viewInstance ) ; let config = { } ; config = fn ( ) ; const compositionState = { } ; for ( const k in config ) { const value = config [ k ] ; if ( typeof value === 'function' ) { Object . defineProperty ( viewInstance , k , { value , enumerable : true , configurable : true , writable : true , } ) ; } else { Object . defineProperty ( compositionState , k , { configurable : true , enumerable : true , get ( ) { return config [ k ] ; } , set ( v ) { config [ k ] = v ; } , } ) ; } } const reactiveCompositionState = reactive ( compositionState ) ; ( store as any ) . $$compositionState = reactiveCompositionState ; const stopList = [ ] ; stopList . push ( watchDeep ( reactiveCompositionState , ( obj , keyPathList , newV , oldV , options ) => { if ( ! ( store as any ) . isSyncDataSafe ) { return ; } const miniDataSetter = getMiniDataSetter ( ) ; miniDataSetter . set ( viewInstance , obj , keyPathList , newV , oldV , options ) ; } , { immediate : true , } ) ) ; return stopList ; }