Giter VIP home page Giter VIP logo

meatball's Introduction

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

meatball's People

Contributors

rametta avatar vageez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.