如何修改 Effect 以处理多个调用的请求?
How to modify an Effect to work with multiple requests called?
我编写了这个 Effect 来一次处理一个调用:
@Effect()
indexCollectiveDocuments$ = this.actions$.pipe(
ofType<IndexCollectiveDocuments>(CollectiveIndexingActionTypes.IndexCollectiveDocuments),
mergeMapTo(this.store.select(getIndexingRequest)),
exhaustMap((request: any[], index: number) => {
return zip(...request.map(item => {
this.currentItem = item;
return this.indexingService.indexDocuments(item).pipe(
map((response: any[]) => new IndexCollectiveDocumentsSuccess(response)),
catchError(error => of(new IndexCollectiveDocumentsFailure({ error: error, item: this.currentItem })))
)
}))
})
);
它确实会根据请求结果分派成功和失败操作。
但是当我用多个项目(它们是 getIndexingRequest 的有效负载)来一个接一个地发送请求时,成功和失败操作没有相应地调度,取消当一个失败时。
如何修改它以使其适用于多个请求,而不是一个?
编辑:我可以在网络选项卡中看到所有请求及其结果。但是只发送了一个动作。
您正在使用 RxJS 运算符 exhaustMap
,它将在已经有一个请求时取消传入请求 运行ning。
至运行所有请求使用mergeMap
(运行并行)或concatMap
(运行串行)。
我编写了这个 Effect 来一次处理一个调用:
@Effect()
indexCollectiveDocuments$ = this.actions$.pipe(
ofType<IndexCollectiveDocuments>(CollectiveIndexingActionTypes.IndexCollectiveDocuments),
mergeMapTo(this.store.select(getIndexingRequest)),
exhaustMap((request: any[], index: number) => {
return zip(...request.map(item => {
this.currentItem = item;
return this.indexingService.indexDocuments(item).pipe(
map((response: any[]) => new IndexCollectiveDocumentsSuccess(response)),
catchError(error => of(new IndexCollectiveDocumentsFailure({ error: error, item: this.currentItem })))
)
}))
})
);
它确实会根据请求结果分派成功和失败操作。
但是当我用多个项目(它们是 getIndexingRequest 的有效负载)来一个接一个地发送请求时,成功和失败操作没有相应地调度,取消当一个失败时。
如何修改它以使其适用于多个请求,而不是一个?
编辑:我可以在网络选项卡中看到所有请求及其结果。但是只发送了一个动作。
您正在使用 RxJS 运算符 exhaustMap
,它将在已经有一个请求时取消传入请求 运行ning。
至运行所有请求使用mergeMap
(运行并行)或concatMap
(运行串行)。