summaryrefslogtreecommitdiff
blob: 1403b8084f1dc07731475e20a36676b743390111 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * External dependencies
 */
import refx from 'refx';
import { flowRight } from 'lodash';

/**
 * Internal dependencies
 */
import effects from './effects';

/**
 * Applies the custom middlewares used specifically in the Publicize extension.
 *
 * @param {Object} store Store Object.
 *
 * @return {Object} Update Store Object.
 */
export default function applyMiddlewares( store ) {
	const middlewares = [ refx( effects ) ];

	let enhancedDispatch = () => {
		throw new Error(
			'Dispatching while constructing your middleware is not allowed. ' +
				'Other middleware would not be applied to this dispatch.'
		);
	};
	let chain = [];

	const middlewareAPI = {
		getState: store.getState,
		dispatch: ( ...args ) => enhancedDispatch( ...args ),
	};
	chain = middlewares.map( middleware => middleware( middlewareAPI ) );
	enhancedDispatch = flowRight( ...chain )( store.dispatch );

	store.dispatch = enhancedDispatch;

	return store;
}