@toreda/time
Version:
Simple, small footprint library for common time operations and converting between units of time.
66 lines (65 loc) • 1.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimerPassive = void 0;
const mutable_1 = require("../mutable");
const make_1 = require("../time/make");
const since_1 = require("../time/since");
/**
* @category Timers
*/
class TimerPassive {
constructor() {
this.running = false;
this.interval = (0, mutable_1.mutable)(0);
this.lastTrigger = (0, mutable_1.mutable)(0);
this.triggerLimit = (0, mutable_1.mutable)(0);
this.timeStart = (0, make_1.timeMake)('s', 0);
}
/**
* Start timer using the current time. No effect if timer is running.
* @returns Whether timer started successfully.
*/
start() {
if (this.running) {
return false;
}
this.timeStart.setNow();
this.running = true;
return true;
}
/**
* Stop current timer. No effect if timer is not running.
* @returns Whether timer stopped succesfully.
*/
stop() {
if (!this.running) {
return false;
}
this.running = false;
return true;
}
trigger() {
// Todo
}
onUpdate() {
if (!this.running) {
return;
}
const elapsed = (0, since_1.timeSince)(this.timeStart());
if (elapsed === null) {
return;
}
const seconds = elapsed.asSeconds();
if (seconds === null) {
return;
}
if (seconds < this.interval()) {
return;
}
this.trigger();
}
reset() {
this.running = false;
}
}
exports.TimerPassive = TimerPassive;