Skip to content

Instantly share code, notes, and snippets.

@keeth
Last active October 9, 2021 16:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeth/96b97c6cf890187d0b17d98ce9d4fbd4 to your computer and use it in GitHub Desktop.
Save keeth/96b97c6cf890187d0b17d98ce9d4fbd4 to your computer and use it in GitHub Desktop.
Pre-process (de-batch, filter) actions for redux saga middleware, without affecting the reducers
const noop = () => {};
const actionsToIgnore = new Set(['SOME_ACTION', 'SOME_OTHER_ACTION']);
function createWrappedSagaMiddleware() {
const delegate = createSagaMiddleware();
const sagaMiddleware = props => next => {
const actionHandler = delegate(props)(noop);
return action => {
// send to reducers, keep the result
const result = next(action);
// expand batch actions
const actions = action.type === 'BATCHING_REDUCER.BATCH' ? action.payload : [action];
// trigger saga side-effects for actions we care about, ignore return value
actions
.filter(({type}) => !actionsToIgnore.has(type))
.forEach(actionHandler);
// pass new state back up the middleware chain
return result;
};
};
sagaMiddleware.run = delegate.run;
return sagaMiddleware;
}
@keeth
Copy link
Author

keeth commented Aug 10, 2016

A way to pre-process actions for redux-saga middleware, without affecting the action stream that is sent to the reducers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment