thunk.js (835B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 /** 7 * A middleware that allows thunks (functions) to be dispatched 8 * If it's a thunk, it is called with an argument that will be an object 9 * containing `dispatch` and `getState` properties, plus any additional properties 10 * defined in the `options` parameters. 11 * This allows the action to create multiple actions (most likely asynchronously). 12 */ 13 function thunk(options = {}) { 14 return function ({ dispatch, getState }) { 15 return next => action => { 16 return typeof action === "function" 17 ? action({ dispatch, getState, ...options }) 18 : next(action); 19 }; 20 }; 21 } 22 23 exports.thunk = thunk;