forked from redux-observable/redux-observable
/
typings.ts
218 lines (187 loc) · 6.83 KB
/
typings.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { expect } from 'chai';
import { createStore, applyMiddleware, MiddlewareAPI, Action } from 'redux';
import { Observable } from 'rxjs/Observable';
import { ajax } from 'rxjs/observable/dom/ajax';
import { map } from 'rxjs/operators/map';
import { asap } from 'rxjs/scheduler/asap';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/mapTo';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import { createEpicMiddleware, Epic, combineEpics,
EpicMiddleware, ActionsObservable, ofType } from '../';
interface State {
foo: string
}
interface FluxStandardAction {
type: string | symbol | any;
payload?: any;
error?: boolean | any;
meta?: any
}
interface Dependencies {
func(value: string): string;
}
const epic1: Epic<FluxStandardAction, State> = (action$, store) =>
action$.ofType('FIRST')
.mapTo({
type: 'first',
payload: store.getState()
});
const epic2: Epic<FluxStandardAction, State> = (action$, store) =>
action$.ofType('SECOND', 'NEVER')
.mapTo('second')
.mergeMap(type => Observable.of({ type }));
const epic3: Epic<FluxStandardAction, State> = action$ =>
action$.ofType('THIRD')
.mapTo({
type: 'third'
});
const epic4: Epic<FluxStandardAction, State> = () =>
Observable.of({
type: 'fourth'
});
const epic5: Epic<FluxStandardAction, State> = (action$, store) =>
action$.ofType('FIFTH')
.mergeMap(({ type, payload }) => Observable.of({
type: 'fifth',
payload
}));
const epic6: Epic<FluxStandardAction, State> = (action$, store) =>
action$.ofType('SIXTH')
.map(({ type, payload }) => ({
type: 'sixth',
payload
}));
const epic7: Epic<FluxStandardAction, State, Dependencies> = (action$, store, dependencies) =>
action$.ofType('SEVENTH')
.map(({ type, payload}) => ({
type: 'seventh',
payload: dependencies.func(payload)
}));
const epic8: Epic<FluxStandardAction, State, Dependencies> = (action$, store, dependencies) =>
action$.pipe(
ofType('EIGHTH'),
map(({ type, payload }) => ({
type: 'eighth',
payload
}))
)
const rootEpic1: Epic<FluxStandardAction, State> = combineEpics<FluxStandardAction, State>(epic1, epic2, epic3, epic4, epic5, epic6, epic7, epic8);
const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6, epic7, epic8);
const dependencies: Dependencies = {
func(value: string) { return `func-${value}`}
}
const epicMiddleware1: EpicMiddleware<FluxStandardAction, State> = createEpicMiddleware<FluxStandardAction, State>(rootEpic1, { dependencies });
const epicMiddleware2 = createEpicMiddleware(rootEpic2, { dependencies });
interface CustomEpic<T extends Action, S, U> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>, api: U): Observable<T>;
}
const customEpic: CustomEpic<FluxStandardAction, State, number> = (action$, store, some) =>
action$.ofType('CUSTOM1')
.map(({ type, payload }) => ({
type: 'custom1',
payload
}));
const customEpic2: CustomEpic<FluxStandardAction, State, number> = (action$, store, some) =>
action$.ofType('CUSTOM2')
.map(({ type, payload }) => ({
type: 'custom2',
payload
}));
const customEpicMiddleware: EpicMiddleware<FluxStandardAction, State> = createEpicMiddleware<FluxStandardAction, State>(rootEpic1, {
dependencies: { getJSON: ajax.getJSON }
});
const combinedCustomEpics = combineEpics<CustomEpic<FluxStandardAction, State, number>>(customEpic, customEpic2);
const reducer = (state: Array<FluxStandardAction> = [], action: FluxStandardAction) => state.concat(action);
const store = createStore(
reducer,
applyMiddleware(epicMiddleware1, epicMiddleware2)
);
epicMiddleware1.replaceEpic(rootEpic2);
epicMiddleware2.replaceEpic(rootEpic1)
store.dispatch({ type: 'FIRST' });
store.dispatch({ type: 'SECOND' });
store.dispatch({ type: 'FIFTH', payload: 'fifth-payload' });
store.dispatch({ type: 'SIXTH', payload: 'sixth-payload' });
store.dispatch({ type: 'SEVENTH', payload: 'seventh-payload' });
store.dispatch({ type: 'EIGHTH', payload: 'eighth-payload' });
expect(store.getState()).to.deep.equal([
{ "type": "@@redux/INIT" },
{ "type": "fourth" },
{ "type": "fourth" },
{ "type": "@@redux-observable/EPIC_END" },
{ "type": "fourth" },
{ "type": "@@redux-observable/EPIC_END" },
{ "type": "fourth" },
{ "type": "FIRST" },
{ "type": "first",
"payload": [
{ "type": "@@redux/INIT" },
{ "type": "fourth" },
{ "type": "fourth" },
{ "type": "@@redux-observable/EPIC_END" },
{ "type": "fourth" },
{ "type": "@@redux-observable/EPIC_END" }
]
},
{ "type": "first",
"payload": [
{ "type": "@@redux/INIT" },
{ "type": "fourth" },
{ "type": "fourth" },
{ "type": "@@redux-observable/EPIC_END" }
]
},
{ "type": "SECOND" },
{ "type": "second" },
{ "type": "second" },
{ "type": "FIFTH", "payload": "fifth-payload" },
{ "type": "fifth", "payload": "fifth-payload" },
{ "type": "fifth", "payload": "fifth-payload" },
{ "type": "SIXTH", "payload": "sixth-payload" },
{ "type": "sixth", "payload": "sixth-payload" },
{ "type": "sixth", "payload": "sixth-payload" },
{ "type": "SEVENTH", "payload": "seventh-payload" },
{ "type": "seventh", "payload": "func-seventh-payload" },
{ "type": "seventh", "payload": "func-seventh-payload" },
{ "type": "EIGHTH", "payload": "eighth-payload" },
{ "type": "eighth", "payload": "eighth-payload" },
{ "type": "eighth", "payload": "eighth-payload" }
]);
const input$ = Observable.create(() => {});
const action$1: ActionsObservable<FluxStandardAction> = new ActionsObservable<FluxStandardAction>(input$);
const action$2: ActionsObservable<FluxStandardAction> = ActionsObservable.of<FluxStandardAction>({ type: 'SECOND' }, { type: 'FIRST' }, asap);
const action$3: ActionsObservable<FluxStandardAction> = ActionsObservable.from<FluxStandardAction>([{ type: 'SECOND' }, { type: 'FIRST' }], asap);
{
// proper type narrowing
const enum ActionTypes {
One = 'ACTION_ONE',
Two = 'ACTION_TWO',
}
const doOne = (myStr: string): One => ({type: ActionTypes.One, myStr})
const doTwo = (myBool: boolean): Two => ({type: ActionTypes.Two, myBool})
interface One extends Action {
type: ActionTypes.One
myStr: string
}
interface Two extends Action {
type: ActionTypes.Two
myBool: boolean
}
type Actions = One | Two
// Explicitly set generics fixes the issue
const epic = (action$: ActionsObservable<Actions>) =>
action$
.ofType<One>(ActionTypes.One)
// action is correctly narrowed to One
.map((action) => { console.log(action.myStr) })
// Explicitly set generics fixes the issue
const epicLettable = (action$: ActionsObservable<Actions>) =>
action$.pipe(
ofType<Actions,One>(ActionTypes.One),
// action is correctly narrowed to One
map((action) => { console.log(action.myStr) })
);
}
console.log('typings.ts: OK');