Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aaronshaf/react-callbag-subject

Asynchronous pipelines in React using callbags
https://github.com/aaronshaf/react-callbag-subject

callbags

Last synced: 20 days ago
JSON representation

Asynchronous pipelines in React using callbags

Awesome Lists containing this project

README

        

Asynchronous reducer pipelines using [callbags](https://github.com/staltz/callbag-basics).

Try it now on [CodeSandbox](https://codesandbox.io/s/mozmv6vrmp).

## Install

```
npm install react-callbag-subject --save
```

## Pipeline operator

If you don't have the [pipeline operator](https://github.com/tc39/proposal-pipeline-operator) you can use the [pipe function](https://github.com/staltz/callbag-pipe). `foo |> bar()` would instead be `pipe(foo, bar())`.

## Basic usage

```javascript
import { Subject, reducerFromMap, startWith } from "react-callbag-subject";

const reducers = new Map([
["SUBTRACT", (state, amount) => ({ count: state.count - amount })],
["ADD", (state, amount) => ({ count: state.count + amount })],
["MULTIPLY", (state, multiplier) => ({ count: state.count * multiplier })]
]);

const pipeline = actions =>
actions |> reducerFromMap(reducers) |> startWith({ count: 0 });
```

```jsx

{(state, send) => (


send("SUBTRACT", 1)}>Remove 1
send("ADD", 1)}>Add 1
send("MULTIPLY", 2)}>Multiply by 2
{state.count}


)}

```

## Debouncing example

```javascript
import { debounce } from "callbag-debounce";

const pipeline = actions =>
actions
|> debounce(250)
|> reducerFromMap(reducers)
|> startWith({ counter: 1 });
```

## Further reading

* [Callbag basics](https://github.com/staltz/callbag-basics)
* [Callbag wiki](https://github.com/callbag/callbag/wiki)
* [Why we need callbags](https://staltz.com/why-we-need-callbags.html), by André Staltz