fix: midnight roll (#484)

* refactor: extract utilities and types

* refactor: extract and simplify progress bar logic

* refactor: duration validation handles midnight

* fix: prevent stale event loader

* refactor: consider next event in timer invalidation

* refactor: reload changes on changed roll target

* refactor: timer load accounts for midnight

* fix: issues with midnight on roll

* refactor: prevent stale secondary event
This commit is contained in:
Carlos Valente
2023-08-12 10:42:43 +02:00
committed by GitHub
parent 8c9cb908cf
commit 023aac4ead
28 changed files with 520 additions and 271 deletions
+20 -4
View File
@@ -1,4 +1,5 @@
type MaybeNumber = number | null;
import { MaybeNumber } from 'ontime-types';
import { dayInMs } from 'ontime-utils';
/**
* Calculates expected finish time of a running timer
@@ -18,7 +19,14 @@ export function getExpectedFinish(
return finishedAt;
}
return Math.max(startedAt + duration + pausedTime + addedTime, startedAt);
// handle events that finish the day after
const expectedFinish = startedAt + duration + pausedTime + addedTime;
if (expectedFinish > dayInMs) {
return expectedFinish - dayInMs;
}
// an event cannot finish before it started (user added too much negative time)
return Math.max(expectedFinish, startedAt);
}
/**
@@ -34,15 +42,23 @@ export function getCurrent(
if (startedAt === null) {
return null;
}
if (startedAt > clock) {
return startedAt + duration + addedTime + pausedTime - clock - dayInMs;
}
return startedAt + duration + addedTime + pausedTime - clock;
}
/**
* Calculates elapsed time
*/
export function getElapsed(startedAt: number, clock: number) {
export function getElapsed(startedAt: number | null, clock: number): number | null {
if (startedAt === null) {
return null;
}
// we are in the day after
if (startedAt > clock) {
throw new Error('clock cannot be higher than startedAt');
return dayInMs - startedAt + clock;
}
return clock - startedAt;
}