redux-saga 什么时候使用 fork? [英] redux-saga when to use fork?

查看:63
本文介绍了redux-saga 什么时候使用 fork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面两种方法有什么区别?

what would be the difference between the two approaches below?

export function* watchLoginUser() {
  yield takeEvery(USER_LOGIN, loginUser)
}
export function* watchLogoutUser() {
  yield takeEvery(USER_LOGOUT, logoutUser)
}
export function* watchGetParties() {
  yield takeEvery(PARTIES_GET, getParties)
}
export default function* root() {
  yield [
    fork(watchLoginUser),
    fork(watchLogoutUser),
    fork(watchGetParties)
  ]
}

export default function* root() {
  yield [
    takeEvery(USER_LOGIN, loginUser),
    takeEvery(USER_LOGOUT, logoutUser),
    takeEvery(PARTIES_GET, getParties)
  ]
}

什么时候需要使用 fork,什么时候不需要?

When do I need to use fork and when not?

推荐答案

一般来说,fork 在 saga 需要启动非阻塞任务时很有用.这里的非阻塞是指:调用者启动任务并继续执行,无需等待任务完成.

In general, fork is useful when a saga needs to start a non-blocking task. Non-blocking here means: the caller starts the task and continues executing without waiting for it to complete.

这在很多情况下都有用,但主要有两种情况:

There is a variety of situations where this can be useful, but the 2 main ones are:

  • 按逻辑域对传奇进行分组
  • 保留对任务的引用以便能够取消/加入它

您的顶级传奇可以作为第一个用例的示例.您可能会遇到以下情况:

Your top-level saga can be an example of the first use-case. You'll likely have something like:

yield fork(authSaga);
yield fork(myDomainSpecificSaga);
// you could use here something like yield [];
// but it wouldn't make any difference here

其中 authSaga 可能包含以下内容:

Where authSaga will likely include things like:

yield takeEvery(USER_REQUESTED_LOGIN, authenticateUser);
yield takeEvery(USER_REQUESTED_LOGOUT, logoutUser);

你可以看到这个例子和你建议的一样,用 fork 调用一个产生 takeEvery 调用的 saga.但在实践中,您只需要出于代码组织的目的执行此操作.takeEvery 本身就是一个分叉任务,所以在大多数情况下,这将是无用的冗余.

You can see that this example is equivalent to what you suggested, calling with fork a saga yielding a takeEvery call. But in practice, you only need to do this for code organisation purposes. takeEvery is itself a forked task, so in most cases, this would be uselessly redundant.

第二个用例的示例如下:

An example of the second use-case would be something like:

yield take(USER_WAS_AUTHENTICATED);
const task = yield fork(monitorUserProfileUpdates);
yield take(USER_SIGNED_OUT);
yield cancel(task);

您可以在此示例中看到 monitorUserProfileUpdates 将在调用者 saga 恢复时执行,并等待 USER_SIGNED_OUT 操作被分派.此外,它还可以保留对它的引用,以便在需要时取消它.

You can see in this example that the monitorUserProfileUpdates will execute while the caller saga resumes, and gets to wait to the USER_SIGNED_OUT action to be dispatched. It can in addition keep a reference to it in order to cancel it when needed.

为了完整起见,还有另一种启动非阻塞调用的方法:spawn.forkspawn 的不同之处在于错误和取消从子级到父级的冒泡方式.

For the sake of completeness, there is another way to start non-blocking calls: spawn. fork and spawn differ in how errors and cancellations bubble from child to parent saga.

这篇关于redux-saga 什么时候使用 fork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆