Skip to content

Instantly share code, notes, and snippets.

@vojtatranta
Created October 2, 2016 21:53
Show Gist options
  • Save vojtatranta/e4058b6a5b2cafea811155a2403b6844 to your computer and use it in GitHub Desktop.
Save vojtatranta/e4058b6a5b2cafea811155a2403b6844 to your computer and use it in GitHub Desktop.
const createDispatch = (state, doThingsWithUpdatedState) => {
return (executeChange) => {
const newState = executeChange(state)
return doThingsWithUpdatedState(newState, createDispatch(newState, doThingsWithUpdatedState))
}
}
const initialState = { counter: 0 }
const dispatch = createDispatch(initialState, (newState, dispatch) => {
document.write(newState.counter)
setTimeout(() => {
dispatch(({ counter }) => ({ counter: ++counter }))
}, 1000)
})
// start it!
dispatch(state => state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment