-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapply-middleware.js
More file actions
26 lines (24 loc) · 828 Bytes
/
apply-middleware.js
File metadata and controls
26 lines (24 loc) · 828 Bytes
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
/*
=> api - middleware api
=> next - next/dispatch function
=> action - action object
*/
const applyMiddleware = (...middlewares) => {
return middlewareAPI => {
const originalDispatch = (action) => {
middlewareAPI.dispatch(action);
};
// `api` is middlewareAPI
const wrapMiddleware = middlewares.map(middleware => {
return middleware(middlewareAPI);
});
// apply middleware order by first
const last = wrapMiddleware[wrapMiddleware.length - 1];
const rest = wrapMiddleware.slice(0, -1);
const roundDispatch = rest.reduceRight((oneMiddle, middleware) => {
return middleware(oneMiddle);
}, last);
return roundDispatch(originalDispatch);
};
};
export default applyMiddleware;