timer.ts 633 B

12345678910111213141516171819202122232425262728293031
  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. }
  10. register(promise) {
  11. this.timers.push(promise);
  12. return promise;
  13. }
  14. cancel(promise) {
  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);