feat: granular endpoints for rundown

This commit is contained in:
Carlos Valente
2024-07-10 13:23:37 +02:00
committed by Carlos Valente
parent e6c3322321
commit 917b182e39
8 changed files with 143 additions and 18 deletions
@@ -0,0 +1,39 @@
import { OntimeRundown } from 'ontime-types';
import { getPaginated } from '../rundownUtils.js';
describe('getPaginated', () => {
// mock cache so we dont run data functions
beforeAll(() => {
vi.mock('../rundownCache.js', () => ({}));
});
// @ts-expect-error -- we know this is not correct, but good enough for the test
const getData = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as OntimeRundown;
it('should return the correct paginated rundown', () => {
const offset = 0;
const limit = 1;
const result = getPaginated(offset, limit, getData);
expect(result.rundown).toHaveLength(1);
expect(result.total).toBe(10);
});
it('should handle overflows', () => {
const offset = 0;
const limit = 20;
const result = getPaginated(offset, limit, getData);
expect(result.rundown).toHaveLength(10);
expect(result.total).toBe(10);
});
it('should handle out of range', () => {
const offset = 11;
const limit = Infinity;
const result = getPaginated(offset, limit, getData);
expect(result.rundown).toHaveLength(0);
expect(result.total).toBe(10);
});
});
@@ -1,4 +1,4 @@
import { OntimeEvent, OntimeRundown, isOntimeEvent, RundownCached } from 'ontime-types';
import { OntimeEvent, OntimeRundown, isOntimeEvent, RundownCached, OntimeRundownEntry } from 'ontime-types';
import * as cache from './rundownCache.js';
@@ -53,9 +53,9 @@ export function getEventAtIndex(eventIndex: number): OntimeEvent | undefined {
* @param {string} eventId
* @return {object | undefined}
*/
export function getEventWithId(eventId: string): OntimeEvent | undefined {
const timedEvents = getTimedEvents();
return timedEvents.find((event) => event.id === eventId);
export function getEventWithId(eventId: string): OntimeRundownEntry | undefined {
const rundown = getRundown();
return rundown.find((event) => event.id === eventId);
}
/**
@@ -117,3 +117,19 @@ export function findNext(currentEventId?: string): OntimeEvent | null {
const nextEvent = timedEvents.at(newIndex);
return nextEvent ?? null;
}
/**
* Returns a paginated rundown
* Exposes a getter function for the rundown for testing
*/
export function getPaginated(
offset: number,
limit: number,
source = getRundown,
): { rundown: OntimeRundownEntry[]; total: number } {
const rundown = source();
return {
rundown: rundown.slice(Math.min(offset, rundown.length), Math.min(offset + limit, rundown.length)),
total: rundown.length,
};
}
@@ -1,5 +1,6 @@
import {
EndAction,
isOntimeEvent,
LogOrigin,
MaybeNumber,
OntimeEvent,
@@ -225,6 +226,9 @@ class RuntimeService {
}
// load stuff again, but keep running if our events still exist
const eventNow = getEventWithId(state.eventNow.id);
if (!isOntimeEvent(eventNow)) {
return;
}
const onlyChangedNow = affectedIds?.length === 1 && affectedIds.at(0) === eventNow.id;
if (onlyChangedNow) {
runtimeState.reload(eventNow);
@@ -272,7 +276,7 @@ class RuntimeService {
*/
startById(eventId: string): boolean {
const event = getEventWithId(eventId);
if (!event) {
if (!event || !isOntimeEvent(event)) {
return false;
}
const loaded = this.loadEvent(event);
@@ -323,7 +327,7 @@ class RuntimeService {
*/
loadById(eventId: string): boolean {
const event = getEventWithId(eventId);
if (!event) {
if (!event || !isOntimeEvent(event)) {
return false;
}
return this.loadEvent(event);
@@ -529,7 +533,7 @@ 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
const event = getEventWithId(selectedEventId);
if (!event) {
if (!event || !isOntimeEvent(event)) {
return;
}