Skip to content

Instantly share code, notes, and snippets.

@oskarhane
Created January 29, 2017 21:18
Show Gist options
  • Save oskarhane/dbfcf2ea8b73061b92e91c1ba3a56262 to your computer and use it in GitHub Desktop.
Save oskarhane/dbfcf2ea8b73061b92e91c1ba3a56262 to your computer and use it in GitHub Desktop.
Use applyReduxMiddleware
import { getBus, applyReduxMiddleware } from 'suber'
import { createEpicMiddleware } from 'redux-observable'
import { ajax } from 'rxjs/observable/dom/ajax'
// Get the bus
const bus = getBus()
// Declare the redux-observable middleware
// Listen on the 'FETCH_USER' channel and look for github username
// in the 'payload' property.
// Call the Github API and send the response on the 'FETCH_USER_FULFILLED' channel
const ghUserEpic = action$ =>
action$.ofType('FETCH_USER')
.mergeMap(action =>
ajax.getJSON(`https://api.github.com/users/${action.payload}`)
.map(response => ({ type: 'FETCH_USER_FULFILLED', response }))
)
const epicMiddleware = createEpicMiddleware(ghUserEpic)
// Apply the middleware to suber
applyReduxMiddleware(epicMiddleware)
// Listen for future messages on the 'FETCH_USER_FULFILLED' channel
bus.take('FETCH_USER_FULFILLED', ({response}) => console.log(response))
// Send the wanted username as 'payload' on the 'FETCH_USER' channel
bus.send('FETCH_USER', {payload: 'oskarhane'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment