sussy-util
Version:
Util package made by me
120 lines (119 loc) • 3.79 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_events_1 = require("node:events");
const Optional_1 = __importDefault(require("./Optional"));
class StopWatch extends node_events_1.EventEmitter {
constructor() {
super();
this.rounds = [];
this.totalTimePaused = 0;
this.startTime = performance.now();
}
/**
* Fixes the elapsed time when the stopwatch is paused.
*/
fixPausedTime() {
if (this.pauseStartTime) {
const time = performance.now();
this.totalTimePaused += time - this.pauseStartTime;
this.pauseStartTime = time;
}
}
/**
* Records a round by calculating the lap time and emitting a 'round' event.
*/
round() {
this.fixPausedTime();
const lapTime = performance.now() -
(this.totalTimePaused + this.startTime + this.rounds.reduce((acc, val) => acc + val, 0));
this.rounds.push(lapTime);
this.emit('round', lapTime);
if (lapTime > (this.threshold ? this.threshold : Infinity)) {
this.emit('lapExceededThreshold', lapTime);
}
}
/**
* Resets the stopwatch to its initial state and emits a 'reset' event.
*/
reset() {
this.emit('reset', this.time(), [...this.rounds]);
this.startTime = performance.now();
this.rounds.length = 0;
this.totalTimePaused = 0;
this.pauseStartTime = void 0;
}
/**
* Returns the elapsed time in milliseconds.
* @returns The elapsed time in milliseconds.
*/
time() {
this.fixPausedTime();
return performance.now() - this.startTime - this.totalTimePaused;
}
/**
* Returns the recorded rounds.
* @returns An array of lap times for each round.
*/
getRounds() {
return [...this.rounds];
}
/**
* Pauses the stopwatch and emits a 'pause' event.
*/
pause() {
if (!this.pauseStartTime) {
this.pauseStartTime = performance.now();
this.emit('pause');
}
}
/**
* Resumes the stopwatch if paused and emits a 'resume' event.
*/
resume() {
if (this.pauseStartTime) {
this.fixPausedTime();
this.pauseStartTime = void 0;
this.emit('resume');
}
}
formatLapTime(lapTime) {
const minutes = Math.floor(lapTime / 60000);
const seconds = Math.floor((lapTime % 60000) / 1000);
const milliseconds = Math.floor(lapTime % 1000);
return `${minutes}:${seconds.toString().padStart(2, '0')}.${milliseconds.toString().padStart(3, '0')}`;
}
getAverageLapTime() {
const totalLaps = this.rounds.length;
if (totalLaps === 0) {
return 0;
}
const totalTime = this.rounds.reduce((acc, lapTime) => acc + lapTime, 0);
return totalTime / totalLaps;
}
getMaximumLapTime() {
return Math.max(...this.rounds);
}
getMinimumLapTime() {
return Math.min(...this.rounds);
}
setLapTimeThreshold(threshold) {
this.threshold = threshold;
}
getLapTimeAtIndex(index) {
if (index >= 0 && index < this.rounds.length) {
return Optional_1.default.of(this.rounds[index]);
}
return Optional_1.default.empty();
}
/**
* Adds an event listener for the 'round' event.
* @param callback - The callback function to be called when a 'round' event is emitted.
*/
onRound(callback) {
this.on('round', callback);
}
}
exports.default = StopWatch;