[英]Reset RxJS concatMap without completing the outter Subject
我正在使用concatMap
處理對 API 的多個請求,我希望在處理下一個批次之前完成每個請求批次。 concatMap
在使用callSubject.next(requestData)
觸發流程時按預期工作
問題:對於某些類型的requestData
我想取消任何飛行中的 http 調用,並重置concatMap
。 取消getAll
function 中發生的httpClient
調用非常方便(我有一個takeUntil
可以做到這一點 - 未顯示),但concatMap
可能仍然有許多排隊的請求,然后將被處理。
有沒有辦法在不完成callSubject
主題的情況下重置concatMap
?
注意:如果我觸發unsubscribeCallSubject$.next()
這將清除 concatmap,但也會完成 callSubject,這意味着它不能再與callSubject.next(reqData)
一起使用
// callSubject is a Subject which can be triggered multiple times
callSubject
.pipe(
concatMap((req) => {
// getAll makes multiple httpClient calls in sequence
return getAll(req).pipe(
catchError((err) => {
// prevent callSubject completing on http error
return of(err);
})
);
}),
takeUntil(unsubscribeCallSubject$)
)
.subscribe(
(v) => log("callSubject: next handler", v),
(e) => log("callSubject: error", e),
() => log("callSubject: complete")
);
如果我理解正確的問題,您可以嘗試在unsubscribeCallSubject$
發出時隨時使用switchMap
的方法。
代碼看起來像這樣
unsubscribeCallSubject$.pipe(
// use startWith to start the stream with something
startWith('anything'),
// switchMap any time unsubscribeCallSubject$ emits, which will unsubscribe
// any Observable within the following concatMap
switchMap(() => callSubject$),
// concatMap as in your example
concatMap((req) => {
// getAll makes multiple httpClient calls in sequence
return getAll(req).pipe(
catchError((err) => {
// prevent callSubject completing on http error
return of(err);
})
);
}),
)
.subscribe(
(v) => log("callSubject: next handler", v),
(e) => log("callSubject: error", e),
() => log("callSubject: complete")
);
老實說,我沒有測試過這種方法,所以我不確定它是否能解決你的問題,但如果我正確理解了你的問題,這可能會奏效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.