L
L
lena_shevtsova2021-08-30 00:54:40
redux
lena_shevtsova, 2021-08-30 00:54:40

How to call dispatch from a generator function inside an event subscription from firebase?

There are two options for the function.
In this - yield cannot be called where I want

function* subscribeToChatChanges () {
  firebase
  .database()
  .ref('chats')
  .on('child_added', (snapshot) => {
    yield put({ type: ADD_CHAT_TO_STORE, payload: {chatId: snapshot.key, value: snapshot.val()} })
  })
}


And this adds only one entry. Even if you make an array of chats, push chat there and do resolve(chats)
function* subscribeToChatChanges () {
var chat = yield call(() => {
    return new Promise((resolve, reject) => {

      firebase
      .database()
      .ref('chats')
      .on('child_added', (snapshot) => {
        var chat = {id: snapshot.key, name: snapshot.val(), messageList:[]}
        resolve(chat)
      })
    })
  })
  yield put({ type: ADD_CHAT_TO_STORE, payload: chat })
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question