2.1.2 • Published 5 years ago

meatball v2.1.2

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
5 years ago

npm PRs Welcome Open Source Love License

🍝 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))
)

Real example

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago