Last active
March 12, 2016 07:38
-
-
Save 3den/789dcafe13c023f37578 to your computer and use it in GitHub Desktop.
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
import {setEntries, next, vote, INITIAL_STATE} from './core'; | |
// This is a polymorphic aprorach that conforms to all SOLID principles | |
// This reduce is much simpler and will raise an error if the action does not exist | |
export default function reducer(state = INITIAL_STATE, action) { | |
return action.handler(state, action.payload); | |
} | |
// This is the defensive version of the reducer above, if there is an error returns the current state | |
export default function maybeReducer(state = INITIAL_STATE, action) { | |
try { | |
return reducer(state, action); | |
} catch (err) { | |
console.warn(err); | |
return state; | |
} | |
} | |
// you can stack actions the same way with the benefit that a typo on the action function will raise an error | |
const actions = [ | |
{handler: setEntries, payload: ['Trainspotting', '28 Days Later']}, | |
{handler: next}, | |
{handler: vote, payload: 'Trainspotting'}, | |
{handler: vote, payload: '28 Days Later'}, | |
{handler: vote, payload: 'Trainspotting'}, | |
{handler: next} | |
]; | |
// this returns the same think as the reducer-standard.js but raises an error if something goes worng | |
const finalState = actions.reduce(reducer, Map()); | |
// this returns the same think as above but swallow errors | |
const finalState2 = actions.reduce(maybeReducer, Map()); | |
// allows you to create a action on the fly, without modifying the reducer | |
const finalState3 = reducer(finalState, {handler: (state => state.set('isNice', true) }); | |
// Example of a store dispatch | |
store.dispatch({handler: vote, payload: 'Trainspotting'}); |
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
import {setEntries, next, vote, INITIAL_STATE} from './core'; | |
// This is kind of the standard aproach | |
// it violates the open closed and single resposability priciplse by having a huge while loop that knows about all available actions | |
// Also it is hard to create variations of this reducer without duplicationt a lot of code, | |
// the more actions you have the bigger this function becomes (open closed violation). | |
export default function reducer(state = INITIAL_STATE, action) { | |
switch (action.type) { | |
case 'SET_ENTRIES': | |
return setEntries(state, action.payload); | |
case 'NEXT': | |
return next(state); | |
case 'VOTE': | |
return vote(state, action.payload) | |
} | |
// this will swallow errors if the action you try to use dont exist | |
return state; | |
} | |
// this is how people use it for stacking changes | |
const actions = [ | |
{type: 'SET_ENTRIES', payload: ['Trainspotting', '28 Days Later']}, | |
{type: 'NEXT'}, | |
{type: 'VOTE', payload: 'Trainspotting'}, | |
{type: 'VOTE', payload: '28 Days Later'}, | |
{type: 'VOTE', payload: 'Trainspotting'}, | |
{type: 'NEXT'} | |
]; | |
const finalState = actions.reduce(reducer, Map()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment