An epic is a function that takes two arguments and returns a stream of Redux actions. In MapStore2, epics are used to implement asynchronous operations and complex data flows.
Epic Signature:
const myEpic = (action$, store) => { ... }
action$: An RxJS Observable representing the stream of all Redux actions. Every time an action is dispatched in Redux, it is emitted here.store: A simplified version of the Redux store. It provides the getState() method to access the current application state.
Key Pattern: Actions In, Actions Out
Typically, an epic listens for specific actions on the action$ stream, performs logic (like filtering or state checks), and then returns a new stream that emits actions to be dispatched back to Redux.
Note on ofType: MapStore2 uses redux-observable, which adds the ofType operator to RxJS. This operator allows you to filter the action$ stream for specific action types.
const fetchUserEpic = (action$, store) => action$
.ofType(MAP_CONFIG_LOADED)
.filter(() => isMapLoadConfigurationEnabled(store.getState()))
.map({
type: NOTIFICATION,
message: "Map Loaded"
});