試験中…
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/** * redux-thunque.js * * https://www.npmjs.com/package/redux-thunk * https://github.com/reduxjs/redux-thunk * https://kitak.hatenablog.jp/entry/2014/12/01/234759 * */ // import { Middleware, Action, AnyAction } from "redux"; class Queue { constructor() { const queue = {}; this.add = function (type, payload, dispatch, getState, extraArgument) { if (!queue[type]) { queue[type] = Promise.resolve(true); } const f = function () { var promise = new Promise(function (resolve, reject) { payload(dispatch, getState, extraArgument, resolve, reject); // append resolve and reject }); return promise; }; queue[type] = queue[type].then(f); // .catch(function (reason) { ... }); }; this.remove = function (type) { delete queue[type]; // release array index }; } } function createThunkMiddleware(extraArgument) { const queue = new Queue(); return ({ dispatch, getState }) => next => action => { if (typeof action === "function") { return action(dispatch, getState, extraArgument); } else if (typeof action.payload === "function") { return queue.add(action.type, action.payload, dispatch, getState, extraArgument); } return next(action); }; } const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware; export default thunk; |