Redux-saga 任务取消
Redux-saga task cancellation
我很难取消这个任务。它在一个小循环中,如果我在函数底部的累加器中使用 takeLatest
,它 运行 没问题。如果我使用 take
,happens/the flowControl
函数不会出现在 运行 中。
取消操作触发,但任务在 运行ning 保持正确 - 这是使用 while
的问题吗?这是我没看到的另一个问题吗?
如果问题出在使用页面底部的 *appRoot
导出此流时,如何将流控制正确地包含到 React/Redux 存储中?
function* install() {
const items = list.length - 1; // total number of things to call
let count = 1;
while (count < items) {
const response = yield call(() => Promise.resolve(list[count]));
if (response && !response.error) {
yield put({type: LOAD_ITEM_SUCCESS, item: response.data});
console.log('success, created new item', response.data);
}
yield delay(5000);
}
}
export function* flowControl() {
while ( true ) {
// starts the task in the background
const task = yield fork(install);
// wait for the user stop action
yield take(CANCEL_ACTION, cancelInstall, task);
}
}
function* cancelInstall(task){
yield cancel(task);
}
export default function* appRoot() {
yield all([
takeLatest(LOAD_ITEMS, flowControl)
]);
}
其他人的更新 Post-Rubber-Duck 在这里:
- 问题 1:
flowControl
中的 while 循环不可证伪,因此它 运行 永远存在
- 问题 2:
take
没有按预期工作,但将其替换为 takeLatest
会正确触发取消功能。
尽管 present in the demos,while
循环还没有可证伪的状态,所以它只是... 运行s。一堆!我不确定为什么,但是删除 FlowControl
while 并将取消 take
替换为 takeLatest
会使此流程按预期工作。
take 接收一个参数。
重试此更改:
export function* flowControl() {
while ( true ) {
// starts the task in the background
const task = yield fork(install);
// wait for the user stop action
yield take(CANCEL_ACTION); // <-
yield call(cancelInstall, task); // <-
}
}
我很难取消这个任务。它在一个小循环中,如果我在函数底部的累加器中使用 takeLatest
,它 运行 没问题。如果我使用 take
,happens/the flowControl
函数不会出现在 运行 中。
取消操作触发,但任务在 运行ning 保持正确 - 这是使用 while
的问题吗?这是我没看到的另一个问题吗?
如果问题出在使用页面底部的 *appRoot
导出此流时,如何将流控制正确地包含到 React/Redux 存储中?
function* install() {
const items = list.length - 1; // total number of things to call
let count = 1;
while (count < items) {
const response = yield call(() => Promise.resolve(list[count]));
if (response && !response.error) {
yield put({type: LOAD_ITEM_SUCCESS, item: response.data});
console.log('success, created new item', response.data);
}
yield delay(5000);
}
}
export function* flowControl() {
while ( true ) {
// starts the task in the background
const task = yield fork(install);
// wait for the user stop action
yield take(CANCEL_ACTION, cancelInstall, task);
}
}
function* cancelInstall(task){
yield cancel(task);
}
export default function* appRoot() {
yield all([
takeLatest(LOAD_ITEMS, flowControl)
]);
}
其他人的更新 Post-Rubber-Duck 在这里:
- 问题 1:
flowControl
中的 while 循环不可证伪,因此它 运行 永远存在 - 问题 2:
take
没有按预期工作,但将其替换为takeLatest
会正确触发取消功能。
尽管 present in the demos,while
循环还没有可证伪的状态,所以它只是... 运行s。一堆!我不确定为什么,但是删除 FlowControl
while 并将取消 take
替换为 takeLatest
会使此流程按预期工作。
take 接收一个参数。
重试此更改:
export function* flowControl() {
while ( true ) {
// starts the task in the background
const task = yield fork(install);
// wait for the user stop action
yield take(CANCEL_ACTION); // <-
yield call(cancelInstall, task); // <-
}
}