How to make the watcher saga fire a worker saga only on the first dispatch on an action pattern?

768 Views Asked by At

How to make the watcher saga fire a worker saga only on the first dispatch on an action pattern?

function* rootSaga() {
    yield takeEvery(CHATBOT.START, handleChatbotLoad); //I want watcher saga to trigger handleChatbotLoad only on the very first dispatch of CHATBOT.START
    yield takeEvery(CONVERSATION.ADD_QUERY, handleUserInput);
}

So ya, I want watcher saga to trigger handleChatbotLoad only on the very first dispatch of CHATBOT.START. I can have a flag in the state like started and dispatch CHATBOT.START only once. But then I was expecting a method like takeFirst or something like that. Is there any such method to achieve this?

1

There are 1 best solutions below

2
Alejandro On BEST ANSWER

You can call (or spawn, fork) some function in root saga, which means it'll be called only once when app started. And use take in this function to wait for action dispatched:

function* onlyVeryFirstStartWatcher() {
    const action = yield take(CHATBOT.START);
    // started, do stuff...
    yield call(handleChatbotLoad);
}

function* rootSaga() {
  yield takeEvery(CONVERSATION.ADD_QUERY, handleUserInput);
  yield call(onlyVeryFirstStartWatcher)
}