2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
import coreModule from 'app/core/core_module';
|
2017-10-25 12:32:19 +02:00
|
|
|
|
2017-10-26 11:46:33 +02:00
|
|
|
// This service really just tracks a list of $timeout promises to give us a
|
|
|
|
|
// method for cancelling them all when we need to
|
2017-10-25 12:32:19 +02:00
|
|
|
export class Timer {
|
|
|
|
|
timers = [];
|
|
|
|
|
|
2017-10-26 13:22:45 +02:00
|
|
|
/** @ngInject */
|
2017-12-19 16:06:54 +01:00
|
|
|
constructor(private $timeout) {}
|
2017-10-25 12:32:19 +02:00
|
|
|
|
|
|
|
|
register(promise) {
|
|
|
|
|
this.timers.push(promise);
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(promise) {
|
|
|
|
|
this.timers = _.without(this.timers, promise);
|
|
|
|
|
this.$timeout.cancel(promise);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancelAll() {
|
2017-10-28 13:28:06 +02:00
|
|
|
_.each(this.timers, t => {
|
2017-10-25 12:32:19 +02:00
|
|
|
this.$timeout.cancel(t);
|
|
|
|
|
});
|
|
|
|
|
this.timers = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.service('timer', Timer);
|