refactor(timer): elapsed is active time from start

This commit is contained in:
Carlos Valente
2026-06-28 19:12:22 +02:00
committed by Carlos Valente
parent 4f4626a163
commit 161ab3fe13
12 changed files with 196 additions and 6 deletions
+27
View File
@@ -92,6 +92,33 @@ export function getCurrent(state: RuntimeState): number {
return startedAt + duration + addedTime - clock - correctDay;
}
/**
* Calculates active time elapsed since the timer started.
*/
export function getElapsed(state: RuntimeState): MaybeNumber {
const { clock } = state;
const { startedAt } = state.timer;
const { pausedAt, pausedDuration } = state._timer;
if (startedAt === null) {
return null;
}
const referenceClock = pausedAt ?? clock;
const elapsedSinceStart = getTimeSinceStart(referenceClock, startedAt);
const activeElapsed = elapsedSinceStart - pausedDuration;
return Math.max(0, activeElapsed);
}
function getTimeSinceStart(clock: TimeOfDay, startedAt: number): number {
if (clock < startedAt) {
return clock + dayInMs - startedAt;
}
return clock - startedAt;
}
/**
* Checks whether we have skipped out of the event
* @param {RuntimeState} state runtime state