mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 14:39:06 +00:00
refactor: rundown must be in order
This commit is contained in:
committed by
Carlos Valente
parent
bef69e7d0f
commit
8f249b9d51
@@ -1,5 +1,5 @@
|
|||||||
import { MaybeNumber, MaybeString, OntimeEvent, TimerType } from 'ontime-types';
|
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 { RuntimeState } from '../stores/runtimeState.js';
|
||||||
import { timerConfig } from '../config/config.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 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 orderedEvents = sortArrayByProperty(rundown, 'timeStart');
|
const lastEvent = rundown[rundown.length - 1];
|
||||||
const lastEvent = orderedEvents[orderedEvents.length - 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;
|
||||||
@@ -142,7 +141,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 = orderedEvents[0];
|
const firstEvent = rundown[0];
|
||||||
nextIndex = 0;
|
nextIndex = 0;
|
||||||
nextEvent = firstEvent;
|
nextEvent = firstEvent;
|
||||||
timeToNext = firstEvent.timeStart + dayInMs - timeNow;
|
timeToNext = firstEvent.timeStart + dayInMs - timeNow;
|
||||||
@@ -154,7 +153,7 @@ 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 orderedEvents) {
|
for (const event of rundown) {
|
||||||
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
|
||||||
@@ -169,7 +168,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 orderedEvents) {
|
for (const event of rundown) {
|
||||||
// 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);
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export { dayInMs, mts } from './src/timeConstants.js';
|
|||||||
export { deepmerge } from './src/externals/deepmerge.js';
|
export { deepmerge } from './src/externals/deepmerge.js';
|
||||||
|
|
||||||
// array utils
|
// 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
|
// generic utilities
|
||||||
export { getErrorMessage } from './src/generic/generic.js';
|
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', () => {
|
describe('insertAtIndex', () => {
|
||||||
it('should insert an item at the beginning of the array', () => {
|
it('should insert an item at the beginning of the array', () => {
|
||||||
@@ -52,37 +52,3 @@ describe('reorderArray', () => {
|
|||||||
expect(result).toEqual(['b', 'c', 'a']);
|
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);
|
modifiedArray.splice(toIndex, 0, reorderedItem);
|
||||||
return modifiedArray;
|
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];
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user