Last active
October 8, 2017 14:50
-
-
Save torgeir/32d4e68e14089df0fd0b2b62314ce1c0 to your computer and use it in GitHub Desktop.
redux-observable example
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('rxjs/Rx'); | |
const { createStore, applyMiddleware } = require('redux'); | |
const { createEpicMiddleware, combineEpics } = require('redux-observable'); | |
const inc = n => n + 1; | |
const reducer = function ({ count } = { count: 0 }, { type } = {}) { | |
switch (type) { | |
case "add": | |
return { count: inc(count) }; | |
case "add async": | |
return { count: inc(count) }; | |
} | |
return { count }; | |
}; | |
const handleAdd = function (action$) { | |
return action$ | |
.ofType("add") | |
.delay(2000) | |
.mapTo({ type: "add async" }); | |
}; | |
const logger = function (action$, { getState }) { | |
return action$ | |
.forEach(_ => console.log(getState())); | |
}; | |
const store = createStore( | |
reducer, | |
applyMiddleware( | |
createEpicMiddleware( | |
combineEpics(handleAdd, logger)))); | |
store.dispatch({ type: "add" }); | |
// { count: 1 } | |
// .. 2 second .. | |
// { count: 2 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment