-
Notifications
You must be signed in to change notification settings - Fork 14
/
combineEpics.ts
54 lines (45 loc) · 1.44 KB
/
combineEpics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { mergeArray } from '@most/core'
import { findIndex, map } from '@most/prelude'
import {
Action,
AnyAction,
Dispatch,
Middleware,
MiddlewareAPI,
StoreEnhancer,
} from 'redux'
import { Stream } from '@most/types'
import { Epic } from './types'
export declare function combineEpics_<S, A extends Action>(
epicsArray: Epic<S, A>[],
): Epic<S, A>
export const combineEpics = <S, A extends Action>(
epicsArray: Epic<S, A>[],
): Epic<S, A> => (actionsStream, middlewareApiOrStateStream) => {
if (!epicsArray || !Array.isArray(epicsArray)) {
throw new TypeError('You must provide an array of Epics to combineEpics.')
}
if (epicsArray.length < 1) {
throw new TypeError(
'The array passed to combineEpics must contain at least one Epic.',
)
}
const callEpic = (epic: Epic<S, A>) => {
if (typeof epic !== 'function') {
throw new TypeError(
'The array passed to combineEpics must contain only Epics (functions).',
)
}
const out = epic(actionsStream, middlewareApiOrStateStream)
if (!out || !out.source) {
const epicIdentifier = epic.name
? `named ${epic.name}`
: `at index ${findIndex(epic, epicsArray)} of the passed in array`
throw new TypeError(
`All Epics in the array provided to combineEpics must return a stream. Check the return value of the Epic ${epicIdentifier}.`,
)
}
return out
}
return mergeArray(map(callEpic, epicsArray))
}