可观察到的数组,它们在完成后分派操作

时间:2019-12-03 01:39:37

标签: reactjs redux rxjs observable

使用ReactJS和redux-observable(so rxjs)

我有以下

const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
    ofType(ItemsDetailsActions.goToItem),
    concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) => {
        return [
            ProjectActions.setCurrentProjectId({projectId: action.payload.projectId}),
            DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
            ItemsActions.setCurrentItemId({ itemId: action.payload.itemId }),
        ]
    })
);

我希望在分派第一个动作时,我等待另一个动作再执行下一个动作 所以我想这样

const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
    ofType(ItemsDetailsActions.goToItem),
    concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) => {
        return [
            ProjectActions.setCurrentProjectId({projectId: action.payload.projectId}),
            takeUntil(action$.ofType(DatastoreActions.getDatastoresSuccess)),
            DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
            takeUntil(action$.ofType(ItemsActions.getItemsListSuccess)),
            ItemsActions.setCurrentItemId({ itemId: action.payload.itemId }),
        ]
    })
);

还是

        return [
            ProjectActions.setCurrentProjectId({projectId: action.payload.projectId}),
            delayWhen(pipe(action$.ofType(DatastoreActions.getDatastoresSuccess))),
            DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
            delayWhen(pipe(action$.ofType(ItemsActions.getItemsListSuccess))),
            ItemsActions.setCurrentItemId({ itemId: action.payload.itemId }),
        ]

因此,基本上,我调度了setCurrentProjectId,并且我想在getDatastoresSuccess也被调度之前等待setCurrentDatastoreId

是否可能有此行为?我尝试使用delayWhentakeUntil,但由于我的语法错误而无法编译,或者redux抱怨

  

未捕获的错误:动作必须是纯对象。使用自定义中间件   进行异步操作。

编辑:

我也尝试过,但是使用了Observable.js:106 Uncaught TypeError: Object(...)(...) is not a function

const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
    ofType(ItemsDetailsActions.goToItem),
    concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) =>
        of(ProjectActions.setCurrentProjectId({projectId: action.payload.projectId})).pipe(
            action$.pipe(
                ofType(DatastoreActions.getDatastoresSuccess),
                concatMap(() =>
                    of(DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId})).pipe(
                        action$.pipe(
                        ofType(ItemsActions.getItemsListSuccess),
                        concatMap(() =>
                                of(ItemsActions.setCurrentItemId({ itemId: action.payload.itemId }))
                            )
                        )
                    )
                )
            )
        )
    )
);

0 个答案:

没有答案