如何合并多个Redux Saga调用

时间:2019-04-25 13:26:00

标签: reactjs react-native redux redux-saga

如何合并需要在其他页面上单独使用的2个佐贺电话?

我已经根据其他SO答案尝试了一些方法(全部屈服,等等),但是我无法达到预期的结果。

我的传奇故事如下所示,我的应用程序运行正常。在各个“收藏夹”页面上,我没有问题。但是,我有一个页面是集合和集合的组合。返回Collections之后,我的Saga在第二次saga调用返回Collection的结果之前,将setLoader设置为false,这意味着我的加载器无法正常运行。

我确定我可以为GET_BOTH添加另一个鸭子/中间件,* loadBoth都可以完成,但我想避免这种情况。

任何建议将不胜感激。谢谢。

import { fork, call, put, take } from "redux-saga/effects";
import { setLoader } from "../ducks/loader";
import { types as collectionsTypes, actions as collectionsActions } from "../ducks/collections";
import { types as collectionTypes, actions as collectionActions } from "../ducks/collection";
import { 
    getCollectionsApiRequest,
    getCollectionApiRequest
} from "../services/api";

export function* loadInitialCollections(query = "") {
    try {
        yield put(setLoader(true));
        const collectionsResult = yield call(getCollectionsApiRequest, query);

        yield put(collectionsActions.getCollectionsSuccess({
            collections: collectionsResult.data,
            feature: collectionsTypes.COLLECTIONS
        }));
        yield put(setLoader(false));

    } catch(e) {
        console.log(e);
    }
}

export function* loadInitialCollection(query = "") {
    try {
        yield put(setLoader(true));
        const collectionResult = yield call(getCollectionApiRequest, query);

        yield put(collectionActions.getCollectionSuccess({
            collection: collectionResult.data,
            feature: collectionTypes.COLLECTION
        }));
        yield put(setLoader(false));

    } catch(e) {
        console.log(e);
    }
}


export function* watchGetCollectionsRequest() {
    while(true) {
        const { query } = yield take(collectionsTypes.GET_COLLECTIONS);
        yield fork(loadInitialCollections, query);
    }
}

export function* watchGetCollectionRequest() {
    while(true) {
        const { query } = yield take(collectionTypes.GET_COLLECTION);
        yield fork(loadInitialCollection, query);
    }
}

const salesSaga = [
    fork(watchGetCollectionsRequest),
    fork(watchGetCollectionRequest)
];

export default salesSaga;

1 个答案:

答案 0 :(得分:0)

您无需直接对put执行collectionActions.getCollectionSuccess操作,而可以直接使用call来调用saga通过动作解决的问题,因为它将等待单位saga getCollectionSuccess将执行完成。