import EventEmitter from 'eventemitter3' ; import { splitPropertyAndMethod } from './helper' ; type Listener = ( ) => void ; /**
 * The container is one of the most important objects in FOX.
 * It manages the organization, distribution,
 * and synchronization of all module states to the UI layer
 */ class Container { private rootState : Record < string , any > ; private rootReducers : Record < string , any > ; private rootEffects : Record < string , any > ; private emitter : EventEmitter ; constructor ( modules ) { const initialData = splitPropertyAndMethod ( modules ) ; this . rootState = initialData . rootState ; this . rootReducers = initialData . rootReducers ; this . rootEffects = initialData . rootEffects ; this . emitter = new EventEmitter ( ) ; } public getRootEffects ( ) { return this . rootEffects ; } public getRootReducers ( ) { return this . rootReducers ; } public getRootState ( ) { return this . rootState ; } public getModule ( namespace ) { const combineModule = { } ; const assign = ( k ) => { const module = { state : this . rootState [ k ] , reducers : this . rootReducers [ k ] , effects : this . rootEffects [ k ] } ; Object . assign ( combineModule , { [ k ] : module } ) ; } ; if ( Array . isArray ( namespace ) ) { namespace . forEach ( ( k ) => assign ( k ) ) ; } else { assign ( namespace ) ; } return combineModule ; } public getState ( namespace ) { const combineModule = this . getModule ( namespace ) ; return Object . keys ( combineModule ) . reduce ( ( result , key ) => { result [ key ] = combineModule [ key ] . state ; return result ; } , { } ) ; } public setState ( namespace , newState ) { if ( this . rootState [ namespace ] ) { this . rootState [ namespace ] = newState ; } this . trigger ( namespace ) ; } public addListener ( namespace , listener ) { if ( Array . isArray ( namespace ) ) { namespace . forEach ( ( k ) => this . emitter . on ( k , listener ) ) ; } else { this . emitter . on ( namespace , listener ) ; } } public removeListener ( namespace , listener ) { if ( Array . isArray ( namespace ) ) { namespace . forEach ( ( k ) => this . emitter . removeListener ( k , listener ) ) ; } else { this . emitter . removeListener ( namespace , listener ) ; } } public trigger ( namespace ) { this . emitter . emit ( namespace ) ; } } export default Container ;