UNPKG

refx

Version:

Redux middleware for triggering side effects

49 lines (39 loc) 889 B
'use strict'; function flattenIntoMap( map, effects ) { var i; if ( Array.isArray( effects ) ) { for ( i = 0; i < effects.length; i++ ) { flattenIntoMap( map, effects[ i ] ); } } else { for ( i in effects ) { map[ i ] = ( map[ i ] || [] ).concat( effects[ i ] ); } } } function refx( effects ) { var map = {}, middleware; flattenIntoMap( map, effects ); middleware = function( store ) { return function( next ) { return function( action ) { var handlers = map[ action.type ], result = next( action ), i, handlerAction; if ( handlers ) { for ( i = 0; i < handlers.length; i++ ) { handlerAction = handlers[ i ]( action, store ); if ( handlerAction ) { store.dispatch( handlerAction ); } } } return result; }; }; }; middleware.effects = map; return middleware; } module.exports = refx;