File size: 730 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import notes from './notes';
import overrides from './overrides';
import suggestions from './suggestions';
import ui from './ui';
import { mergeHandlers } from './utils';

const mergedHandlers = mergeHandlers( notes, overrides, suggestions, ui );

export const middleware = ( handlers ) => ( store ) => ( next ) => ( action ) => {
	const handlerChain = handlers[ action.type ];

	// if no handler is defined for the action type
	// then pass it along the chain untouched
	if ( ! handlerChain ) {
		return next( action );
	}

	handlerChain.forEach( ( handler ) => handler( store, action ) );

	return next( action );
};

export default ( customMiddleware = {} ) =>
	middleware( mergeHandlers( customMiddleware, mergedHandlers ) );