問題描述
如何從所有待處理的請求中取消特定請求 (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 Wasim、timdeschryver、Wasim)
參考文件