Created
July 31, 2015 21:08
-
-
Save hleumas/bf710769a4cf6dec76b4 to your computer and use it in GitHub Desktop.
Compose stores
This file contains 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 {fromJS} from 'immutable' | |
import userActions from './userActions' | |
const userInitialState = fromJS({ | |
logged: false | |
}) | |
function userStore(state = userInitialState, action, payload) { | |
switch (action) { | |
case userActions.loggedIn: | |
return state.set('logged', true) | |
case userActions.loggedOut: | |
return state.set('logged', false) | |
} | |
return state | |
} | |
import petActions from './petActions' | |
const petInitialState = fromJS({ | |
pets: [] | |
}) | |
function petStore(state = petInitialState, action, payload) { | |
switch (action) { | |
case.petActions.buyPet: | |
return state.update('pets', (pets) => pets.push(payload)) | |
case petActions.sellAllPets: | |
return state.set('pets', List()) | |
} | |
return state | |
} | |
// And now simply compose the stores. | |
// No need for waitFor. | |
const initialState = fromJS({}) | |
function store(state = initialState, action, payload) { | |
return state | |
.updateIn('user', (s) => userStore(s, action, payload)) | |
.updateIn('pet', (s) => petStore(s, action, payload)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment