meatball
v2.1.2
Published
Future based redux side effects
Downloads
24
Maintainers
Readme
🍝 Meatball
Future based redux side effects
Alternative to rxjs and redux-observable
Install
yarn add meatball
Meatball has a peer dependency on fluture
yarn add fluture
Usage examples
Listen to any redux action, perform side effect, return a new redux action to be fired
// epics.js
import { searchRes, searchErr } from './reducer'
import { tryP, after } from 'fluture'
// Simple example
const simpleEpic = {
type: 'SUBMIT_SEARCH', // listen for this action
do: ({ payload }) => tryP(() => fetch(payload)) // fetch async data
.map(res => res.json())
.map(data => searchRes(data)) // redux action to save data
.mapRej(e => searchErr(e)) // redux action for handling error
}
// Return multiple actions with an array
const multipleEpic = {
type: 'SUBMIT_SEARCH', // listen for this action
do: ({ payload }) => tryP(() => fetch(payload)) // fetch async data
.map(res => res.json())
.map(data => [searchRes(data), clearSidebar()]) // multiple actions
}
// Complex example
const complexEpic = {
type: 'SUBMIT_SEARCH', // listen for this action
latest: true, // Like rxjs switchMap, cancels previous action if not resolved
do: ({ payload }) => after(200, payload) // delay fetching data for 200ms
.chain(text => tryP(() => fetch(text)))
.map(res => res.json())
.map(data => [searchRes(data), clearSidebar()]) // multiple actions
.mapRej(searchErr)
}
// Debounce example
const debounceEpic = {
type: 'FILTER_SOMETHING', // listen for this action
debounce: 1000, // debounce this action call for 1s
do: ({ payload }) => tryP(() => fetch(payload)) // fetch async data
.map(res => res.json())
.map(data => filterRes(data)) // redux action to save data
.mapRej(filterErr)) // redux action for handling error
}
export default [simpleEpic, multipleEpic, complexEpic, debounceEpic]
// index.js
import meatball from 'meatball'
import epics from './epics'
const store = createStore(
reducers,
applyMiddleware(meatball(epics))
)