2022-04-22 08:33:13 -05:00
|
|
|
import { ITimeoutService } from 'angular';
|
2021-04-21 02:38:00 -05:00
|
|
|
import { without, each } from 'lodash';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2021-11-09 01:37:16 -06:00
|
|
|
import coreModule from 'app/angular/core_module';
|
2017-10-25 05:32:19 -05:00
|
|
|
|
2017-10-26 04:46:33 -05:00
|
|
|
// This service really just tracks a list of $timeout promises to give us a
|
2018-12-05 06:02:58 -06:00
|
|
|
// method for canceling them all when we need to
|
2017-10-25 05:32:19 -05:00
|
|
|
export class Timer {
|
2019-04-28 02:58:12 -05:00
|
|
|
timers: Array<angular.IPromise<any>> = [];
|
2017-10-25 05:32:19 -05:00
|
|
|
|
2017-10-26 06:22:45 -05:00
|
|
|
/** @ngInject */
|
2019-04-28 02:58:12 -05:00
|
|
|
constructor(private $timeout: ITimeoutService) {}
|
2017-10-25 05:32:19 -05:00
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
register(promise: angular.IPromise<any>) {
|
2017-10-25 05:32:19 -05:00
|
|
|
this.timers.push(promise);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
cancel(promise: angular.IPromise<any>) {
|
2021-04-21 02:38:00 -05:00
|
|
|
this.timers = without(this.timers, promise);
|
2017-10-25 05:32:19 -05:00
|
|
|
this.$timeout.cancel(promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelAll() {
|
2021-04-21 02:38:00 -05:00
|
|
|
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);
|