Race condition SSE Redux Saga with additional REST request

50 views Asked by At
function* firstSubscribeSSESaga() {
  try {
    // here deleted unnecessary information

    while (true) {
      const event = yield take(channel)
      const sseEventData: {
        someProperty1: string | null
        someProperty2: string | null
      } = event.data && JSON.parse(String(event.data))

      if (sseEventData.someProperty2) {
        yield call(secondSubscribeSSESaga, sseEventData.someProperty2)
      }

      yield put(firstProviderActions.setUpdateProperty(sseEventData))
    }
  } catch (error) {
    console.log('error', error)
  }
}

Perhaps someone knows how to solve this problem, please tell me.

Inforamation:

SSE Event has two keys in the object {someProperty1: string | null, someProperty2: string | null}

if (someProperty1) comes, then nothing specific happens, I just call the action (firstProviderActions.setUpdateProperty) and update data.

when (someProperty2) arrives, I need to additionally call another Saga (secondSubscribeSSESaga) and there make an additional GET request to Backend, when this additional request is completed and updated the data in second Redux Store, after that I return to the current Saga and execute (firstProviderActions.setUpdateProperty)

My problem:

The first SSE Event arrives and it sent (someProperty2) and I go to call an additional GET request and wait for it. While I am waiting for this GET request to be executed, I receive a second SSE Event and it only contains (someProperty1). Since for (someProperty1) I do not need to make an additional GET request, I go to immediately update the data (firstProviderActions.setUpdateProperty), but the problem is that I should not update the data from the second SSE event until I have updated the data from the first request.

The idea is that I should save data from SSE in parallel, in what order they come in and save it, but since in certain cases I have to make an additional GET request and wait for it, I cannot fulfill this condition.

0

There are 0 answers