refactor: continue roll

This commit is contained in:
Carlos Valente
2024-05-29 13:25:33 +02:00
committed by Carlos Valente
parent ec55236eb2
commit a53d354b16
2 changed files with 18 additions and 14 deletions
+15 -12
View File
@@ -118,9 +118,8 @@ type RollTimers = {
* Finds loading information given a current rundown and time * Finds loading information given a current rundown and time
* @param {OntimeEvent[]} rundown - List of playable events * @param {OntimeEvent[]} rundown - List of playable events
* @param {number} timeNow - time now in ms * @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 nowIndex: number | null = null; // index of event now
let nowId: string | null = null; // id of event now let nowId: string | null = null; // id of event now
let publicIndex: number | null = null; // index of public 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 timeToNext: number | null = null; // counter: time for next event
let publicTimeToNext: number | null = null; // counter: time for next public 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); const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
let nextEvent: OntimeEvent | null = null; let nextEvent: OntimeEvent | null = null;
@@ -141,7 +144,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
// we are past last end // we are past last end
// preload first and find next // preload first and find next
const firstEvent = rundown[0]; const firstEvent = filteredRundown.at(0);
nextIndex = 0; nextIndex = 0;
nextEvent = firstEvent; nextEvent = firstEvent;
timeToNext = firstEvent.timeStart + dayInMs - timeNow; timeToNext = firstEvent.timeStart + dayInMs - timeNow;
@@ -153,11 +156,11 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
// look for next public // look for next public
// dev note: we feel that this is more efficient than filtering // dev note: we feel that this is more efficient than filtering
// since the next event will likely be close to the one playing // 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) { if (event.isPublic) {
nextPublicEvent = event; nextPublicEvent = event;
// we need the index before this was sorted // 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; break;
} }
} }
@@ -168,7 +171,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
// keep track of the end times when looking for public // keep track of the end times when looking for public
let publicTime = -1; let publicTime = -1;
for (const event of rundown) { for (const event of filteredRundown) {
// When does the event end (handle midnight) // When does the event end (handle midnight)
const normalEnd = normaliseEndTime(event.timeStart, event.timeEnd); 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 // public event might not be the one running
publicTime = normalEnd; publicTime = normalEnd;
currentPublicEvent = event; currentPublicEvent = event;
publicIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); publicIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id);
} }
} else if (hasNotEnded && hasStarted && !nowFound) { } else if (hasNotEnded && hasStarted && !nowFound) {
// event is running // event is running
currentEvent = event; currentEvent = event;
nowIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); nowIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id);
nowId = event.id; nowId = event.id;
nowFound = true; nowFound = true;
@@ -196,7 +199,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
if (event.isPublic) { if (event.isPublic) {
publicTime = normalEnd; publicTime = normalEnd;
currentPublicEvent = event; currentPublicEvent = event;
publicIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); publicIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id);
} }
} else if (normalEnd > timeNow) { } else if (normalEnd > timeNow) {
// event will run // event will run
@@ -214,7 +217,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
if (nextIndex === null || timeToEventStart < timeToNext) { if (nextIndex === null || timeToEventStart < timeToNext) {
timeToNext = timeToEventStart; timeToNext = timeToEventStart;
nextEvent = event; nextEvent = event;
nextIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); nextIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id);
} }
if (event.isPublic) { if (event.isPublic) {
@@ -222,7 +225,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
if (publicNextIndex === null || timeToEventStart < publicTimeToNext) { if (publicNextIndex === null || timeToEventStart < publicTimeToNext) {
publicTimeToNext = timeToEventStart; publicTimeToNext = timeToEventStart;
nextPublicEvent = event; nextPublicEvent = event;
publicNextIndex = rundown.findIndex((rundownEvent) => rundownEvent.id === event.id); publicNextIndex = filteredRundown.findIndex((rundownEvent) => rundownEvent.id === event.id);
} }
} }
} }
+3 -2
View File
@@ -434,10 +434,11 @@ export function update(): UpdateResult {
} }
export function roll(rundown: OntimeEvent[]) { export function roll(rundown: OntimeEvent[]) {
const selectedEventIndex = runtimeState.runtime.selectedEventIndex;
clear(); clear();
runtimeState.runtime.numEvents = rundown.length; runtimeState.runtime.numEvents = rundown.length;
const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock);
const { nextEvent, currentEvent } = getRollTimers(rundown, runtimeState.clock, selectedEventIndex);
if (currentEvent) { if (currentEvent) {
// there is something running, load // there is something running, load