/
sagas.ts
54 lines (44 loc) · 1.49 KB
/
sagas.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 { ActionBundle, DataFetchFunction, PayloadType, ReduxAction, SagaYields, ReduxSaga, ReduxSagaWatcher } from "./baseTypes";
import { put, call, take, delay, race, } from "redux-saga/effects";
//TOdo - fix signature
export function createDefaultSagaWatcher(actions: ActionBundle, saga: ReduxSaga<any, any>): ReduxSagaWatcher {
return function* () {
console.log("watcher");
while (true) {
const action = yield take(actions.REQUEST)
const result = yield race({
task: call(saga, action),
cancel: take(actions.CANCEL)
});
}
}
}
export function createGenericApiSaga<P extends PayloadType, Q extends PayloadType>(
actions: ActionBundle,
apiCall: DataFetchFunction<P, Q>,
) : ReduxSaga<P, Q> {
const saga = function* (action: ReduxAction<P>) {
console.log(action);
try {
const result: Q = yield call(apiCall, action.payload);
yield put({
type: actions.UPDATE,
payload: result
})
yield put({
type: actions.SUCCESS,
payload: result
})
}
catch (err) {
yield put({
type: actions.FAILURE,
payload: {
message: err.message,
rest: err
}
});
}
} as unknown as ReduxSaga<P,Q> //todo
return saga;
}