import compose from "./compose";
/**
* Creates a store enhancer that applies middleware to the dispatch method
* of the Redux store. This is handy for a variety of tasks, such as expressing
* asynchronous actions in a concise manner, or logging every action payload.
*
* See `redux-thunk` package as an example of the Redux middleware.
*
* Because middleware is potentially asynchronous, this should be the first
* store enhancer in the composition chain.
*
* Note that each middleware will be given the `dispatch` and `getState` functions
* as named arguments.
*
* @param {...Function} middlewares The middleware chain to be applied.
* @returns {Function} A store enhancer applying the middleware.
*/
// 中间件
export default function applyMiddleware(...middlewares) {
return createStore => (reducer, preloadedState, enhancer) => {
const store = createStore(reducer, preloadedState, enhancer);
let dispatch = store.dispatch;
let chain = [];
const middlewareAPI = {
getState: store.getState,
dispatch: action => dispatch(action)
};
chain = middlewares.map(middleware => middleware(middlewareAPI));
dispatch = compose(...chain)(store.dispatch);
return {
...store,
dispatch
};
};
}
applyMiddleware.js
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 因为 js 是由浏览器来解释执行的,因此这里有一个问题,不同类型的浏览器可能对 js 的支持不一样。 js 的开发...
- HTML 学习笔记 May 9,2017 js运行原理、js开发工具介绍、js程序(hello)、js基本语法、j...
- HTML 学习笔记 May 12,2017 JS 面向对象三大特征(封装、继承、多态)、多态经典案例、补讲闭包细节...
- HTML 学习笔记 May 13,2017 js事件驱动机制、js事件分类、js访问css技术、js事件驱动机制深...
- 结合方式1 结合方式2 js的基本语法 二ECMAScript 基础2.1.ECMAScript 语法变量区分大小...