如何让 watcher saga 仅在第一次调度动作模式时触发 worker saga?
How to make the watcher saga fire a worker saga only on the first dispatch on an action pattern?
如何让 watcher saga 仅在第一次调度 action pattern 时触发 worker saga?
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);
}
所以,我希望 watcher saga 仅在 CHATBOT.START 的第一次调度时触发 handleChatbotLoad。我可以在 started
这样的状态下有一个标志,并且只发送一次 CHATBOT.START 。
但后来我期待一个像 takeFirst
或类似的方法。有没有这样的方法可以实现?
您可以call (or spawn, fork) some function in root saga, which means it'll be called only once when app started. And use take在这个函数中等待动作派发:
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)
}
如何让 watcher saga 仅在第一次调度 action pattern 时触发 worker saga?
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);
}
所以,我希望 watcher saga 仅在 CHATBOT.START 的第一次调度时触发 handleChatbotLoad。我可以在 started
这样的状态下有一个标志,并且只发送一次 CHATBOT.START 。
但后来我期待一个像 takeFirst
或类似的方法。有没有这样的方法可以实现?
您可以call (or spawn, fork) some function in root saga, which means it'll be called only once when app started. And use take在这个函数中等待动作派发:
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)
}