timer.ts 783 B

1234567891011121314151617181920212223242526272829303132
  1. import { ITimeoutService } from 'angular';
  2. import { without, each } from 'lodash';
  3. import coreModule from 'app/angular/core_module';
  4. // This service really just tracks a list of $timeout promises to give us a
  5. // method for canceling them all when we need to
  6. export class Timer {
  7. timers: Array<angular.IPromise<any>> = [];
  8. /** @ngInject */
  9. constructor(private $timeout: ITimeoutService) {}
  10. register(promise: angular.IPromise<any>) {
  11. this.timers.push(promise);
  12. return promise;
  13. }
  14. cancel(promise: angular.IPromise<any>) {
  15. this.timers = without(this.timers, promise);
  16. this.$timeout.cancel(promise);
  17. }
  18. cancelAll() {
  19. each(this.timers, (t) => {
  20. this.$timeout.cancel(t);
  21. });
  22. this.timers = [];
  23. }
  24. }
  25. coreModule.service('timer', Timer);