From a53d354b16e37cefb03210ba0681a48a268b8da2 Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Wed, 29 May 2024 13:25:33 +0200 Subject: [PATCH] refactor: continue roll --- apps/server/src/services/timerUtils.ts | 27 ++++++++++++++------------ apps/server/src/stores/runtimeState.ts | 5 +++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/apps/server/src/services/timerUtils.ts b/apps/server/src/services/timerUtils.ts index 57ce874cb..4c78b3d55 100644 --- a/apps/server/src/services/timerUtils.ts +++ b/apps/server/src/services/timerUtils.ts @@ -118,9 +118,8 @@ type RollTimers = { * Finds loading information given a current rundown and time * @param {OntimeEvent[]} rundown - List of playable events * @param {number} timeNow - time now in ms - * @returns {{}} */ -export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTimers => { +export const getRollTimers = (rundown: OntimeEvent[], timeNow: number, currentIndex?: number | null): RollTimers => { let nowIndex: number | null = null; // index of event now let nowId: string | null = null; // id of event now let publicIndex: number | null = null; // index of public event now @@ -129,7 +128,11 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime let timeToNext: number | null = null; // counter: time for next event let publicTimeToNext: number | null = null; // counter: time for next public event - const lastEvent = rundown[rundown.length - 1]; + const hasLoaded = currentIndex !== null; + const canFilter = hasLoaded && currentIndex === rundown.length - 1; + const filteredRundown = canFilter ? rundown.slice(currentIndex) : rundown; + + const lastEvent = filteredRundown.at(-1); const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd); let nextEvent: OntimeEvent | null = null; @@ -141,7 +144,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime // we are past last end // preload first and find next - const firstEvent = rundown[0]; + const firstEvent = filteredRundown.at(0); nextIndex = 0; nextEvent = firstEvent; timeToNext = firstEvent.timeStart + dayInMs - timeNow; @@ -153,11 +156,11 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime // look for next public // dev note: we feel that this is more efficient than filtering // since the next event will likely be close to the one playing - for (const event of rundown) { + for (const event of filteredRundown) { if (event.isPublic) { nextPublicEvent = event; // we need the index before this was sorted - publicNextIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); + publicNextIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id); break; } } @@ -168,7 +171,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime // keep track of the end times when looking for public let publicTime = -1; - for (const event of rundown) { + for (const event of filteredRundown) { // When does the event end (handle midnight) const normalEnd = normaliseEndTime(event.timeStart, event.timeEnd); @@ -183,12 +186,12 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime // public event might not be the one running publicTime = normalEnd; currentPublicEvent = event; - publicIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); + publicIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id); } } else if (hasNotEnded && hasStarted && !nowFound) { // event is running currentEvent = event; - nowIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); + nowIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id); nowId = event.id; nowFound = true; @@ -196,7 +199,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime if (event.isPublic) { publicTime = normalEnd; currentPublicEvent = event; - publicIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); + publicIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id); } } else if (normalEnd > timeNow) { // event will run @@ -214,7 +217,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime if (nextIndex === null || timeToEventStart < timeToNext) { timeToNext = timeToEventStart; nextEvent = event; - nextIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); + nextIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id); } if (event.isPublic) { @@ -222,7 +225,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime if (publicNextIndex === null || timeToEventStart < publicTimeToNext) { publicTimeToNext = timeToEventStart; nextPublicEvent = event; - publicNextIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); + publicNextIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id); } } } diff --git a/apps/server/src/stores/runtimeState.ts b/apps/server/src/stores/runtimeState.ts index aadf93ad1..31cf80047 100644 --- a/apps/server/src/stores/runtimeState.ts +++ b/apps/server/src/stores/runtimeState.ts @@ -434,10 +434,11 @@ export function update(): UpdateResult { } export function roll(rundown: OntimeEvent[]) { + const selectedEventIndex = runtimeState.runtime.selectedEventIndex; clear(); - runtimeState.runtime.numEvents = rundown.length; - const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock); + + const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock, selectedEventIndex); if (currentEvent) { // there is something running, load