refactor: rundown must be in order

This commit is contained in:
Carlos Valente
2024-04-16 22:22:13 +02:00
committed by Carlos Valente
parent bef69e7d0f
commit 8f249b9d51
4 changed files with 7 additions and 57 deletions
+5 -6
View File
@@ -1,5 +1,5 @@
import { MaybeNumber, MaybeString, OntimeEvent, TimerType } from 'ontime-types';
import { dayInMs, sortArrayByProperty } from 'ontime-utils';
import { dayInMs } from 'ontime-utils';
import { RuntimeState } from '../stores/runtimeState.js';
import { timerConfig } from '../config/config.js';
@@ -129,8 +129,7 @@ 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 orderedEvents = sortArrayByProperty(rundown, 'timeStart');
const lastEvent = orderedEvents[orderedEvents.length - 1];
const lastEvent = rundown[rundown.length - 1];
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
let nextEvent: OntimeEvent | null = null;
@@ -142,7 +141,7 @@ export const getRollTimers = (rundown: OntimeEvent[], timeNow: number): RollTime
// we are past last end
// preload first and find next
const firstEvent = orderedEvents[0];
const firstEvent = rundown[0];
nextIndex = 0;
nextEvent = firstEvent;
timeToNext = firstEvent.timeStart + dayInMs - timeNow;
@@ -154,7 +153,7 @@ 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 orderedEvents) {
for (const event of rundown) {
if (event.isPublic) {
nextPublicEvent = event;
// we need the index before this was sorted
@@ -169,7 +168,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 orderedEvents) {
for (const event of rundown) {
// When does the event end (handle midnight)
const normalEnd = normaliseEndTime(event.timeStart, event.timeEnd);
+1 -1
View File
@@ -52,7 +52,7 @@ export { dayInMs, mts } from './src/timeConstants.js';
export { deepmerge } from './src/externals/deepmerge.js';
// array utils
export { deleteAtIndex, insertAtIndex, reorderArray, sortArrayByProperty } from './src/array-utils/arrayUtils.js';
export { deleteAtIndex, insertAtIndex, reorderArray } from './src/array-utils/arrayUtils.js';
// generic utilities
export { getErrorMessage } from './src/generic/generic.js';
@@ -1,4 +1,4 @@
import { insertAtIndex, reorderArray, sortArrayByProperty } from './arrayUtils.js';
import { insertAtIndex, reorderArray } from './arrayUtils.js';
describe('insertAtIndex', () => {
it('should insert an item at the beginning of the array', () => {
@@ -52,37 +52,3 @@ describe('reorderArray', () => {
expect(result).toEqual(['b', 'c', 'a']);
});
});
describe('sortArrayByProperty()', () => {
it('sort array 1-5', () => {
const arr1 = [{ timeStart: 1 }, { timeStart: 5 }, { timeStart: 3 }, { timeStart: 2 }, { timeStart: 4 }];
const arr1Expected = [{ timeStart: 1 }, { timeStart: 2 }, { timeStart: 3 }, { timeStart: 4 }, { timeStart: 5 }];
const sorted = sortArrayByProperty(arr1, 'timeStart');
expect(sorted).toStrictEqual(arr1Expected);
});
it('sort array 1-5 with null', () => {
const arr1 = [
{ timeStart: 1 },
{ timeStart: 5 },
{ timeStart: 3 },
{ timeStart: 2 },
{ timeStart: 4 },
{ timeStart: null },
];
const arr1Expected = [
{ timeStart: null },
{ timeStart: 1 },
{ timeStart: 2 },
{ timeStart: 3 },
{ timeStart: 4 },
{ timeStart: 5 },
];
const sorted = sortArrayByProperty(arr1, 'timeStart');
expect(sorted).toStrictEqual(arr1Expected);
});
});
@@ -48,18 +48,3 @@ export function reorderArray<T>(array: T[], fromIndex: number, toIndex: number)
modifiedArray.splice(toIndex, 0, reorderedItem);
return modifiedArray;
}
/**
* @description Sorts an array of objects by given property
* @param {array} arr - array to be sorted
* @param {string} property - property to compare
* @returns {array} copy of array sorted in ascending order
*/
export const sortArrayByProperty = <T>(arr: T[], property: string): T[] => {
return [...arr].sort((a, b) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore -- its ok
return a[property] - b[property];
});
};