[英]Angular Http - How to cancel outdated request
因此,基本上,我有一个函数,每次从signalR接收到新数据时都会向服务器发出2个HTTP请求,该数据将被触发很多次。 因此getitemCount
函数将被调用多次。 因此,它将多次击中服务器。
getitemCount
是一个承诺,因为我曾经使用toPromise()
运算符将其从Observable转换。
我想做的是:从服务器接收更新时,如果有任何getitemCount
正在进行。 它将被取消,而是发出一个新请求。
export class LeftNavigationComponent implements OnInit, OnDestroy {
typeACount: number = 0;
typeBCount: number = 0;
constructor(
private itemService: service.ItemService,
private signalR: service.SignalRService) { }
ngOnInit(): void {
this.subscriptions = [
this.signalR.itemCreated.subscribe(item => this.onUpdatingData()),
this.signalR.itemUpdated.subscribe(item => this.onUpdatingData()),
this.signalR.itemDeleted.subscribe(itemId => this.onUpdatingData())]
}
onUpdatingData() {
Promise.all([
this.itemService.getitemCount(APP_SETTING.TYPE_A),
this.itemService.getitemCount(APP_SETTING.TYPE_B)])
.then(response => this.gettingCountDone(response))
}
gettingCountDone(response) {
this.typeACount = <any>response[0];
this.typeBCount = <any>response[1];
}
}
我之前使用过switchMap来实现搜索,但仍无法将其归档在当前用例上。
任何人都可以给我一个建议。 到目前为止,我所做的是使用Observable而不是Promise并使用folkJoin合并结果。 但它的行为与我目前的行为相同。
onUpdatingData() {
Rx.Observable.forkJoin(this.itemService.getitemCount(APP_SETTING.TYPE_A), this.itemService.getitemCount(APP_SETTING.TYPE_B), (r1, r2) => {
})
}
我只是尝试本地化,是的,取消订阅即可完成工作
const subscription = this.http.get(`/api/v2/url`).subscribe();
subscription.unsubscribe();
我看到该请求在Chrome中被取消了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.