0

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

Jatin Bhuva
  • 1,301
  • 1
  • 4
  • 22
Sveta K
  • 1
  • 2
  • Maybe you could use with `timeout`, more details and examples can be found here: https://www.learnrxjs.io/learn-rxjs/operators/utility/timeout – Jimmy Dec 25 '22 at 11:43

0 Answers0