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
@@ -1,5 +1,11 @@
import { OntimeRundown, PlayableEvent, Playback, SupportedEvent, TimerPhase } from 'ontime-types';
import { deepmerge } from 'ontime-utils';
import { PlayableEvent, Playback, TimerPhase } from 'ontime-types';
import { initRundown } from '../../services/rundown-service/RundownService.js';
import {
makeOntimeBlock,
makeOntimeEvent,
makeRundown,
} from '../../services/rundown-service/__mocks__/rundown.mocks.js';
import {
type RuntimeState,
@@ -13,7 +19,6 @@ import {
start,
stop,
} from '../runtimeState.js';
import { initRundown } from '../../services/rundown-service/RundownService.js';
const mockEvent = {
type: 'event',
@@ -23,6 +28,7 @@ const mockEvent = {
timeEnd: 1000,
duration: 1000,
skip: false,
currentBlock: null,
} as PlayableEvent;
const mockState = {
@@ -51,11 +57,6 @@ const mockState = {
},
} as RuntimeState;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const makeMockState = (patch: RuntimeState): RuntimeState => {
return deepmerge(mockState, patch);
};
beforeAll(() => {
vi.mock('../../classes/data-provider/DataProvider.js', () => {
return {
@@ -70,23 +71,14 @@ beforeAll(() => {
});
describe('mutation on runtimeState', () => {
beforeEach(() => {
beforeEach(async () => {
clearState();
vi.mock('../../services/rundown-service/RundownService.js', async (importOriginal) => {
const actual = (await importOriginal()) as object;
return {
...actual,
getPlayableEvents: vi.fn().mockReturnValue([
{
id: 'mock',
cue: 'mock',
timeStart: 0,
timeEnd: 1000,
duration: 1000,
},
]),
initRunddown: vi.fn().mockReturnValue(undefined),
};
});
});
@@ -97,15 +89,18 @@ describe('mutation on runtimeState', () => {
describe('playback operations', async () => {
it('refuses if nothing is loaded', () => {
initRundown(makeRundown({}), {});
let success = start(mockState);
expect(success).toBe(false);
success = pause();
expect(success).toBe(false);
});
test('normal playback cycle', () => {
// 1. Load event
load(mockEvent, [mockEvent]);
const mockRundown = makeRundown({ entries: { [mockEvent.id]: mockEvent }, order: [mockEvent.id] });
load(mockEvent, mockRundown, mockRundown.order);
let newState = getState();
expect(newState.eventNow?.id).toBe(mockEvent.id);
expect(newState.timer.playback).toBe(Playback.Armed);
@@ -170,17 +165,21 @@ describe('mutation on runtimeState', () => {
});
// do this before the test so that it is applied
const event1 = { ...mockEvent, id: 'event1', timeStart: 0, timeEnd: 1000, duration: 1000 };
const event2 = { ...mockEvent, id: 'event2', timeStart: 1000, timeEnd: 1500, duration: 500 };
const entries = {
event1: { ...mockEvent, id: 'event1', timeStart: 0, timeEnd: 1000, duration: 1000, currentBlock: null },
event2: { ...mockEvent, id: 'event2', timeStart: 1000, timeEnd: 1500, duration: 500, currentBlock: null },
};
const rundown = makeRundown({ entries, order: ['event1', 'event2'] });
// force update
vi.useFakeTimers();
await initRundown([event1, event2], {});
await initRundown(rundown, {});
vi.runAllTimers();
vi.useRealTimers();
test('runtime offset', async () => {
// 1. Load event
load(event1, [event1, event2]);
load(entries.event1, rundown, rundown.order);
let newState = getState();
expect(newState.runtime.actualStart).toBeNull();
expect(newState.runtime.plannedStart).toBe(0);
@@ -197,11 +196,11 @@ describe('mutation on runtimeState', () => {
}
expect(newState.runtime.actualStart).toBe(newState.clock);
expect(newState.runtime.offset).toBe(event1.timeStart - newState.clock);
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset);
expect(newState.runtime.offset).toBe(entries.event1.timeStart - newState.clock);
expect(newState.runtime.expectedEnd).toBe(entries.event2.timeEnd - newState.runtime.offset);
// 3. Next event
load(event2, [event1, event2]);
load(entries.event2, rundown, rundown.order);
start();
newState = getState();
@@ -214,10 +213,10 @@ describe('mutation on runtimeState', () => {
const forgivingActualStart = Math.abs(newState.runtime.actualStart - firstStart);
expect(forgivingActualStart).toBeLessThanOrEqual(1);
// we are over-under, the difference between the schedule and the actual start
const delayBefore = event2.timeStart - newState.clock;
const delayBefore = entries.event2.timeStart - newState.clock;
expect(newState.runtime.offset).toBe(delayBefore);
// finish is the difference between the runtime and the schedule
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset);
expect(newState.runtime.expectedEnd).toBe(entries.event2.timeEnd - newState.runtime.offset);
expect(newState.currentBlock.block).toBeNull();
// 4. Add time
@@ -228,7 +227,7 @@ describe('mutation on runtimeState', () => {
}
expect(newState.runtime.offset).toBe(delayBefore - 10);
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset);
expect(newState.runtime.expectedEnd).toBe(entries.event2.timeEnd - newState.runtime.offset);
// 5. Stop event
stop();
@@ -237,8 +236,6 @@ describe('mutation on runtimeState', () => {
expect(newState.runtime.offset).toBe(0);
expect(newState.runtime.expectedEnd).toBeNull();
});
test.todo('runtime offset on timers in overtime', () => {});
});
});
@@ -253,14 +250,17 @@ describe('roll mode', () => {
});
describe('normal roll', () => {
const rundown = [
{ ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
{ ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
{ ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
] as PlayableEvent[];
const rundown = makeRundown({
entries: {
1: { ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
2: { ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
3: { ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
},
order: ['1', '2', '3'],
});
test('pending event', () => {
const { eventId, didStart } = roll(rundown);
const { eventId, didStart } = roll(rundown, rundown.order);
const state = getState();
expect(eventId).toBe('1');
@@ -271,29 +271,32 @@ describe('roll mode', () => {
test('roll events', () => {
vi.setSystemTime('jan 1 00:00:01');
let result = roll(rundown);
let result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '1', didStart: true });
vi.setSystemTime('jan 1 00:00:02');
result = roll(rundown);
result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '2', didStart: true });
vi.setSystemTime('jan 1 00:00:03:500');
result = roll(rundown);
result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '3', didStart: true });
});
});
describe('roll takover', () => {
const rundown = [
{ ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
{ ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
{ ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
] as PlayableEvent[];
const rundown = makeRundown({
entries: {
1: { ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
2: { ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
3: { ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
},
order: ['1', '2', '3'],
});
test('from load', () => {
load(rundown[2], rundown);
const result = roll(rundown);
load(rundown.entries[3] as PlayableEvent, rundown, rundown.order);
const result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '3', didStart: false });
const state = getState();
expect(state.timer.phase).toBe(TimerPhase.Pending);
@@ -301,9 +304,9 @@ describe('roll mode', () => {
});
test('from play', () => {
load(rundown[0], rundown);
load(rundown.entries[1] as PlayableEvent, rundown, rundown.order);
start();
const result = roll(rundown);
const result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '1', didStart: false });
expect(getState().runtime.offset).toBe(1000);
});
@@ -311,153 +314,167 @@ describe('roll mode', () => {
describe('roll continue with offset', () => {
test('no gaps', () => {
const rundown = [
{ ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
{ ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
{ ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
] as PlayableEvent[];
const rundown = makeRundown({
entries: {
1: { ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
2: { ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
3: { ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
},
order: ['1', '2', '3'],
});
load(rundown[0], rundown);
load(rundown.entries[1] as PlayableEvent, rundown, rundown.order);
start();
let result = roll(rundown, getState().runtime.offset);
let result = roll(rundown, rundown.order, getState().runtime.offset);
expect(result).toStrictEqual({ eventId: '1', didStart: false });
expect(getState().runtime.offset).toBe(1000);
vi.setSystemTime('jan 1 00:00:01');
result = roll(rundown, getState().runtime.offset);
result = roll(rundown, rundown.order, getState().runtime.offset);
expect(result).toStrictEqual({ eventId: '2', didStart: true });
expect(getState().runtime.offset).toBe(1000);
vi.setSystemTime('jan 1 00:00:02');
result = roll(rundown, getState().runtime.offset);
result = roll(rundown, rundown.order, getState().runtime.offset);
expect(result).toStrictEqual({ eventId: '3', didStart: true });
expect(getState().runtime.offset).toBe(1000);
});
test.todo('with gaps', () => {
//this is a bit involved as it also depends somewhat on the RintimeService
});
});
});
describe('loadBlock', () => {
test('from no-block to a block will clear startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Block },
{ id: '2', type: SupportedEvent.Event },
{ id: '3', type: SupportedEvent.Block },
{ id: '4', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeBlock({ id: '1', events: ['11'] }),
11: makeOntimeEvent({ id: '11', currentBlock: '1' }),
2: makeOntimeBlock({ id: '2', events: [] }),
3: makeOntimeEvent({ id: '3', currentBlock: null }),
},
order: ['0', '1', '2', '3'],
});
const state = {
currentBlock: {
block: null,
startedAt: 123,
},
eventNow: rundown[2],
eventNow: rundown.entries[11],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: rundown[1], startedAt: null },
eventNow: rundown[2],
currentBlock: { block: rundown.entries[1], startedAt: null },
eventNow: rundown.entries[11],
});
});
test('from block to a different block will clear startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Block },
{ id: '2', type: SupportedEvent.Event },
{ id: '3', type: SupportedEvent.Block },
{ id: '4', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeBlock({ id: '1', events: ['11'] }),
11: makeOntimeEvent({ id: '11', currentBlock: '1' }),
2: makeOntimeBlock({ id: '2', events: ['22'] }),
22: makeOntimeEvent({ id: '22', currentBlock: '2' }),
},
order: ['0', '1', '2'],
});
const state = {
currentBlock: {
block: rundown[1],
block: rundown.entries[1],
startedAt: 123,
},
eventNow: rundown[4],
eventNow: rundown.entries[22],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: rundown[3], startedAt: null },
eventNow: rundown[4],
currentBlock: { block: rundown.entries[2], startedAt: null },
eventNow: rundown.entries[22],
});
});
test('from block to a no-block will clear startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Block },
{ id: '2', type: SupportedEvent.Event },
{ id: '3', type: SupportedEvent.Block },
{ id: '4', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeBlock({ id: '1', events: ['11'] }),
11: makeOntimeEvent({ id: '11', currentBlock: '1' }),
2: makeOntimeBlock({ id: '2', events: ['22'] }),
22: makeOntimeEvent({ id: '22', currentBlock: '2' }),
},
order: ['0', '1', '2'],
});
const state = {
currentBlock: {
block: rundown[1],
block: rundown.entries[1],
startedAt: 123,
},
eventNow: rundown[0],
eventNow: rundown.entries[0],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: null, startedAt: null },
eventNow: rundown[0],
eventNow: rundown.entries[0],
});
});
test('from block to same block will keep startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Block },
{ id: '1', type: SupportedEvent.Event },
{ id: '2', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeBlock({ id: '0', events: ['1', '2'] }),
1: makeOntimeEvent({ id: '1', currentBlock: '0' }),
2: makeOntimeEvent({ id: '2', currentBlock: '0' }),
},
order: ['0'],
});
const state = {
currentBlock: {
block: rundown[0],
block: rundown.entries[0],
startedAt: 123,
},
eventNow: rundown[2],
eventNow: rundown.entries[2],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: rundown[0], startedAt: 123 },
eventNow: rundown[2],
currentBlock: { block: rundown.entries[0], startedAt: 123 },
eventNow: rundown.entries[2],
});
});
test('from no-block to no-block will keep startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeEvent({ id: '1', currentBlock: null }),
},
order: ['0', '1'],
});
const state = {
currentBlock: {
block: null,
startedAt: 123,
},
eventNow: rundown[0],
eventNow: rundown.entries[0],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: null, startedAt: 123 },
eventNow: rundown[0],
eventNow: rundown.entries[0],
});
});
});
+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;
}