subspace-docs/redux-observable.md

34 lines
1.2 KiB
Markdown
Raw Normal View History

2019-09-06 09:28:26 -04:00
# redux-observable
2019-09-11 10:07:10 -04:00
[redux-observables](https://redux-observable.js.org/) can be used to manage side effects via `Epics` (their core primitive to receive and create stream of actions). **Subspace** can be configured inside these epics.
2019-09-06 09:28:26 -04:00
It's recommended to compose these epics by using [mergeMap](https://www.learnrxjs.io/operators/transformation/mergemap.html) or [switchMap](https://www.learnrxjs.io/operators/transformation/switchmap.html) operators.
2019-09-11 10:07:10 -04:00
Here's an example on how to use **Subspace** to subscribe to an Event when the action `SOME_ACTION` is dispatched, and then it will trigger `myAction` when the observable emits a value.
2019-09-06 09:28:26 -04:00
```js
// ...
const myEpic = action$ =>
action$.pipe(
ofType("SOME_ACTION"), // Execute when the action type is 'INIT'
switchMap(action =>
2019-09-27 15:24:05 -04:00
subspace
2019-09-06 09:28:26 -04:00
.trackEvent(MyContract, "MyEventName", { filter: {}, fromBlock: 1})
.pipe(
map(myAction) // Trigger redux action: MY_ACTION with the eventData
)
)
);
// ...
```
2019-09-06 09:53:21 -04:00
::: tip
2019-09-27 15:24:05 -04:00
An example is available in [Github](https://github.com/status-im/subspace/tree/master/examples/redux-observable)
2019-09-06 09:53:21 -04:00
:::
2019-09-06 09:28:26 -04:00
#### Further read
- [Epics](https://redux-observable.js.org/docs/basics/Epics.html)