64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
const log = Java.type('org.slf4j.LoggerFactory').getLogger('js.utils.timer');
|
|
log.info('Load utils.timer');
|
|
|
|
class Timer {
|
|
|
|
#timers = new Object();
|
|
|
|
constructor() {
|
|
log.info('Initialization of timer helper class');
|
|
}
|
|
|
|
create(identifier, timeout, func) {
|
|
log.debug(`Create timer with identifier ${identifier}`);
|
|
this.#timers[identifier] = actions.ScriptExecution.createTimer(identifier, timeout, func);
|
|
}
|
|
|
|
cancel(identifier) {
|
|
// Return if no timer with the respactive identifier is available
|
|
if (!this.#timers.hasOwnProperty(identifier)) {
|
|
log.debug(`No timer with identifier ${identifier} available to cancel`);
|
|
return false;
|
|
}
|
|
|
|
// Check if timer is active
|
|
if (!this.#timers[identifier].isActive()) {
|
|
log.debug(`Timer with identifier ${identifier} not running. Cancel anyway`);
|
|
} else {
|
|
log.debug(`Cancel timer with identifier ${identifier}`);
|
|
}
|
|
|
|
// Cancel timer
|
|
this.#timers[identifier].cancel();
|
|
delete this.#timers[identifier];
|
|
}
|
|
|
|
isActive(identifier) {
|
|
return this.#timers[identifier].isActive();
|
|
}
|
|
|
|
cancelAll() {
|
|
// Fetch timers
|
|
let timers = Object.keys(this.#timers);
|
|
|
|
// Return if no timers available
|
|
if (timers.length == 0) {
|
|
log.debug('No timers available to cancel');
|
|
return false;
|
|
}
|
|
|
|
// Cancel all timers
|
|
for (let timer of timers) {
|
|
this.cancel(timer);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
Timer,
|
|
};
|
|
|
|
|