如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)


問題描述

如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)

在我的 Ionic web 應用程序中,

對於瀏覽器

我想實現取消下載功能,目前,用戶可以在同時,我想取消特定的下載 HTTP 請求。

這是我目前擁有的。

@Effect()
public fileDownloadAction$: Observable<Action> = this.action$
    .ofType(FileDownloadActionTypes.FILE_DOWNLOAD_REQUESTED)
    .map(this.toPayload)
    .mergeMap(payload => (this.downloadService.downloadFile(payload))
        .concatMap((res: any) => {                       
            return Observable.from([
                new FileDownloadCompleted(downloadSuccess)
            ]);
        })
    )

但我不知道如何在效果中執行此操作。有什麼建議麼?在此先感謝:‑)


參考解法

方法 1:

Not sure what the specific question is but you can use exhaustMap or switchMap (instead of mergeMap) to cancel new or pending requests.

方法 2:

After spending much time digging into RxJS I did it without changing effect Here is mess magic.

public download (endpoint: string, body: any = {}, uniqueKey: string) {

let req = new HttpRequest('POST', endpoint, body, {
responseType: 'blob',
reportProgress: true,
headers: new HttpHeaders({
'Content‑Type': 'application/json',
'Accept': 'application/json'
})
});

return new Observable(observable => {
let downloadRequest = this.http.request(req).subscribe(results => {
observable.next(results);
if(results['type'] == 4) {
//Complete Observable once download success
observable.complete();
}
});
allDownloadReq[uniqueKey] = downloadRequest;
});
}
</code></pre>

I have pushed each download subscriber in an array and canceled it with uniqeKey

public cancelDownload (key) {
  if(allDownloadReq[key]) {
    allDownloadReq[key].unsubscribe()
    return true;
  } else {
    return true
  }
}

Suggestions have still welcome

Hope this will help someone.

(by WasimtimdeschryverWasim)

參考文件

  1. How to cancel specific request from all pending request (CC BY‑SA 2.5/3.0/4.0)

#ngrx-effects #angular5 #ngrx #ngrx-store #ionic3






相關問題

rxjs減少不繼續 (rxjs reduce does not continue)

從 reducer/action 觸發 API 重新加載的正確方法是什麼? (What's the proper way to trigger the API reload from reducer/action?)

Angular NgRx 效果,如何傳遞參數? (Angular NgRx effects, how to pass a parameter?)

NgRx 效果無限循環 (NgRx Effects infinite loop)

發送 forkjoin api,每個 api 之間有延遲 (Send forkjoin api with a delay in between each api)

如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)

使用 NgRx 存儲優化 Angular 內存以避免內存洩漏 (Angular memory optimization with NgRx store to avoid memory leaks)

NgRx EffectsModule 導入組 (NgRx EffectsModule imports group)

切換地圖後在NGRX特效點擊功能中獲取動作道具 (Get action props in the NGRX effects tap function after switch Map)

如何防止在 NgRx 中第二次加載數據? (How prevent loading data second time in NgRx?)

需要幫助了解這種 Ngrx 效果 (Need help understanding this Ngrx effect)

測試效果 NgRx Angular 11 (Testing effects NgRx Angular 11)







留言討論