block end calculation (#1683)

This commit is contained in:
Alex Christoffer Rasmussen
2025-07-13 17:01:21 +02:00
committed by Carlos Valente
parent e811a82be2
commit e9dda817e5
15 changed files with 474 additions and 376 deletions
+55 -2
View File
@@ -1,12 +1,65 @@
import { MaybeNumber, TimerPhase } from 'ontime-types';
import { dayInMs, isPlaybackActive } from 'ontime-utils';
import { isOntimeEvent, MaybeNumber, OntimeBlock, OntimeEvent, Rundown, TimerPhase } from 'ontime-types';
import { calculateTimeUntilStart, dayInMs, isPlaybackActive } from 'ontime-utils';
import type { RuntimeState } from '../stores/runtimeState.js';
import { shouldCrashDev } from '../utils/development.js';
/**
* handle events that span over midnight
*/
export const normaliseEndTime = (start: number, end: number) => (end < start ? end + dayInMs : end);
/**
* Calculates the expected time of the group to end.
* Should only be called if a block is running
* TODO: take a look at how it handles relative offset mode
*/
export function getExpectedBlockFinish(state: RuntimeState, rundown: Rundown): MaybeNumber {
const { blockNow, eventNow, timer, clock } = state;
if (blockNow === null) return null;
// if the group doesn't have a start time there is no end time either
if (blockNow.startedAt === null) return null;
if (eventNow === null) return null;
if (timer.current === null) return null;
const { entries } = rundown;
const orderInBlock = (entries[blockNow.id] as OntimeBlock).entries;
const indexInBlock = orderInBlock.findIndex((id) => eventNow.id === id);
shouldCrashDev(indexInBlock < 0, 'Running event is not in current block');
if (indexInBlock === orderInBlock.length - 1) return timer.expectedFinish;
let totalGap = 0;
let isLinkedToLoaded = true;
for (let i = indexInBlock + 1; i < orderInBlock.length; i++) {
const entry = entries[orderInBlock[i]];
if (isOntimeEvent(entry)) {
totalGap += entry.gap;
isLinkedToLoaded = isLinkedToLoaded && entry.linkStart;
}
}
const lastEntry = entries[orderInBlock.at(-1)!] as OntimeEvent;
const { offsetMode, offset, plannedStart, actualStart } = state.runtime;
const timeUntilLastEvent = calculateTimeUntilStart({
timeStart: lastEntry.timeStart,
dayOffset: lastEntry.dayOffset,
delay: lastEntry.delay,
currentDay: eventNow.dayOffset,
totalGap,
isLinkedToLoaded,
clock,
offsetMode,
offset,
plannedStart,
actualStart,
});
return clock + timeUntilLastEvent + lastEntry.duration;
}
/**
* Calculates expected finish time of a running timer
* @param {RuntimeState} state runtime state