grafana/public/app/angular/services/timer.ts

33 lines
783 B
TypeScript
Raw Normal View History

import { ITimeoutService } from 'angular';
import { without, each } from 'lodash';
import coreModule from 'app/angular/core_module';
2017-10-25 05:32:19 -05:00
// This service really just tracks a list of $timeout promises to give us a
// method for canceling them all when we need to
2017-10-25 05:32:19 -05:00
export class Timer {
timers: Array<angular.IPromise<any>> = [];
2017-10-25 05:32:19 -05:00
/** @ngInject */
constructor(private $timeout: ITimeoutService) {}
2017-10-25 05:32:19 -05:00
register(promise: angular.IPromise<any>) {
2017-10-25 05:32:19 -05:00
this.timers.push(promise);
return promise;
}
cancel(promise: angular.IPromise<any>) {
this.timers = without(this.timers, promise);
2017-10-25 05:32:19 -05:00
this.$timeout.cancel(promise);
}
cancelAll() {
each(this.timers, (t) => {
2017-10-25 05:32:19 -05:00
this.$timeout.cancel(t);
});
this.timers = [];
}
}
2017-12-20 05:33:33 -06:00
coreModule.service('timer', Timer);