Redux-Entry
Middleware for Redux.
Definition
Entry is a function that:
- Target one type of
action
- Will be called with
(store, action)
, after theaction
is dispatched, but before it reaches the store reducer - Can block current dispatched
action
byreturn true
- Has access to
store
. Hence you can access current store state bystore.getState()
, or dispatch someaction
bystore.dispatch
. From there we have some feature like:- Block and Redirect
action
to another action type - Dispatch multiple
action
from one Entry - dispatch to another Entry (beware of dead loops)
- Block and Redirect
Usage
ReduxEntry
const reducer = state // some sample reducer const middleware: reduxEntryMiddleware setEntry setEntryMap = // get ReduxEntry instance // add reduxEntry to middlewareconst store = // define some entryFunctionconst entryFunction0 = {}const entryFunction1 = {}// and set // or pack them upconst entryMap = 'action-type-to-precess-0': entryFunction0 'action-type-to-precess-1': entryFunction1// and set
createStateStore
& createStateStoreReducer
These are packed handy functions to create mini-state-store, if needed to.
mini-state-store can be used to offload Redux store reducer codes.
Suppose we have a store state like this:
const storeState = 'mini-state-a': {} 'mini-state-b': {} 'mini-state-c': {}
and for each mini state, we have reducers like:
const miniStateReducer = { if actiontype === 'mini-state:set-key-0' return ...state key0: 0 else if actiontype === 'mini-state:set-key-1' return ...state key1: 1 else if actiontype === 'mini-state:set-key-2' return ...state key2: 2 else return state}
We might want to 'reduce' the reducer code to a single merge operation:
const miniStateReducer = { if actiontype === 'mini-state:update' return ...state ...actionpayload else return state}
...and put the each reduce action to one entryFunction:
const entryMap = { const miniState = 'mini-state-a' } { const miniState = 'mini-state-a' } { const miniState = 'mini-state-a' }
And with createStateStore
and createStateStoreReducer
, we get a mini-state-store:
const initialState = {}const stateStore = const getState setState wrapEntry = stateStoreconst reducer = const entryMap = 'mini-state:set-key-0': 'mini-state:set-key-1': 'mini-state:set-key-2':
License
MIT. Issues and Pull Requests are welcomed.