import { BehaviorSubject } from 'rxjs' ; import { Action } from 'redux' ; import { combineEpics , Epic , createEpicMiddleware , EpicMiddleware , } from 'redux-observable' ; import { mergeMap , catchError } from 'rxjs/operators' ; import { RootEpicDependency , RootEpic , RootStoreState } from './types' ; export function createEpicManager ( dependencies = { } , ... epics ) { const addedEpics = [ ] ; const epic$ = new BehaviorSubject ( combineEpics ( ... epics ) ) ; const addEpic = ( epic ) => { if ( addedEpics . includes ( epic ) ) { return ; } addedEpics . push ( epic ) ; epic$ . next ( epic ) ; } ; const rootEpic = ( action$ , state$ ) => epic$ . pipe ( mergeMap ( ( epic ) => epic ( action$ , state$ , dependencies ) . pipe ( catchError ( ( _ , source ) => source ) ) ) ) ; const epicMiddleware = createEpicMiddleware < Action , Action , RootStoreState , RootEpicDependency > ( ) ; return { epic$ , rootEpic , epicMiddleware , addEpic } ; }