Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 working indicator

Is it possible to do the "ready" indicator in angular2. I need to detect somehow is angular working or not to avoid race conditions.

Here's what I need but for angularjs implementation:

var el = document.querySelector('[ng-app], [data-ng-app]');
window.angularReady = false;
if (angular.getTestability) {
    angular.getTestability(el).whenStable(function() {  
        window.angularReady = true; 
    });
} else {
    var $browser = angular.element(el).injector().get('$browser');           
    if ($browser.outstandingRequestCount > 0) { 
        window.angularReady = false; 
    }
    $browser.notifyWhenNoOutstandingRequests(function() {
        window.angularReady = true; 
    });
}

https://github.com/wrozka/capybara-angular/blob/master/lib/capybara/angular/waiter.rb#L51-L61

like image 461
ole Avatar asked Feb 12 '26 00:02

ole


1 Answers

You could implement this feature by extending the Http class.

First create a monitoring service that you can use from this class to monitor in-progress requests:

export class MonitoringService {
  private outstandingRequestsNumber: int = 0;
  private outstandingRequestsNumberObserver: Observer;
  private outstandingRequestsNumberObservable: Observable;

  constructor() {
    this.outstandingRequestsNumberObservable = Observable.create((observer:Observer) => {
      this.outstandingRequestsNumberObserver = observer;
    }).share();
  }

  getOutstandingRequests() {
    return this.outstandingRequestsNumber;
  }

  incrementOutstandingRequests() {
    this.outstandingRequestsNumber++;
  }

  decrementOutstandingRequests() {
    this.outstandingRequestsNumber--;
    if (this.outstandingRequestsNumber === 0) {
      this.outstandingRequestsNumberObserver.next();
    }
  }

  notifyWhenNoOutstandingRequests(callback) {
    this.outstandingRequestsNumberObservable.subscribe(callback);
  }
}

Now the implementation of the CustomHttp class:

    @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...');
    this.monitoring.incrementOutstandingRequests();
    return super.request(url, options).finally(() => {
      console.log('finally');
      this.monitoring.decrementOutstandingRequests();
    });
  }

  get(url: string, options?: RequestOptionsArgs): Observable<Response> {
    console.log('get...');
    this.monitoring.incrementOutstandingRequests();
    return super.get(url, options).finally(() => {
      console.log('finally');
      this.monitoring.decrementOutstandingRequests();
    });
  }

  (...)
}

The last step consists of registering both classes when bootstrapping the application:

bootstrap(AppComponent, [HTTP_PROVIDERS,
  MonitoringService,
  provide(Http, {
    useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, monitory:MonitoringService) => new CustomHttp(backend, defaultOptions, monitory),
    deps: [XHRBackend, RequestOptions, MonitoringService]
  })
]);

See this plunkr: https://plnkr.co/edit/lXn5vKY1K2A8RvSsJWm3?p=preview.

See this question for more details:

  • How to get all the pending http requests in javascript?
like image 103
Thierry Templier Avatar answered Feb 13 '26 14:02

Thierry Templier