UNPKG

@toreda/time

Version:

Simple, small footprint library for common time operations and converting between units of time.

62 lines (61 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimerPassive = void 0; const strong_types_1 = require("@toreda/strong-types"); const make_1 = require("../time/make"); const since_1 = require("../time/since"); /** * @category Timers */ class TimerPassive { constructor() { this.running = (0, strong_types_1.boolMake)(false); this.interval = (0, strong_types_1.floatMake)(0); this.lastTrigger = (0, strong_types_1.floatMake)(0); this.triggerLimit = (0, strong_types_1.uIntMake)(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(); return this.running(true); } /** * Stop current timer. No effect if timer is not running. * @returns Whether timer stopped succesfully. */ stop() { if (!this.running()) { return false; } return this.running(false); } trigger() { // Todo } onUpdate() { // do nothing if (!this.running()) { return; } const elapsed = (0, since_1.timeSince)(this.timeStart()); const seconds = elapsed.asSeconds(); if (seconds === null) { return; } if (seconds < this.interval()) { return; } this.trigger(); } reset() { this.running.reset(); } } exports.TimerPassive = TimerPassive;