Skip to content

Instantly share code, notes, and snippets.

@robcolburn
Created March 19, 2016 01:12
Show Gist options
  • Save robcolburn/3f537a182931f0b297ac to your computer and use it in GitHub Desktop.
Save robcolburn/3f537a182931f0b297ac to your computer and use it in GitHub Desktop.
function reducer(state, action) {
const store = mastermind(this);
state = combineReducers({
cats: catReducer
}, store);
return state;
}
catReducer(state, action) {
const store = this;
switch (action.type) {
case 'LAUNCH_CAT':
store.dispatch({
delay: 60,
type: 'ROTATE_CAT',
angle: action.angle - 15
})
store.dispatch({
delay: 120,
type: 'ROTATE_CAT',
angle: action.angle + 90
})
return {
...state,
[action.catId]: {
angle: action.angle
}
}
case 'LOAD_CAT':
store.dispatch({
type: 'PROMISE_CAT',
success: 'LOADED_CAT',
failure: 'FAILURE_CAT',
promise: api.get('cat', catId)
})
return {
...state,
[action.catId]: {
loading: true
}
}
}
return state;
}
function mastermind(store) {
return {
...store,
dispatch(action) {
switch (action.type) {
case 'ROTATE_CAT':
setTimeout(() => {
store.dispatch(action)
}, action.delay);
case 'LOAD_CAT':
action.promise.then(
data => store.dispatch({
type: action.success,
data
}),
error => store.dispatch({
type: action.failure,
error
})
)
default:
setImmediate(() => {
store.dispatch(action);
])
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment