mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 22:49:18 +00:00
@@ -1,9 +1,10 @@
|
||||
import { OntimeEvent, Playback } from 'ontime-types';
|
||||
import { deepmerge } from 'ontime-utils';
|
||||
|
||||
import { RuntimeState, clear, getState, load, pause, start, stop } from '../runtimeState.js';
|
||||
import { RuntimeState, addTime, clear, getState, load, pause, start, stop } from '../runtimeState.js';
|
||||
|
||||
const mockEvent = {
|
||||
type: 'event',
|
||||
id: 'mock',
|
||||
cue: 'mock',
|
||||
timeStart: 0,
|
||||
@@ -88,6 +89,7 @@ describe('mutation on runtimeState', () => {
|
||||
expect(newState.timer).toMatchObject({
|
||||
playback: Playback.Play,
|
||||
});
|
||||
expect(newState.runtime.actualStart).toBe(newState.clock);
|
||||
|
||||
// 3. Pause event
|
||||
success = pause();
|
||||
@@ -122,7 +124,7 @@ describe('mutation on runtimeState', () => {
|
||||
);
|
||||
expect(newState._timer.pausedAt).toBeNull();
|
||||
|
||||
// 4. Stop event
|
||||
// 5. Stop event
|
||||
success = stop();
|
||||
expect(success).toBe(true);
|
||||
expect(newState.eventNow).toBe(null);
|
||||
@@ -133,8 +135,55 @@ describe('mutation on runtimeState', () => {
|
||||
expectedFinish: null,
|
||||
startedAt: null,
|
||||
});
|
||||
expect(newState.runtime.actualStart).toBeNull();
|
||||
});
|
||||
|
||||
test('runtime offset', () => {
|
||||
const event1 = { ...mockEvent, id: 'event1', timeStart: 0, timeEnd: 1000, duration: 1000 };
|
||||
const event2 = { ...mockEvent, id: 'event2', timeStart: 1000, timeEnd: 1500, duration: 500 };
|
||||
|
||||
// 1. Load event
|
||||
load(event1, [event1, event2]);
|
||||
let newState = getState();
|
||||
expect(newState.runtime.actualStart).toBeNull();
|
||||
expect(newState.runtime.plannedStart).toBe(0);
|
||||
expect(newState.runtime.plannedEnd).toBe(1500);
|
||||
|
||||
// 2. Start event
|
||||
start();
|
||||
newState = getState();
|
||||
const firstStart = newState.clock;
|
||||
expect(newState.runtime.actualStart).toBe(newState.clock);
|
||||
expect(newState.runtime.offset).toBe(newState.clock - event1.timeStart);
|
||||
expect(newState.runtime.expectedEnd).toBe(newState.runtime.offset + event2.timeEnd);
|
||||
|
||||
// 3. Next event
|
||||
load(event2, [event1, event2]);
|
||||
start();
|
||||
newState = getState();
|
||||
expect(newState.runtime.actualStart).toBe(firstStart);
|
||||
// we are over-under, the difference between the schedule and the actual start
|
||||
const delayBefore = newState.clock - event2.timeStart;
|
||||
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);
|
||||
|
||||
// 4. Add time
|
||||
addTime(10);
|
||||
newState = getState();
|
||||
expect(newState.runtime.offset).toBe(delayBefore + 10);
|
||||
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset);
|
||||
|
||||
// 5. Stop event
|
||||
stop();
|
||||
newState = getState();
|
||||
expect(newState.runtime.actualStart).toBeNull();
|
||||
expect(newState.runtime.offset).toBe(0);
|
||||
expect(newState.runtime.expectedEnd).toBeNull();
|
||||
});
|
||||
|
||||
test.todo('runtime offset on timers in overtime', () => {});
|
||||
|
||||
test.todo('roll mode', () => {});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,15 +1,27 @@
|
||||
import { Runtime, OntimeEvent, Playback, TimerState, TimerType, MaybeNumber } from 'ontime-types';
|
||||
import { calculateDuration, dayInMs } from 'ontime-utils';
|
||||
import { calculateDuration, dayInMs, getFirstEvent, getLastEvent } from 'ontime-utils';
|
||||
|
||||
import { clock } from '../services/Clock.js';
|
||||
import { RestorePoint } from '../services/RestoreService.js';
|
||||
import { getPlayableEvents } from '../services/rundown-service/RundownService.js';
|
||||
import { getCurrent, getExpectedFinish, getRollTimers, skippedOutOfEvent, updateRoll } from '../services/timerUtils.js';
|
||||
import {
|
||||
getCurrent,
|
||||
getExpectedFinish,
|
||||
getRollTimers,
|
||||
getRuntimeOffset,
|
||||
skippedOutOfEvent,
|
||||
updateRoll,
|
||||
} from '../services/timerUtils.js';
|
||||
import { timerConfig } from '../config/config.js';
|
||||
|
||||
const initialRuntime: Runtime = {
|
||||
selectedEventIndex: null,
|
||||
numEvents: 0,
|
||||
offset: 0,
|
||||
plannedStart: 0,
|
||||
plannedEnd: 0,
|
||||
actualStart: null,
|
||||
expectedEnd: null,
|
||||
};
|
||||
|
||||
const initialTimer: TimerState = {
|
||||
@@ -64,13 +76,13 @@ export function getState(): Readonly<RuntimeState> {
|
||||
}
|
||||
|
||||
export function clear() {
|
||||
// TODO: check that entire state is reset here
|
||||
runtimeState.eventNow = null;
|
||||
runtimeState.publicEventNow = null;
|
||||
runtimeState.eventNext = null;
|
||||
runtimeState.publicEventNext = null;
|
||||
|
||||
runtimeState.runtime = { ...initialRuntime };
|
||||
runtimeState.runtime = { ...initialRuntime, actualStart: runtimeState.runtime.actualStart };
|
||||
// TODO: can we cleanup the initialisation of runtime state?
|
||||
runtimeState.runtime.numEvents = fetchNumEvents();
|
||||
|
||||
runtimeState.timer.playback = Playback.Stop;
|
||||
@@ -106,11 +118,17 @@ function fetchNumEvents(): number {
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility, allows updating the number of events
|
||||
* Utility, allows updating data derived from the rundown
|
||||
* @param numEvents
|
||||
*/
|
||||
export function updateNumEvents(numEvents: number) {
|
||||
runtimeState.runtime.numEvents = numEvents;
|
||||
export function updateRundownData(playableRundown: OntimeEvent[]) {
|
||||
runtimeState.runtime.numEvents = playableRundown.length;
|
||||
|
||||
const { firstEvent } = getFirstEvent(playableRundown);
|
||||
const { lastEvent } = getLastEvent(playableRundown);
|
||||
|
||||
runtimeState.runtime.plannedStart = firstEvent?.timeStart ?? null;
|
||||
runtimeState.runtime.plannedEnd = lastEvent?.timeEnd ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,9 +137,11 @@ export function updateNumEvents(numEvents: number) {
|
||||
* @param rundown
|
||||
* @param initialData
|
||||
*/
|
||||
export function load(event: OntimeEvent, rundown: OntimeEvent[], initialData?: Partial<TimerState>) {
|
||||
export function load(event: OntimeEvent, rundown: OntimeEvent[], initialData?: Partial<TimerState & RestorePoint>) {
|
||||
clear();
|
||||
|
||||
updateRundownData(rundown);
|
||||
|
||||
const eventIndex = rundown.findIndex((eventInMemory) => eventInMemory.id === event.id);
|
||||
|
||||
runtimeState.runtime.selectedEventIndex = eventIndex;
|
||||
@@ -137,6 +157,13 @@ export function load(event: OntimeEvent, rundown: OntimeEvent[], initialData?: P
|
||||
|
||||
if (initialData) {
|
||||
patchTimer(initialData);
|
||||
|
||||
const firstStart = initialData?.firstStart;
|
||||
if (firstStart === null || typeof firstStart === 'number') {
|
||||
runtimeState.runtime.actualStart = firstStart;
|
||||
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
||||
runtimeState.runtime.expectedEnd = runtimeState.runtime.plannedEnd + runtimeState.runtime.offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,6 +283,15 @@ export function start(state: RuntimeState = runtimeState): boolean {
|
||||
state.timer.playback = Playback.Play;
|
||||
state.timer.expectedFinish = getExpectedFinish(state);
|
||||
state.timer.elapsed = 0;
|
||||
|
||||
// update runtime delays: over - under
|
||||
if (state.runtime.actualStart === null) {
|
||||
state.runtime.actualStart = state.clock;
|
||||
}
|
||||
|
||||
state.runtime.offset = getRuntimeOffset(state);
|
||||
state.runtime.expectedEnd = state.runtime.plannedEnd + state.runtime.offset;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -274,6 +310,7 @@ export function stop(state: RuntimeState = runtimeState): boolean {
|
||||
if (state.timer.playback === Playback.Stop) {
|
||||
return false;
|
||||
}
|
||||
runtimeState.runtime.actualStart = null;
|
||||
clear();
|
||||
return true;
|
||||
}
|
||||
@@ -298,6 +335,10 @@ export function addTime(amount: number) {
|
||||
runtimeState.timer.finishedAt = null;
|
||||
}
|
||||
}
|
||||
|
||||
// update runtime delays: over - under
|
||||
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
||||
runtimeState.runtime.expectedEnd = runtimeState.runtime.plannedEnd + runtimeState.runtime.offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -318,6 +359,9 @@ export function update(force: boolean, updateInterval: number) {
|
||||
_force = true;
|
||||
}
|
||||
|
||||
// update offset
|
||||
runtimeState.runtime.offset = getRuntimeOffset(runtimeState);
|
||||
|
||||
// we call integrations if we update timers
|
||||
if (runtimeState.timer.playback === Playback.Roll) {
|
||||
const result = roll();
|
||||
|
||||
Reference in New Issue
Block a user