import type { Middleware } from './dispatcher.h' ; import { DispatcherContext } from './dispatcherContext' ; import type { StoreClass } from '../typings' ; import type { Reducer } from '../createReducer/createReducer.h' ; export class Dispatcher { stores : Record < StoreClass [ 'storeName' ] , StoreClass > ; class param options param options . stores constructor constructor ( options ? ) { options = options || { } ; options . stores = options . stores || ( [ ] as any [ ] ) ; this . stores = { } ; this . handlers = { } ; options . stores . forEach ( ( store ) => { this . registerStore ( store ) ; } ) ; } handlers : Record < string , Array < { name : string ; handler : Function | string ; } > > ; createContext < T > ( context , initialState , middlewares ? ) { return new DispatcherContext < T > ( this , context , initialState , middlewares ) ; } hasStore ( store ) { return ! ! this . stores [ store . storeName ] ; } param store throws throws registerStore ( store ) { const { storeName } = store ; if ( this . stores [ storeName ] ) { if ( this . stores [ storeName ] === store ) { return ; } if ( process . env . NODE_ENV !== 'production' ) { throw new Error ( `Store with name '${ storeName }' has already been registered.
                Make sure you do not have multiple copies of the store installed.
                exist ${ this . stores [ storeName ] }
                try ${ store }` ) ; } } this . stores [ storeName ] = store ; if ( store . handlers ) { Object . keys ( store . handlers ) . forEach ( ( action ) => { const handler = store . handlers [ action ] ; this . registerHandler ( action , storeName , handler ) ; } ) ; } } unregisterStore ( store ) { const { storeName } = store ; if ( ! this . stores [ storeName ] ) { return ; } delete this . stores [ storeName ] ; if ( store . handlers ) { Object . keys ( store . handlers ) . forEach ( ( action ) => { const handler = store . handlers [ action ] ; this . unregisterHandler ( action , storeName , handler ) ; } ) ; } } param { String | Object } store getStoreName ( store ) { if ( typeof store === 'string' ) { return store ; } return store . storeName ; } param action param name param handler private registerHandler ( action , name , handler ) { this . handlers [ action ] = this . handlers [ action ] || [ ] ; this . handlers [ action ] . push ( { name , handler , } ) ; } private unregisterHandler ( action , name , handler ) { if ( Array . isArray ( this . handlers [ action ] ) ) { this . handlers [ action ] = this . handlers [ action ] . filter ( ( actionHandlers ) => { return ! ( actionHandlers . name === name && actionHandlers . handler === handler ) ; } ) ; } } } export function createDispatcher ( options ? ) { return new Dispatcher ( options ) ; }