Skip to content

Instantly share code, notes, and snippets.

@DobrinGanev
Last active September 15, 2017 16:10
Show Gist options
  • Save DobrinGanev/a366ba757600fc24146f53343cba98c2 to your computer and use it in GitHub Desktop.
Save DobrinGanev/a366ba757600fc24146f53343cba98c2 to your computer and use it in GitHub Desktop.
Redux pattern with RxJS
const Rx = require('rx')
const action$ = new Rx.Subject()
const initState = 0
const reducer = (state, action) => {
switch (action.type) {
case 'INC':
return state + action.payload
default:
return state
}
}
const store$ = action$
.startWith(initState)
.scan(reducer)
const dispatcher = (func) => (...args) => {
action$.onNext(func(...args))
}
const inc = dispatcher((payload) => ({
type: 'INC',
payload
}));
store$.subscribe((state) => {
console.log(state)
})
setInterval(() => {
inc(1)
}, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment