add relevantBlock function (#1100)

* feat: current block

* pass full rundown to functions and use filter utils
This commit is contained in:
Alex Christoffer Rasmussen
2024-07-23 12:22:47 +02:00
committed by GitHub
parent 20838c038a
commit 158ef05ff0
18 changed files with 246 additions and 49 deletions
+4
View File
@@ -179,6 +179,10 @@ export const startServer = async (
message: messageService.getState(),
runtime: state.runtime,
eventNow: state.eventNow,
currentBlock: {
block: null,
startedAt: null,
},
publicEventNow: state.publicEventNow,
eventNext: state.eventNext,
publicEventNext: state.publicEventNext,
+2 -2
View File
@@ -1,4 +1,4 @@
import { OntimeEvent } from 'ontime-types';
import { OntimeRundown } from 'ontime-types';
import * as runtimeState from '../stores/runtimeState.js';
import type { UpdateResult } from '../stores/runtimeState.js';
@@ -106,7 +106,7 @@ export class TimerService {
* Loads roll information into timer service
* @param {OntimeEvent[]} rundown -- list of events to run
*/
roll(rundown: OntimeEvent[]) {
roll(rundown: OntimeRundown) {
runtimeState.roll(rundown);
}
@@ -25,13 +25,14 @@ import { eventStore } from '../../stores/EventStore.js';
type PatchWithId = (Partial<OntimeEvent> | Partial<OntimeBlock> | Partial<OntimeDelay>) & { id: string };
type CompleteEntry<T> = T extends Partial<OntimeEvent>
? OntimeEvent
: T extends Partial<OntimeDelay>
? OntimeDelay
: T extends Partial<OntimeBlock>
? OntimeBlock
: never;
type CompleteEntry<T> =
T extends Partial<OntimeEvent>
? OntimeEvent
: T extends Partial<OntimeDelay>
? OntimeDelay
: T extends Partial<OntimeBlock>
? OntimeBlock
: never;
function generateEvent<T extends Partial<OntimeEvent> | Partial<OntimeDelay> | Partial<OntimeBlock>>(
eventData: T,
@@ -240,7 +241,7 @@ function notifyChanges(options: { timer?: boolean | string[]; external?: boolean
// notify timer service of changed events
// timer can be true or an array of changed IDs
const affected = Array.isArray(options.timer) ? options.timer : undefined;
runtimeService.maybeUpdate(playableEvents, affected);
runtimeService.maybeUpdate(affected);
}
}
@@ -9,7 +9,7 @@ import {
TimerLifeCycle,
TimerPhase,
} from 'ontime-types';
import { millisToString, validatePlayback } from 'ontime-utils';
import { filterPlayable, millisToString, validatePlayback } from 'ontime-utils';
import { deepEqual } from 'fast-equals';
@@ -28,6 +28,7 @@ import {
getNextEventWithCue,
getEventWithId,
getPlayableEvents,
getRundown,
} from '../rundown-service/rundownUtils.js';
import { skippedOutOfEvent } from '../timerUtils.js';
import { integrationService } from '../integration-service/IntegrationService.js';
@@ -95,7 +96,7 @@ class RuntimeService {
}
// we dont call this.roll because we need to bypass the checks
const rundown = getPlayableEvents();
const rundown = getRundown();
// TODO: by not calling roll, we dont get the events
this.eventTimer.roll(rundown);
}
@@ -217,7 +218,7 @@ class RuntimeService {
* Called when the underlying data has changed,
* we check if the change affects the runtime
*/
maybeUpdate(playableEvents: OntimeEvent[], affectedIds?: string[]) {
maybeUpdate(affectedIds?: string[]) {
const state = runtimeState.getState();
const hasLoadedElements = state.eventNow !== null || state.eventNext !== null;
if (!hasLoadedElements) {
@@ -245,7 +246,8 @@ class RuntimeService {
if (onlyChangedNow) {
runtimeState.reload(eventNow);
} else {
runtimeState.reloadAll(eventNow, playableEvents);
const rundown = getRundown();
runtimeState.reloadAll(eventNow, rundown);
}
return;
}
@@ -253,7 +255,8 @@ class RuntimeService {
// Maybe the event will become the next
isNext = this.isNewNext();
if (isNext) {
runtimeState.loadNext(playableEvents);
const rundown = getRundown();
runtimeState.loadNext(rundown);
}
}
@@ -269,8 +272,8 @@ class RuntimeService {
return false;
}
const timedEvents = getPlayableEvents();
const success = runtimeState.load(event, timedEvents);
const rundown = getRundown();
const success = runtimeState.load(event, rundown);
if (success) {
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
@@ -513,13 +516,14 @@ class RuntimeService {
return;
}
const playableEvents = getPlayableEvents();
const rundown = getRundown();
const playableEvents = filterPlayable(rundown);
if (playableEvents.length === 0) {
logger.warning(LogOrigin.Server, 'Roll: no events found');
return;
}
this.eventTimer.roll(playableEvents);
this.eventTimer.roll(rundown);
const state = runtimeState.getState();
const newState = state.timer.playback;
@@ -543,14 +547,14 @@ class RuntimeService {
}
// the db would have to change for the event not to exist
// we do not kow the reason for the crash, so we check anyway
// we do not know the reason for the crash, so we check anyway
const event = getEventWithId(selectedEventId);
if (!event || !isOntimeEvent(event)) {
return;
}
const timedEvents = getPlayableEvents();
runtimeState.resume(restorePoint, event, timedEvents);
const rundown = getRundown();
runtimeState.resume(restorePoint, event, rundown);
logger.info(LogOrigin.Playback, 'Resuming playback');
}
@@ -620,6 +624,11 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
updateEventIfChanged('eventNext', state);
updateEventIfChanged('publicEventNext', state);
if (!deepEqual(RuntimeService?.previousState.currentBlock, state.currentBlock)) {
eventStore.set('currentBlock', state.currentBlock);
RuntimeService.previousState.currentBlock = { ...state.currentBlock };
}
if (shouldUpdateClock) {
RuntimeService.previousClockUpdate = state.clock;
eventStore.set('clock', state.clock);
+8 -4
View File
@@ -127,10 +127,14 @@ type RollTimers = {
/**
* Finds loading information given a current rundown and time
* @param {OntimeEvent[]} rundown - List of playable events
* @param {OntimeEvent[]} playableEvents - List of playable events
* @param {number} timeNow - time now in ms
*/
export const getRollTimers = (rundown: OntimeEvent[], timeNow: number, currentIndex?: number | null): RollTimers => {
export const getRollTimers = (
playableEvents: OntimeEvent[],
timeNow: number,
currentIndex?: number | null,
): RollTimers => {
let nowIndex: MaybeNumber = null; // index of event now
let nowId: MaybeString = null; // id of event now
let publicIndex: MaybeNumber = null; // index of public event now
@@ -140,8 +144,8 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number, currentIn
let publicTimeToNext: MaybeNumber = null; // counter: time for next public event
const hasLoaded = currentIndex !== null;
const canFilter = hasLoaded && currentIndex === rundown.length - 1;
const filteredRundown = canFilter ? rundown.slice(currentIndex) : rundown;
const canFilter = hasLoaded && currentIndex === playableEvents.length - 1;
const filteredRundown = canFilter ? playableEvents.slice(currentIndex) : playableEvents;
const lastEvent = filteredRundown.at(-1);
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
@@ -98,6 +98,7 @@ describe('mutation on runtimeState', () => {
expect(newState.eventNow?.id).toBe(mockEvent.id);
expect(newState.timer.playback).toBe(Playback.Armed);
expect(newState.clock).not.toBe(666);
expect(newState.currentBlock.block).toBeNull();
// 2. Start event
let success = start();
@@ -171,6 +172,7 @@ describe('mutation on runtimeState', () => {
expect(newState.runtime.actualStart).toBeNull();
expect(newState.runtime.plannedStart).toBe(0);
expect(newState.runtime.plannedEnd).toBe(1500);
expect(newState.currentBlock.block).toBeNull();
// 2. Start event
start();
@@ -202,6 +204,7 @@ describe('mutation on runtimeState', () => {
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.currentBlock.block).toBeNull();
// 4. Add time
addTime(10);
+66 -15
View File
@@ -1,5 +1,14 @@
import { MaybeNumber, OntimeEvent, Playback, Runtime, TimerPhase, TimerState } from 'ontime-types';
import { calculateDuration, dayInMs } from 'ontime-utils';
import {
CurrentBlockState,
MaybeNumber,
OntimeEvent,
OntimeRundown,
Playback,
Runtime,
TimerPhase,
TimerState,
} from 'ontime-types';
import { calculateDuration, dayInMs, filterPlayable, getRelevantBlock } from 'ontime-utils';
import { clock } from '../services/Clock.js';
import { RestorePoint } from '../services/RestoreService.js';
@@ -41,6 +50,7 @@ const initialTimer: TimerState = {
export type RuntimeState = {
clock: number; // realtime clock
eventNow: OntimeEvent | null;
currentBlock: CurrentBlockState;
publicEventNow: OntimeEvent | null;
eventNext: OntimeEvent | null;
publicEventNext: OntimeEvent | null;
@@ -52,10 +62,15 @@ export type RuntimeState = {
totalDelay: number; // this value comes from rundown service
pausedAt: MaybeNumber;
};
_prevCurrentBlock: CurrentBlockState;
};
const runtimeState: RuntimeState = {
clock: clock.timeNow(),
currentBlock: {
block: null,
startedAt: null,
},
eventNow: null,
publicEventNow: null,
eventNext: null,
@@ -67,6 +82,10 @@ const runtimeState: RuntimeState = {
totalDelay: 0,
pausedAt: null,
},
_prevCurrentBlock: {
block: null,
startedAt: null,
},
};
export function getState(): Readonly<RuntimeState> {
@@ -77,6 +96,11 @@ export function clear() {
runtimeState.eventNow = null;
runtimeState.publicEventNow = null;
runtimeState.eventNext = null;
runtimeState._prevCurrentBlock = { ...runtimeState.currentBlock };
runtimeState.currentBlock.block = null;
runtimeState.currentBlock.startedAt = null;
runtimeState.publicEventNext = null;
runtimeState.runtime.offset = 0;
@@ -129,12 +153,13 @@ export function updateRundownData(rundownData: RundownData) {
/**
* Loads a given event into state
* @param event
* @param rundown
* @param initialData
* @param {OntimeEvent[]} playableEvents list of events availebe for playback
* @param {OntimeRundown} rundown the full rundown
* @param initialData potential data from restore point
*/
export function load(
event: OntimeEvent,
rundown: OntimeEvent[],
rundown: OntimeRundown,
initialData?: Partial<TimerState & RestorePoint>,
): boolean {
clear();
@@ -165,8 +190,14 @@ export function load(
return event.id === runtimeState.eventNow?.id;
}
export function loadNow(event: OntimeEvent, playableEvents: OntimeEvent[]) {
export function loadNow(event: OntimeEvent, rundown: OntimeRundown) {
runtimeState.eventNow = event;
runtimeState.currentBlock.block = getRelevantBlock(rundown, event.id);
//if we are still in the same block keep the startedAt time
if (runtimeState._prevCurrentBlock.block?.id === runtimeState.currentBlock.block?.id) {
runtimeState.currentBlock.startedAt = runtimeState._prevCurrentBlock.startedAt;
}
// check if current is also public
if (event.isPublic) {
@@ -180,6 +211,8 @@ export function loadNow(event: OntimeEvent, playableEvents: OntimeEvent[]) {
return;
}
const playableEvents = filterPlayable(rundown);
// iterate backwards to find it
for (let i = runtimeState.runtime.selectedEventIndex; i >= 0; i--) {
if (playableEvents[i].isPublic) {
@@ -190,7 +223,7 @@ export function loadNow(event: OntimeEvent, playableEvents: OntimeEvent[]) {
}
}
export function loadNext(playableEvents: OntimeEvent[]) {
export function loadNext(rundown: OntimeRundown) {
// assume there are no next events
runtimeState.eventNext = null;
runtimeState.publicEventNext = null;
@@ -199,6 +232,7 @@ export function loadNext(playableEvents: OntimeEvent[]) {
return;
}
const playableEvents = filterPlayable(rundown);
const numEvents = playableEvents.length;
if (runtimeState.runtime.selectedEventIndex < numEvents - 1) {
@@ -224,7 +258,14 @@ export function loadNext(playableEvents: OntimeEvent[]) {
}
}
export function resume(restorePoint: RestorePoint, event: OntimeEvent, rundown: OntimeEvent[]) {
/**
* Resume from restore point
* @param restorePoint
* @param event
* @param playableEvents list of events availebe for playback
* @param rundown the full rundown
*/
export function resume(restorePoint: RestorePoint, event: OntimeEvent, rundown: OntimeRundown) {
load(event, rundown, restorePoint);
}
@@ -255,6 +296,8 @@ export function reload(event?: OntimeEvent) {
runtimeState.timer.addedTime = 0;
runtimeState.timer.expectedFinish = getExpectedFinish(runtimeState);
runtimeState.currentBlock.startedAt = null;
return runtimeState.eventNow.id;
}
@@ -263,10 +306,11 @@ export function reload(event?: OntimeEvent) {
* without interrupting timer
* @param eventNow
* @param playableEvents
* @param rundown
*/
export function reloadAll(eventNow: OntimeEvent, playableEvents: OntimeEvent[]) {
loadNow(eventNow, playableEvents);
loadNext(playableEvents);
export function reloadAll(eventNow: OntimeEvent, rundown: OntimeRundown) {
loadNow(eventNow, rundown);
loadNext(rundown);
reload(eventNow);
}
@@ -291,6 +335,11 @@ export function start(state: RuntimeState = runtimeState): boolean {
state.timer.startedAt = state.clock;
}
if (state.currentBlock.startedAt === null) {
console.log('currentBlock.startedAt is null, setting new start');
state.currentBlock.startedAt = state.clock;
}
state.timer.playback = Playback.Play;
state.timer.expectedFinish = getExpectedFinish(state);
state.timer.elapsed = 0;
@@ -427,12 +476,14 @@ export function update(): UpdateResult {
}
}
export function roll(rundown: OntimeEvent[]) {
export function roll(rundown: OntimeRundown) {
const selectedEventIndex = runtimeState.runtime.selectedEventIndex;
clear();
runtimeState.runtime.numEvents = rundown.length;
const playableEvents = filterPlayable(rundown);
const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock, selectedEventIndex);
clear();
runtimeState.runtime.numEvents = playableEvents.length;
const { nextEvent, currentEvent } = getRollTimers(playableEvents, runtimeState.clock, selectedEventIndex);
if (currentEvent) {
// there is something running, load