import { IStore , watch as baseWatch , IWatchExpressionFn , IWatchCallback , IWatchOptions , autorun as baseAutorun , AutorunFunction , IErrorCallback , DepList , } from '@goldfishjs/reactive' ; import STATE_KEY from './decorators/STATE_KEY' ; import COMPUTED_KEY from './decorators/COMPUTED_KEY' ; import observable from './decorators/observable' ; import state from './decorators/state' ; export type UnAutorun = ( ( ) => void ) & { depList ? : DepList | undefined ; } ; function accessStoreProperties ( keys , store ) { return keys . reduce < Record < string , any > > ( ( prev , key ) => { Object . defineProperty ( prev , key , { get : ( ) => { return ( store as any ) [ key ] ; } , set : ( v ) => { ( store as any ) [ key ] = v ; } , enumerable : true , } ) ; return prev ; } , { } ) ; } @ observable export default abstract class Store implements IStore { @ state public isInitLoading = true ; public stopWatchList : ( ( ) => void ) [ ] = [ ] ; public stopAutorunList : ( ( ) => void ) [ ] = [ ] ; public autorun ( fn , errorCb ? ) { const stop = baseAutorun ( fn , errorCb ) ; this . stopAutorunList . push ( stop ) ; return stop ; } public watch < R > ( fn , cb , options ? ) { const stop = baseWatch ( fn , cb , options ) ; this . stopWatchList . push ( stop ) ; return stop ; } public getStateKeys ( ) { return Object . keys ( ( this as any ) [ STATE_KEY ] || { } ) ; } public getComputedKeys ( ) { return Object . keys ( ( this as any ) [ COMPUTED_KEY ] || { } ) ; } public getState ( ) { const keys = this . getStateKeys ( ) ; return accessStoreProperties ( keys , this ) ; } public getComputed ( ) { const keys = this . getComputedKeys ( ) ; return accessStoreProperties ( keys , this ) ; } return public fetchInitData ( ) { return Promise . resolve ( ) ; } public init ( ) { ( async ( ) => { this . isInitLoading = true ; try { await this . fetchInitData ( ) ; } finally { this . isInitLoading = false ; } } ) ( ) ; } public destroy ( ) { this . stopWatchList . forEach ( stop => stop ( ) ) ; this . stopAutorunList . forEach ( stop => stop ( ) ) ; } }