redux中的applyMiddleware详解
1 | // applyMiddleware.js |
applyMiddleware
返回了一个createStore
方法,这个方法和createStore.js
中的createStore方法比起来到底强在哪里?
答案 : 增强的dispatch方法!
最终返回的store
中的dispatch
方法是将原始的裸体store.dispatch
经过每一层中间件包裹形成的,就像穿衣服一样,下面将详细介绍
中间件
中间件的样子就像下面这样,传入store,返回一个dispatch方法
1 | const middlewareName = store => next => action => {} |
applyMiddleware
核心代码如下: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// 创建一个store
const store = createStore(...args)
// 构造一个像中间件中传递的store
const middlewareAPI = {
getState: store.getState,
dispatch: (...args) => dispatch(...args)
}
// const m1 = store => next => action => {}
// const m2 = store => next => action => {}
// const m3 = store => next => action => {}
// const middlewares = [m1, m2, m3]
const chain = middlewares.map(middleware => middleware(middlewareAPI))
// 返回一个列表, 内容如下
// 假设:
// dis1 = next => action => {}
// dis2 = next => action => {}
// dis3 = next => action => {}
// chain = [dis1, dis2, dis3]
// 关于compose可在另一篇文章中查看
// https://wstreet.github.io/blog/2019/04/02/redux%E4%B8%ADcompose%E8%AF%A6%E8%A7%A3/
dispatch = compose(...chain)(store.dispatch)
// compose(...chain) ===> (...args) => dis1(dis2(dis3(...args)))
// dispatch = compose(...chain)(store.dispatch) ===> (...args) => dis1(dis2(dis3(...args)))(store.dispatch) ===> dis1(dis2(dis3(store.dispatch)))
代码分析到这里一目了然,最后的dispatch是执行中间件列表生成的复杂dispatch