Last active
October 9, 2021 16:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A way to pre-process actions for redux-saga middleware, without affecting the action stream that is sent to the reducers.