refactor: restructure model to contain an object of rundowns

This commit is contained in:
Carlos Valente
2025-03-15 09:04:33 +01:00
committed by arc-alex
parent 0a80d6db31
commit abd9b127db
111 changed files with 4357 additions and 4515 deletions
+35 -30
View File
@@ -1,26 +1,20 @@
import {
CurrentBlockState,
EntryId,
isPlayableEvent,
MaybeNumber,
MaybeString,
OffsetMode,
OntimeBlock,
OntimeEvent,
OntimeRundown,
PlayableEvent,
Playback,
Rundown,
Runtime,
runtimeStorePlaceholder,
TimerPhase,
TimerState,
} from 'ontime-types';
import {
calculateDuration,
checkIsNow,
dayInMs,
filterTimedEvents,
getPreviousBlock,
isPlaybackActive,
} from 'ontime-utils';
import { calculateDuration, checkIsNow, dayInMs, isPlaybackActive } from 'ontime-utils';
import { timeNow } from '../utils/time.js';
import type { RestorePoint } from '../services/RestoreService.js';
@@ -33,6 +27,7 @@ import {
} from '../services/timerUtils.js';
import { timerConfig } from '../config/config.js';
import { loadRoll, normaliseRollStart } from '../services/rollUtils.js';
import { filterTimedEvents } from '../services/rundown-service/rundownUtils.js';
export type RuntimeState = {
clock: number; // realtime clock
@@ -183,19 +178,23 @@ export function updateRundownData(rundownData: RundownData) {
*/
export function load(
event: PlayableEvent,
rundown: OntimeRundown,
rundown: Rundown,
timedEventsOrder: EntryId[],
initialData?: Partial<TimerState & RestorePoint>,
): boolean {
clearEventData();
// filter rundown
const timedEvents = filterTimedEvents(rundown);
const eventIndex = timedEvents.findIndex((eventInMemory) => eventInMemory.id === event.id);
if (timedEvents.length === 0 || eventIndex === -1 || !isPlayableEvent(event)) {
if (timedEventsOrder.length === 0 || !isPlayableEvent(event)) {
return false;
}
// filter rundown
const eventIndex = timedEventsOrder.findIndex((timedEventId) => timedEventId === event.id);
if (eventIndex === -1) {
return false;
}
const timedEvents = filterTimedEvents(rundown, timedEventsOrder);
// load events in memory along with their data
loadNow(timedEvents, eventIndex);
loadNext(timedEvents, eventIndex);
@@ -312,8 +311,8 @@ export function loadNext(
/**
* Resume from restore point
*/
export function resume(restorePoint: RestorePoint, event: PlayableEvent, rundown: OntimeRundown) {
load(event, rundown, restorePoint);
export function resume(restorePoint: RestorePoint, event: PlayableEvent, rundown: Rundown, timedEventOrder: EntryId[]) {
load(event, rundown, timedEventOrder, restorePoint);
}
/**
@@ -373,8 +372,8 @@ export function updateLoaded(event?: PlayableEvent): string | undefined {
/**
* Used in situations when we want to hot-reload all events without interrupting timer
*/
export function updateAll(rundown: OntimeRundown) {
const timedEvents = filterTimedEvents(rundown);
export function updateAll(rundown: Rundown, timedEventsOrder: EntryId[]) {
const timedEvents = filterTimedEvents(rundown, timedEventsOrder);
loadNow(timedEvents);
loadNext(timedEvents);
updateLoaded(runtimeState.eventNow ?? undefined);
@@ -388,6 +387,7 @@ export function start(state: RuntimeState = runtimeState): boolean {
if (state.timer.playback === Playback.Play) {
return false;
}
state.clock = timeNow();
state.timer.secondaryTimer = null;
@@ -576,7 +576,11 @@ export function update(): UpdateResult {
}
}
export function roll(rundown: OntimeRundown, offset = 0): { eventId: MaybeString; didStart: boolean } {
export function roll(
rundown: Rundown,
timedEventOrder: EntryId[],
offset = 0,
): { eventId: MaybeString; didStart: boolean } {
// 1. if an event is running, we simply take over the playback
if (runtimeState.timer.playback === Playback.Play && runtimeState.runtime.selectedEventIndex !== null) {
runtimeState.timer.playback = Playback.Roll;
@@ -633,7 +637,7 @@ export function roll(rundown: OntimeRundown, offset = 0): { eventId: MaybeString
}
// 3. if there is no event running, we need to find the next event
const timedEvents = filterTimedEvents(rundown);
const timedEvents = filterTimedEvents(rundown, timedEventOrder);
if (timedEvents.length === 0) {
throw new Error('No playable events found');
}
@@ -709,9 +713,8 @@ export function roll(rundown: OntimeRundown, offset = 0): { eventId: MaybeString
/**
* handle block loading, not for use outside of runtimeState
* @param rundown
*/
export function loadBlock(rundown: OntimeRundown, state = runtimeState) {
export function loadBlock(rundown: Rundown, state = runtimeState) {
if (state.eventNow === null) {
// we need a loaded event to have a block
state.currentBlock.block = null;
@@ -719,17 +722,19 @@ export function loadBlock(rundown: OntimeRundown, state = runtimeState) {
return;
}
const newCurrentBlock = getPreviousBlock(rundown, state.eventNow.id);
const currentBlockId = state.eventNow.currentBlock;
// update time only if the block has changed
if (state.currentBlock.block?.id !== newCurrentBlock?.id) {
if (state.currentBlock.block?.id != currentBlockId) {
state.currentBlock.startedAt = null;
}
// update the block anyway
state.currentBlock.block = newCurrentBlock === null ? null : { ...newCurrentBlock };
}
if (currentBlockId === null) {
state.currentBlock.block = null;
return;
}
export function setOffsetMode(mode: OffsetMode) {
runtimeState.runtime.offsetMode = mode;
const currentBlock = rundown.entries[currentBlockId];
state.currentBlock.block = currentBlock as OntimeBlock;
}