Redux-saga: Error when emitting null in eventChannel

Created on 20 Jun 2016  路  3Comments  路  Source: redux-saga/redux-saga

Note: onFirebaseAuthStateChange is an event emitter from Firebase. Emits a user object when a user signed on. Emits null when user signed out.

This logs an error.

const firebaseAuthStateChangeChannel = () =>
  eventChannel((emit) => {
    onFirebaseAuthStateChange((user) => {
      emit(user)
    })
  })

export function* authWatch() {
  const userChannel = yield call(firebaseAuthStateChangeChannel)
  while (true) {
    const user = yield take(userChannel)
    if (user == null) {
      yield put(userLost(user))
    } else {
      yield put(userFound(user))
    }
  }
}
utils.js:188 uncaught at check Saga was provided with an undefined action
Hints:
    - check that your Action Creator returns a non-undefined value
    - if the Saga was started using runSaga, check that your subscribe source provides the action to its listeners

This works fine.

const firebaseAuthStateChangeChannel = () =>
  eventChannel((emit) => {
    onFirebaseAuthStateChange((user) => {
      emit(user || 'null')
    })
  })

export function* authWatch() {
  const userChannel = yield call(firebaseAuthStateChangeChannel)
  while (true) {
    const user = yield take(userChannel)
    if (user === 'null') {
      yield put(userLost(user))
    } else {
      yield put(userFound(user))
    }
  }
}

I see how the error could be related. But I'm just wondering if this is intended design, and is there another way around this?

Thanks.

Most helpful comment

On second thought. The channels should feed back a result much like an action creator does.

Turning the code into emit({ user }) makes perfect sense then.

All 3 comments

On second thought. The channels should feed back a result much like an action creator does.

Turning the code into emit({ user }) makes perfect sense then.

@Moeriki Yes, it's a good workaround. I also use the same approach in my exmaple.

@Moeriki yes. As you said it's by design to keep things consistent with Redux actions.

Was this page helpful?
0 / 5 - 0 ratings