MCPcopy
hub / github.com/redux-observable/redux-observable / createEpicMiddleware

Function createEpicMiddleware

src/createEpicMiddleware.ts:22–114  ·  view source on GitHub ↗
(options: Options<D> = {})

Source from the content-addressed store, hash-verified

20}
21
22export function createEpicMiddleware<
23 T extends Action,
24 O extends T = T,
25 S = void,
26 D = any
27>(options: Options<D> = {}): EpicMiddleware<T, O, S, D> {
28 // This isn't great. RxJS doesn't publicly export the constructor for
29 // QueueScheduler nor QueueAction, so we reach in. We need to do this because
30 // we don't want our internal queuing mechanism to be on the same queue as any
31 // other RxJS code outside of redux-observable internals.
32 const QueueScheduler: any = queueScheduler.constructor;
33 const uniqueQueueScheduler: typeof queueScheduler = new QueueScheduler(
34 (queueScheduler as any).schedulerActionCtor
35 );
36
37 if (process.env.NODE_ENV !== 'production' && typeof options === 'function') {
38 throw new TypeError(
39 'Providing your root Epic to `createEpicMiddleware(rootEpic)` is no longer supported, instead use `epicMiddleware.run(rootEpic)`\n\nLearn more: https://redux-observable.js.org/MIGRATION.html#setting-up-the-middleware'
40 );
41 }
42
43 const epic$ = new Subject<Epic<T, O, S, D>>();
44 let store: MiddlewareAPI<Dispatch<any>, S>;
45
46 const epicMiddleware: EpicMiddleware<T, O, S, D> = _store => {
47 if (process.env.NODE_ENV !== 'production' && store) {
48 // https://github.com/redux-observable/redux-observable/issues/389
49 warn(
50 'this middleware is already associated with a store. createEpicMiddleware should be called for every store.\n\nLearn more: https://goo.gl/2GQ7Da'
51 );
52 }
53 store = _store;
54 const actionSubject$ = new Subject<T>();
55 const stateSubject$ = new Subject<S>();
56 const action$ = actionSubject$
57 .asObservable()
58 .pipe(observeOn(uniqueQueueScheduler));
59 const state$ = new StateObservable(
60 stateSubject$.pipe(observeOn(uniqueQueueScheduler)),
61 store.getState()
62 );
63
64 const result$ = epic$.pipe(
65 map(epic => {
66 const output$ = epic(action$, state$, options.dependencies!);
67
68 if (!output$) {
69 throw new TypeError(
70 `Your root Epic "${epic.name ||
71 '<anonymous>'}" does not return a stream. Double check you\'re not missing a return statement!`
72 );
73 }
74
75 return output$;
76 }),
77 mergeMap(output$ =>
78 from(output$).pipe(
79 subscribeOn(uniqueQueueScheduler),

Callers 1

Calls 1

warnFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…