refactor: maintain flat orders

This commit is contained in:
Carlos Valente
2025-04-17 10:12:06 +02:00
parent ed7f8a0aa2
commit a55dfba4ef
13 changed files with 71 additions and 25 deletions
@@ -71,7 +71,7 @@ export async function addEvent(eventData: EventPostPayload): Promise<OntimeEntry
}
// 2. if the user provides a parent (inside a group), we make sure it exists
let parent: EntryId | undefined;
let parent: EntryId | null = null;
if ('parent' in eventData && eventData.parent != null) {
if (!cache.hasId(eventData.parent)) {
throw new Error(`Parent event with ID ${eventData.parent} not found`);
@@ -49,6 +49,19 @@ describe('generate()', () => {
);
});
it('generates metadata from given rundown', () => {
const initResult = generate(demoDb.rundowns.default, demoDb.customFields);
expect(initResult.order.length).toBe(12);
expect(initResult.flatEntryOrder.length).toBe(17);
expect(initResult.timedEventOrder.length).toBe(14);
expect(initResult.playableEventOrder.length).toBe(14);
expect(initResult.firstStart).toBe(36000000);
expect(initResult.lastEnd).toBe(61800000);
expect(initResult.totalDays).toBe(0);
expect(initResult.totalDelay).toBe(0);
});
it('creates normalised versions of a given rundown', () => {
const rundown = makeRundown({
order: ['1', '2', '3'],
@@ -613,7 +626,7 @@ describe('add() mutation', () => {
test('adds an event to the rundown', () => {
const mockEvent = makeOntimeEvent({ id: 'mock', cue: 'mock' });
const rundown = makeRundown({});
const { newRundown } = add({ atIndex: 0, event: mockEvent, rundown });
const { newRundown } = add({ atIndex: 0, entry: mockEvent, parent: null, rundown });
expect(newRundown.order.length).toBe(1);
expect(newRundown.entries['mock']).toMatchObject(mockEvent);
});
@@ -7,9 +7,9 @@ export type RundownMetadata = {
firstStart: MaybeNumber;
lastEnd: MaybeNumber;
playableEventOrder: EntryId[];
timedEventOrder: EntryId[];
flatEventOrder: EntryId[];
playableEventOrder: EntryId[]; // flat order of playable events
timedEventOrder: EntryId[]; // flat order of timed events
flatEntryOrder: EntryId[]; // flat order of entries
/**
* Keep track of which custom fields are used.
@@ -40,7 +40,7 @@ let rundownMetadata: RundownMetadata = {
playableEventOrder: [],
timedEventOrder: [],
flatEventOrder: [],
flatEntryOrder: [],
assignedCustomFields: {},
};
@@ -172,7 +172,7 @@ export function updateCache() {
const { previousEvent, latestEvent, ...metadata } = processedData;
currentRundown.entries = metadata.entries;
currentRundown.order = metadata.order;
currentRundown.flatOrder = metadata.flatEventOrder;
currentRundown.flatOrder = metadata.flatEntryOrder;
rundownMetadata = metadata;
clearIsStale();
customFieldChangelog = {};
@@ -320,7 +320,7 @@ export function mutateCache<T extends object>(mutation: MutatingFn<T>) {
return scopedMutation;
}
type AddArgs = MutationParams<{ atIndex: number; parent?: EntryId; entry: OntimeEntry }>;
type AddArgs = MutationParams<{ atIndex: number; parent: EntryId | null; entry: OntimeEntry }>;
/**
* Add entry to rundown
*/
@@ -158,7 +158,7 @@ export function makeRundownMetadata(customFields: CustomFields, customFieldChang
assignedCustomFields: {},
playableEventOrder: [],
timedEventOrder: [],
flatEventOrder: [],
flatEntryOrder: [],
entries: {},
order: [],
@@ -192,12 +192,10 @@ function processEntry<T extends OntimeEntry>(
): { processedData: ProcessedRundownMetadata; processedEntry: T } {
const processedData = { ...rundownMetadata };
const currentEntry = structuredClone(entry);
processedData.flatEventOrder.push(currentEntry.id);
processedData.flatEntryOrder.push(currentEntry.id);
if (isOntimeEvent(currentEntry)) {
if (!childOfBlock) {
processedData.timedEventOrder.push(currentEntry.id);
}
processedData.timedEventOrder.push(currentEntry.id);
/**
* 1.Checks that link can be established (ie, events exist and are valid)
@@ -227,9 +225,7 @@ function processEntry<T extends OntimeEntry>(
// update rundown metadata, it only concerns playable events
if (isPlayableEvent(currentEntry)) {
if (!childOfBlock) {
processedData.playableEventOrder.push(currentEntry.id);
}
processedData.playableEventOrder.push(currentEntry.id);
// first start is always the first event
if (processedData.firstStart === null) {
@@ -122,23 +122,23 @@ export function findPrevious(currentEventId?: string): OntimeEvent | undefined {
* finds the next event
*/
export function findNext(currentEventId?: string): PlayableEvent | undefined {
const { playableEventsOrder } = cache.getEventOrder();
const { playableEventOrder } = cache.getMetadata();
if (!playableEventsOrder.length) {
if (!playableEventOrder.length) {
return;
}
// if there is no event running, go to first
if (!currentEventId) {
return getFirstPlayable(playableEventsOrder);
return getFirstPlayable(playableEventOrder);
}
const currentIndex = playableEventsOrder.findIndex((eventId) => eventId === currentEventId);
const newIndex = Math.min(currentIndex + 1, playableEventsOrder.length - 1);
const nextEventId = playableEventsOrder.at(newIndex);
const currentIndex = playableEventOrder.findIndex((eventId) => eventId === currentEventId);
const newIndex = Math.min(currentIndex + 1, playableEventOrder.length - 1);
const nextEventId = playableEventOrder.at(newIndex);
if (!nextEventId) {
return getFirstPlayable(playableEventsOrder);
return getFirstPlayable(playableEventOrder);
}
return getEntryWithId(nextEventId) as PlayableEvent | undefined;
@@ -176,7 +176,7 @@ export function getRundownOrThrow(rundowns: ProjectRundowns, rundownId: string):
}
export function getInsertionPosition(
parentId?: EntryId,
parentId: EntryId | null,
afterId?: EntryId,
beforeId?: EntryId,
): { atIndex: number; afterId: EntryId | undefined } {
@@ -202,7 +202,7 @@ export function getInsertionPosition(
afterId: undefined,
};
function selectOrderList(parentId?: EntryId) {
function selectOrderList(parentId: EntryId | null) {
if (parentId) {
return (getEntryWithId(parentId) as OntimeBlock).events;
}