createSessionReducer
Handy for creating the reducer that repacks and pushes the Session to Redux Store State.
Note this requires the input Action to fit the description in: ActionObject
function createSessionReducer (actionType, sessionObject)
Usage:
const session = { data: 'data' }
const reducer = createSessionReducer('reducer:sample:update', session) // Repack Session and Update the Store State
const service = function * ({ store, req, res }) {
yield req('service:sample:init')
while (true) {
yield res({ type: 'reducer:sample:update', payload: session }) // Send the Update Action with latest Session
const action = yield req([ 'service:sample:one', 'service:sample:two' ])
// codes with access to action, session, store
}
}
The (really simple) source code:
function createSessionReducer (actionType, sessionObject) {
const initialState = { ...sessionObject, _tick: 0 }
return (state = initialState, action) => {
if (action.type === actionType) return { ...state, ...action.payload, _tick: state._tick + 1 }
else return state
}
}