Skip to content

Instantly share code, notes, and snippets.

@hleumas
Created July 31, 2015 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hleumas/bf710769a4cf6dec76b4 to your computer and use it in GitHub Desktop.
Save hleumas/bf710769a4cf6dec76b4 to your computer and use it in GitHub Desktop.
Compose stores
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