如何让所有的JavaScript待定的HTTP请求? [英] How to get all the pending http requests in javascript?
问题描述
有没有在JavaScript的方式或angular2
Is there a way in javascript or angular2
要得到待定的HTTP请求的列表?
to get the list of pending http requests?
的目的是启动其他几个进程
The goal is to start 'several other processes'
根据此列表的波动。
请问类似请求的访问堆栈存在呢?
Does something like an accessible stack of requests exist?
感谢。
推荐答案
在实际上可以延长 HTTP
类拦截请求执行。
In fact you can extend the Http
class to intercept request execution.
import {Injectable} from 'angular2/core';
import {Http,ConnectionBackend,RequestOptions,RequestOptionsArgs,Request} from 'angular2/http';
import 'rxjs/Rx';
import {MonitoringService} from './monitoring.service';
@Injectable()
export class CustomHttp extends Http {
constructor(backend: ConnectionBackend,
defaultOptions: RequestOptions,
private monitoring:MonitoringService) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
console.log('request...');
return super.request(url, options);
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
console.log('get...');
return super.get(url, options);
}
(...)
}
您可以利用最后
运营商拦截相应的HTTP请求观测的完成。您可以将呼叫前增加一个属性,它递减在最后
运营商。
You can leverage the finally
operator to intercept the completion of observables corresponding to HTTP requests. You can increment a property before the call and decrement it in the finally
operator.
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
this.monitoring.pendingRequestsNumber++;
return super.get(url, options).finally(() => {
this.monitoring.pendingRequestsNumber--;
});
}
本 CustomHttp
类可以注册这个样子。我添加了一个监控服务来存储(和共享)未决请求数:
This CustomHttp
class can be registered like this. I added a monitoring service to store (and share) the number of pending requests:
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {HTTP_PROVIDERS,Http,XHRBackend,RequestOptions} from 'angular2/http';
import {AppComponent} from './app.component';
import {CustomHttp} from './http.custom';
import {MonitoringService} from './monitoring.service';
bootstrap(AppComponent, [HTTP_PROVIDERS,
MonitoringService,
provide(Http, {
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, monitory:MonitoringService) => new CustomHttp(backend, defaultOptions, monitory),
deps: [XHRBackend, RequestOptions, MonitoringService]
})
]);
我创建描述来实现此方法的方式plunkr: https://开头plnkr .CO /编辑/ qHNn5amI0byci9RMkZyE?p = preVIEW 。
这篇关于如何让所有的JavaScript待定的HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!