trying to refactor the saga code into rxjs epic. It's fetching sessions from the server
yield race([
call(fetchSessions, sessionsList), //complete retrieving sessions
call(sessionWatchdog, 20000), //or timeout with 20 seconds of inactivity
]);
function* fetchSessions(list: Array<number>): Generator<*, *, *> {
console.log('FETCH ALL', list.length);
for (let id of list) {
yield fork(fetchSession, id);
}
}
function* sessionWatchdog(duration: number): Generator<*, *, *> {
while (true) {
let { retrieved, timeout } = yield race({
retrieved: take('SESSION_RETRIEVED'),
timeout: delay(duration),
});
if (timeout) {
return 'TIMEOUT';
}
}
}
The fetch session is an async function that retrieves a single session. I'm not sure how to make sure to make an equivalent epic. After each session is fetched need to make sure it was retrieved or timeout and handle that.
That's what I have now, but don't understand how to make it do the same as saga code with sessionWatchdog
export const fetch_all_sessions = (
action$: Obs<*>,
state$: typeof StateObservable
): Obs<*> =>
action$.pipe(
filter(action => action.type === 'FETCH_ALL_SESSIONS'),
switchMap(action => {
let list = action.list;
from(list).pipe(
map(id => {
return fetchSession(id);
})
);
return of({ type: '' });
})
);
Thanks for your help or advice