Last active
September 15, 2017 16:10
-
-
Save DobrinGanev/a366ba757600fc24146f53343cba98c2 to your computer and use it in GitHub Desktop.
Redux pattern with RxJS
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
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