timer.ts 765 B

12345678910111213141516171819202122232425262728293031
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. import { ITimeoutService } from 'angular';
  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);