Sha256: 7b833bb5ec6eced3d90ad047929bf493b1d03cb998fe72f78e34ada09fb83de2

Contents?: true

Size: 1.33 KB

Versions: 17

Compression:

Stored size: 1.33 KB

Contents

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) => (...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,
    }
  }
}

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
isomorfeus-redux-4.2.0 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.18 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.17 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.16 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.15 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.14 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.13 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.12 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.11 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.10 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.9 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.8 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.7 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.6 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.5 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.4 node_modules/redux/src/applyMiddleware.js
isomorfeus-redux-4.1.3 node_modules/redux/src/applyMiddleware.js