阅读 redux 源码以后,想要加深一下对中间件的理解,因而选择 redux-thunk(2.3.0)这个源码只有十几行的中间件。html
以前 redux 的学习笔记 http://www.javashuo.com/article/p-vqbjvrej-ep.htmlgit
redux 中的 applyMiddleware.jsgithub
export default function applyMiddleware(...middlewares) { return createStore => (...args) => { const store = createStore(...args) let dispatch = () => { throw new Error( `Dispatching while constructing your middleware is not allowed. ` + `Other middleware would not be applied to this dispatch.` ) } const middlewareAPI = { getState: store.getState, dispatch: (...args) => dispatch(...args) } const chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) return { ...store, dispatch } } }
每一个中间件须要传入 store(只有 getState 和 dispatch)和 next(由上一个中间处理过的 dispatch)ajax
在生成 dispatch 的时候 传入的 middlewareAPI 中的 dispatch 是一个只抛出异常的函数,用来提示在建立 dispatch 的时候, 中间件不该该使用 dispatch 。redux
建立后又将 dispatch 赋值为通过中间件生成的函数。这时api
const middlewareAPI = { getState: store.getState, dispatch: (...args) => dispatch(...args) }
中 middlewareAPI.dispatch 就变成了最新的 dispatch 因此在中间件中能够使用 dispatch、app
接下来能够看 redux-thunk 的源码异步
function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; } const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware; export default thunk;
使用时能够是 applyMiddleware(thunk) 或者 applyMiddleware(thunk.withExtraArgument(api)) 函数
若是是默认的 thunk 那么中间件的函数为学习
const thunk = ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch, getState); } return next(action); };
若是是函数 就传入 dispatch 和 getState 不然就执行默认的 next
let store = createStore(reducer, applyMiddleware(thunk)); let action = (dispatch) => { ajax(options).then((res) => { dispatch({ type: 'change', content: res.data }); }) } store.dispatch(action);
这样对于异步的操做就能够在 redux 中使用了~