import * as T from '../types' ; /**
 * Effects Plugin
 *
 * Plugin for handling async actions
 */ const effectsPlugin = { exposed : { effects : { } , } , onModel ( model ) { if ( ! model . effects ) { return ; } const effects = typeof model . effects === 'function' ? model . effects ( this . dispatch ) : model . effects ; this . validate ( [ [ typeof effects !== 'object' , `Invalid effects from Model(${ model . name }), effects should return an object` , ] , ] ) ; for ( const effectName of Object . keys ( effects ) ) { this . validate ( [ [ ! ! effectName . match ( /\// ) , `Invalid effect name (${ model . name }/${ effectName })` , ] , [ typeof effects [ effectName ] !== 'function' , `Invalid effect (${ model . name }/${ effectName }). Must be a function` , ] , ] ) ; this . effects [ `${ model . name }/${ effectName }` ] = effects [ effectName ] . bind ( this . dispatch [ model . name ] ) ; this . dispatch [ model . name ] [ effectName ] = this . createDispatcher . apply ( this , [ model . name , effectName ] ) ; this . dispatch [ model . name ] [ effectName ] . isEffect = true ; } } , middleware ( store ) { return next => async ( action ) => { if ( action . type in this . effects ) { await next ( action ) ; return this . effects [ action . type ] ( action . payload , store . getState ( ) , action . meta ) ; } return next ( action ) ; } ; } , } ; export default effectsPlugin ;