import * as T from '../types' ; /**
 * Dispatch Plugin
 *
 * generates dispatch[modelName][actionName]
 */ const dispatchPlugin = { exposed : { storeDispatch ( action , state ) { console . warn ( 'Warning: store not yet loaded' ) ; } , storeGetState ( ) { console . warn ( 'Warning: store not yet loaded' ) ; } , param action dispatch ( action ) { return this . storeDispatch ( action ) ; } , param modelName param reducerName createDispatcher ( modelName , reducerName ) { return async ( payload ? , meta ? ) : Promise < any > => { const action = { type : `${ modelName }/${ reducerName }` } ; if ( typeof payload !== 'undefined' ) { action . payload = payload ; } if ( typeof meta !== 'undefined' ) { action . meta = meta ; } return this . dispatch ( action ) ; } ; } , } , onStoreCreated ( store ) { this . storeDispatch = store . dispatch ; this . storeGetState = store . getState ; return { dispatch : this . dispatch } ; } , onModel ( model ) { this . dispatch [ model . name ] = { } ; if ( ! model . reducers ) { return ; } for ( const reducerName of Object . keys ( model . reducers ) ) { this . validate ( [ [ ! ! reducerName . match ( /\/.+\// ) , `Invalid reducer name (${ model . name }/${ reducerName })` , ] , [ typeof model . reducers [ reducerName ] !== 'function' , `Invalid reducer (${ model . name }/${ reducerName }). Must be a function` , ] , ] ) ; this . dispatch [ model . name ] [ reducerName ] = this . createDispatcher . apply ( this , [ model . name , reducerName ] ) ; } } , } ; export default dispatchPlugin ;