EventChannel emit not working
bekatd opened this issue · comments
This code worked perfectly fine before, but somehow stopped working recently
function* initRoomListener(event) {
const {roomId} = event;
const auth = yield select(getAuth);
const roomChannel = createRoomChannel(roomId, auth.userId);
while (true) {
const messages = yield take(roomChannel);
yield put(ChatActions.updateMessages(messages));
}
}
function createRoomChannel(roomId, userId) {
const listener = eventChannel((emit) => {
database()
.ref(`/rooms/${roomId}/${userId}`)
.limitToLast(1)
.on('value', (snapshot) => emit(processMessages(snapshot, roomId)));
return () => database().ref(`/rooms/${roomId}/${userId}`).off(listener);
});
return listener;
}
function processMessages(snapshot, roomId) {
const messages = [];
snapshot?.forEach((child) => {
messages.push({
roomId,
id: child.key,
});
});
return messages;
}
I see that only problematic part is emit(processMessages(snapshot, roomId)));
. before this everything can be logged
Hi! I'm going to need a little more information to help you debug. Are you certain the database().on('value')
callback is being called?
Next, are you certain initRoomListener
is getting passed into your root saga? Are you able to add logs before createRoomChannel
and they are getting triggered?
Yeah, everything works perfectly fine, but emit is failing. I can see logs before emit
and after emit
too. I am not saying that emit is failing, but the fact is it does not trigger this part const messages = yield take(roomChannel);
somehow
- firebase callback is executed always
- listeners are started definitely
could be there any chance, that emitting event from another library (https://github.com/meinto/react-native-event-listeners) could cause saga emit
to fail?
Can you show me the root saga where initRoomListener
is being registered?
Circling back, did you resolve this issue?
Closing for now. Please feel free to reply and I'll reopen and continue to help! Thanks!