timer.ts 630 B

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