subspace-docs/redux-observables.md
2019-09-03 14:40:19 -04:00

1.0 KiB

redux-observables

Phoenix can be configured in epics created with redux-observables. It's recommended to compose these epics by using mergeMap or switchMap operators

import {mergeMap, map} from 'rxjs/operators';

const rootEpic = action$ =>
  action$.pipe(
    ofType("INIT"),  // Execute when the action type is 'INIT'
    mergeMap(action =>
      eventSyncer
        .trackEvent(MyContract, "MyEventName", { filter: {}, fromBlock: 1})
        .pipe(
          map(eventData => myAction(eventData)) // Trigger redux action: MY_ACTION
        )
    )
  );



// Read more about epics here: https://redux-observable.js.org/docs/basics/Epics.html
const rootReducer = (state = {}, action) => { /* TODO */ };

const epicMiddleware = createEpicMiddleware();

const store = createStore(rootReducer, applyMiddleware(epicMiddleware));

epicMiddleware.run(rootEpic);