redux-saga 何时使用 fork?
redux-saga when to use fork?
下面两种方法有什么区别?
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,什么时候不需要?
一般来说,fork
在saga需要启动一个非阻塞任务时很有用。这里非阻塞的意思是:调用者启动任务并继续执行而不等待它完成。
这在很多情况下都会有用,但主要有两种情况:
- 按逻辑域对 sagas 分组
- 保留对任务的引用以便能够cancel/join它
您的顶级传奇可以作为第一个用例的示例。你可能会有类似的东西:
yield fork(authSaga);
yield fork(myDomainSpecificSaga);
// you could use here something like yield [];
// but it wouldn't make any difference here
其中 authSaga
可能包括以下内容:
yield takeEvery(USER_REQUESTED_LOGIN, authenticateUser);
yield takeEvery(USER_REQUESTED_LOGOUT, logoutUser);
你可以看到这个例子等同于你所建议的,用 fork
调用一个 saga 产生一个 takeEvery
调用。但实际上,您只需出于代码组织的目的这样做。 takeEvery
本身就是一个分叉任务,所以在大多数情况下,这将是无用的冗余。
第二个用例的示例如下:
yield take(USER_WAS_AUTHENTICATED);
const task = yield fork(monitorUserProfileUpdates);
yield take(USER_SIGNED_OUT);
yield cancel(task);
你可以在这个例子中看到,monitorUserProfileUpdates
将在调用者 saga 恢复时执行,并等待 USER_SIGNED_OUT
动作被调度。它还可以保留对它的引用,以便在需要时取消它。
为了完整起见,还有另一种启动非阻塞调用的方式:spawn
。 fork
和 spawn
的不同之处在于错误和取消从子级到父级 saga 的冒泡方式。
通常 fork
对于多次调度 API 调用的情况更有用,原因是您可以通过实例化任务中的取消来拒绝这些提取,例如cancel(task1);
如果最终用户强行退出应用程序或者如果其中一项任务失败导致您的指令、策略和逻辑出现问题并且取消或终止您的 saga 上的当前处理任务可能是合理的,则很有用;
有2种方法取消任务
基于 redux-saga 非阻塞 效果取消
的文档
import { take, put, call, fork, cancel } from 'redux-saga/effects'
// ...
function* loginFlow() {
while (true) {
const {user, password} = yield take('LOGIN_REQUEST')
// Non-Blocking Effect which is the fork
const task = yield fork(authorize, user, password)
const action = yield take(['LOGOUT', 'LOGIN_ERROR'])
if (action.type === 'LOGOUT'){
//cancel the task
yield cancel(task)
yield call(Api.clearItem, 'token')
}
}
}
或
import {call, put, fork, delay} from 'redux-saga/effects';
import someAction from 'action/someAction';
function* fetchAll() {
yield fork(fetcher, 'users');
yield fork(fetcher, 'posts');
yield fork(fetcher, 'comments');
yield delay(1500);
}
function* fetcher(endpoint) {
const res = yield call(fetchAPI, endpoint);
if (!res.status) {
throw new Error(`Error: ${res.error}`);
}
yield put(someAction({payload: res.payload}));
}
function* worker() {
try {
yield call(fetchAll);
} catch (err) {
// handle fetchAll errors
}
}
function* watcher() {
yield takeEvery(BLOGS.PUSH, worker);
}
不客气:)
下面两种方法有什么区别?
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,什么时候不需要?
一般来说,fork
在saga需要启动一个非阻塞任务时很有用。这里非阻塞的意思是:调用者启动任务并继续执行而不等待它完成。
这在很多情况下都会有用,但主要有两种情况:
- 按逻辑域对 sagas 分组
- 保留对任务的引用以便能够cancel/join它
您的顶级传奇可以作为第一个用例的示例。你可能会有类似的东西:
yield fork(authSaga);
yield fork(myDomainSpecificSaga);
// you could use here something like yield [];
// but it wouldn't make any difference here
其中 authSaga
可能包括以下内容:
yield takeEvery(USER_REQUESTED_LOGIN, authenticateUser);
yield takeEvery(USER_REQUESTED_LOGOUT, logoutUser);
你可以看到这个例子等同于你所建议的,用 fork
调用一个 saga 产生一个 takeEvery
调用。但实际上,您只需出于代码组织的目的这样做。 takeEvery
本身就是一个分叉任务,所以在大多数情况下,这将是无用的冗余。
第二个用例的示例如下:
yield take(USER_WAS_AUTHENTICATED);
const task = yield fork(monitorUserProfileUpdates);
yield take(USER_SIGNED_OUT);
yield cancel(task);
你可以在这个例子中看到,monitorUserProfileUpdates
将在调用者 saga 恢复时执行,并等待 USER_SIGNED_OUT
动作被调度。它还可以保留对它的引用,以便在需要时取消它。
为了完整起见,还有另一种启动非阻塞调用的方式:spawn
。 fork
和 spawn
的不同之处在于错误和取消从子级到父级 saga 的冒泡方式。
通常 fork
对于多次调度 API 调用的情况更有用,原因是您可以通过实例化任务中的取消来拒绝这些提取,例如cancel(task1);
如果最终用户强行退出应用程序或者如果其中一项任务失败导致您的指令、策略和逻辑出现问题并且取消或终止您的 saga 上的当前处理任务可能是合理的,则很有用;
有2种方法取消任务
基于 redux-saga 非阻塞 效果取消
的文档import { take, put, call, fork, cancel } from 'redux-saga/effects'
// ...
function* loginFlow() {
while (true) {
const {user, password} = yield take('LOGIN_REQUEST')
// Non-Blocking Effect which is the fork
const task = yield fork(authorize, user, password)
const action = yield take(['LOGOUT', 'LOGIN_ERROR'])
if (action.type === 'LOGOUT'){
//cancel the task
yield cancel(task)
yield call(Api.clearItem, 'token')
}
}
}
或
import {call, put, fork, delay} from 'redux-saga/effects';
import someAction from 'action/someAction';
function* fetchAll() {
yield fork(fetcher, 'users');
yield fork(fetcher, 'posts');
yield fork(fetcher, 'comments');
yield delay(1500);
}
function* fetcher(endpoint) {
const res = yield call(fetchAPI, endpoint);
if (!res.status) {
throw new Error(`Error: ${res.error}`);
}
yield put(someAction({payload: res.payload}));
}
function* worker() {
try {
yield call(fetchAll);
} catch (err) {
// handle fetchAll errors
}
}
function* watcher() {
yield takeEvery(BLOGS.PUSH, worker);
}
不客气:)