0

I have a network action in my app, that fires fetchUser every time the app reconects to the internet.

I have another saga that looks as follows

export function* watchNewMessage() {
  const { id = {} } = yield take(SET_USER_ID)
  const channel = yield call(subscribeToNewMessages, id)
  while (true) {
   const message = yield take(channel)
   console.log(message)
  }
}

Now the problem is when the app disconects i stop receiving messages which would be fine because my reconect should refire the saga function, but because its stuck in a while true loop its never waiting to be reset. So how do I change the code to reset every time SET_USER_ID is called?

Here is the rest of the relevant code

export function subscribeToNewMessages(id) {
  return eventChannel(emit => {
    const pubsub = fetchNewMessage(emit, id)
    return () => {
      pubsub.unsubscribe()
    }
  }, buffers.expanding(100))
}

export const fetchNewMessage = async (emit, id) => {
  return PubSub.subscribe(`${IOT_PREFIX}/discussion/${id}/message`).subscribe({
    next: data => { emit(data.value) },
    error: () => console.log('error'),
    close: () => console.log('close'),
  })
}
Adam Katz
  • 6,999
  • 11
  • 42
  • 74

1 Answers1

0

Okay I got this working

I created a new function

export function* watchNewMessage() { yield takeLatest(SET_USER_ID, manageSubscription) }

export function* manageSubscription() { const { id = {} } = yield take(SET_USER_ID) const channel = yield call(subscribeToNewMessages, id) while (true) { const message = yield take(channel) console.log(message) } }

And now it works perfectly

Adam Katz
  • 6,999
  • 11
  • 42
  • 74