refactor: use runtime metadate in runtimeState (#1653)

* change load to use metadata

* update roll to use metadata

* remove filterTimedEvents

* add comment

* atempth to refactor rollUtils

* refactor util functions
This commit is contained in:
Alex Christoffer Rasmussen
2025-06-22 10:32:48 +02:00
committed by GitHub
parent e090bde8bb
commit 14a99ce27b
7 changed files with 698 additions and 395 deletions
File diff suppressed because it is too large Load Diff
+14 -15
View File
@@ -1,22 +1,25 @@
import { dayInMs, getFirstEvent } from 'ontime-utils';
import { OntimeEvent, MaybeNumber, PlayableEvent, isPlayableEvent } from 'ontime-types';
import { dayInMs } from 'ontime-utils';
import { MaybeNumber, PlayableEvent, Rundown } from 'ontime-types';
import { normaliseEndTime } from './timerUtils.js';
import { RundownMetadata } from '../api-data/rundown/rundown.types.js';
import { getTimedIndexFromPlayableIndex } from '../api-data/rundown/rundown.utils.js';
/**
* Finds current event in a rolling rundown
*/
export function loadRoll(
timedEvents: OntimeEvent[],
rundown: Rundown,
metadata: RundownMetadata,
timeNow: number,
): {
event: PlayableEvent | null;
index: MaybeNumber;
isPending?: boolean;
} {
const { firstEvent } = getFirstEvent(timedEvents);
const firstEventId = metadata.playableEventOrder[0];
if (!firstEvent) {
if (!firstEventId) {
return { event: null, index: null };
}
@@ -24,13 +27,8 @@ export function loadRoll(
// account for number of times we went over midnight
let daySpan = 0;
for (let i = 0; i < timedEvents.length; i++) {
const event = timedEvents[i];
if (!isPlayableEvent(event)) {
continue;
}
for (let i = 0; i < metadata.playableEventOrder.length; i++) {
const event = rundown.entries[metadata.playableEventOrder[i]] as PlayableEvent;
if (event.duration === 0) {
continue;
}
@@ -62,16 +60,17 @@ export function loadRoll(
const isFromDayBefore = normalEnd > dayInMs && timeNow < event.timeEnd;
const hasStarted = isFromDayBefore || timeNow >= event.timeStart;
if (hasStarted) {
return { event, index: i };
return { event, index: getTimedIndexFromPlayableIndex(metadata, i) };
}
// 3. event will run in the future
// we set the isPending flag to indicate that the event is currently playing
return { event, index: i, isPending: true };
return { event, index: getTimedIndexFromPlayableIndex(metadata, i), isPending: true };
}
// in case we were unable to find anything, we load the first event
return { event: firstEvent, index: 0, isPending: true };
console.log('returning first event');
return { event: rundown.entries[firstEventId] as PlayableEvent, index: 0, isPending: true };
}
/**
@@ -30,7 +30,6 @@ import { RestorePoint, restoreService } from '../RestoreService.js';
import { skippedOutOfEvent } from '../timerUtils.js';
import {
filterTimedEvents,
findNextPlayableId,
findNextPlayableWithCue,
findPreviousPlayableId,
@@ -190,17 +189,15 @@ class RuntimeService {
}
private isNewNext() {
const rundown = getCurrentRundown();
const { timedEventOrder } = getRundownMetadata();
const timedEvents = filterTimedEvents(rundown, timedEventOrder);
const state = runtimeState.getState();
const now = state.eventNow?.id;
const next = state.eventNext?.id;
// check whether the index of now and next are consecutive
const indexNow = timedEvents.findIndex((event) => event.id === now);
const indexNext = timedEvents.findIndex((event) => event.id === next);
const indexNow = timedEventOrder.findIndex((id) => id === now);
const indexNext = timedEventOrder.findIndex((id) => id === next);
return indexNext - indexNow !== 1;
}
@@ -241,8 +238,8 @@ class RuntimeService {
runtimeState.updateLoaded(eventNow);
} else {
const rundown = getCurrentRundown();
const { timedEventOrder } = getRundownMetadata();
runtimeState.updateAll(rundown, timedEventOrder);
const metadata = getRundownMetadata();
runtimeState.updateAll(rundown, metadata);
}
return;
}
@@ -252,9 +249,8 @@ class RuntimeService {
isNext = this.isNewNext();
if (isNext) {
const rundown = getCurrentRundown();
const { timedEventOrder } = getRundownMetadata();
const timedEvents = filterTimedEvents(rundown, timedEventOrder);
runtimeState.loadNext(timedEvents);
const metadata = getRundownMetadata();
runtimeState.loadNext(rundown, metadata);
}
}
@@ -273,8 +269,8 @@ class RuntimeService {
// we can ignore events which are not playable
const rundown = getCurrentRundown();
const rundownMetadata = getRundownMetadata();
const success = runtimeState.load(event, rundown, rundownMetadata.playableEventOrder, initialData);
const metadata = getRundownMetadata();
const success = runtimeState.load(event, rundown, metadata, initialData);
if (success) {
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
@@ -603,10 +599,10 @@ class RuntimeService {
*/
private rollLoaded(offset?: number) {
const rundown = getCurrentRundown();
const { timedEventOrder } = getRundownMetadata();
const metadata = getRundownMetadata();
try {
runtimeState.roll(rundown, timedEventOrder, offset);
runtimeState.roll(rundown, metadata, offset);
} catch (error) {
logger.error(LogOrigin.Server, `Roll: ${error}`);
}
@@ -627,8 +623,8 @@ class RuntimeService {
try {
const rundown = getCurrentRundown();
const rundownMetadata = getRundownMetadata();
const result = runtimeState.roll(rundown, rundownMetadata.playableEventOrder);
const metadata = getRundownMetadata();
const result = runtimeState.roll(rundown, metadata);
const newState = runtimeState.getState();
if (result.eventId !== previousState.eventNow?.id) {
@@ -681,8 +677,8 @@ class RuntimeService {
}
const rundown = getCurrentRundown();
const rundownMetadata = getRundownMetadata();
runtimeState.resume(restorePoint, event, rundown, rundownMetadata.playableEventOrder);
const metadata = getRundownMetadata();
runtimeState.resume(restorePoint, event, rundown, metadata);
logger.info(LogOrigin.Playback, 'Resuming playback');
}
@@ -120,10 +120,3 @@ export function getEventAtIndex(
return rundown.entries[eventId] as OntimeEvent | undefined;
}
/**
* TODO(v4): we dont need this function
*/
export function filterTimedEvents(rundown: Rundown, timedEventOrder: EntryId[]): OntimeEvent[] {
return timedEventOrder.map((id) => rundown.entries[id] as OntimeEvent);
}