add relevantBlock function (#1100)

* feat: current block

* pass full rundown to functions and use filter utils
This commit is contained in:
Alex Christoffer Rasmussen
2024-07-23 12:22:47 +02:00
committed by GitHub
parent 20838c038a
commit 158ef05ff0
18 changed files with 246 additions and 49 deletions
@@ -2,12 +2,15 @@ import type { NormalisedRundown, OntimeEvent, OntimeRundown } from 'ontime-types
import { SupportedEvent } from 'ontime-types';
import {
filterPlayable,
filterTimedEvents,
getLastEvent,
getLastNormal,
getNext,
getNextEvent,
getPrevious,
getPreviousEvent,
getRelevantBlock,
swapEventData,
} from './rundownUtils';
@@ -262,4 +265,59 @@ describe('getLastEvent', () => {
expect(lastEntry).toBe(null);
});
});
describe('relevantBlock', () => {
const testRundown = [
{ id: 'a', type: SupportedEvent.Event },
{ id: 'b', type: SupportedEvent.Event },
{ id: 'c', type: SupportedEvent.Event },
{ id: 'd', type: SupportedEvent.Delay },
{ id: 'e', type: SupportedEvent.Block },
{ id: 'f', type: SupportedEvent.Event },
{ id: 'g', type: SupportedEvent.Block },
{ id: 'h', type: SupportedEvent.Event },
];
it('returns the relevant block', () => {
const block = getRelevantBlock(testRundown as unknown as OntimeRundown, 'h');
expect(block?.id).toBe('g');
});
it('returns the relevant block', () => {
const block = getRelevantBlock(testRundown as unknown as OntimeRundown, 'f');
expect(block?.id).toBe('e');
});
it('returns the relevant block', () => {
const block = getRelevantBlock(testRundown as unknown as OntimeRundown, 'a');
expect(block).toBeNull();
});
it('also works on index 0', () => {
testRundown.unshift({ id: '0', type: SupportedEvent.Block });
const block = getRelevantBlock(testRundown as unknown as OntimeRundown, 'a');
expect(block?.id).toBe('0');
});
});
describe('filter event', () => {
const eventA = { id: 'a', type: SupportedEvent.Event } as OntimeEvent;
const eventB = { id: 'b', skip: true, type: SupportedEvent.Event } as OntimeEvent;
const testRundown = [
eventA,
eventB,
{ id: 'c', type: SupportedEvent.Delay },
{ id: 'd', type: SupportedEvent.Block },
];
test('filterPlayable', () => {
const result = filterPlayable(testRundown as unknown as OntimeRundown);
expect(result).toMatchObject([eventA]);
});
test('filterTimedEvents', () => {
const result = filterTimedEvents(testRundown as unknown as OntimeRundown);
expect(result).toMatchObject([eventA, eventB]);
});
});
});
@@ -1,5 +1,5 @@
import type { NormalisedRundown, OntimeEvent, OntimeRundown, OntimeRundownEntry } from 'ontime-types';
import { isOntimeEvent } from 'ontime-types';
import type { NormalisedRundown, OntimeBlock, OntimeEvent, OntimeRundown, OntimeRundownEntry } from 'ontime-types';
import { isOntimeBlock, isOntimeEvent } from 'ontime-types';
type IndexAndEntry = { entry: OntimeRundownEntry | null; index: number | null };
@@ -326,3 +326,44 @@ export const swapEventData = (eventA: OntimeEvent, eventB: OntimeEvent): { newA:
return { newA, newB };
};
/**
* Gets relevant block element for a given ID
* @param rundown
* @param order
* @param {string} currentId
* @return {OntimeBlock | null}
*/
export function getRelevantBlock(rundown: OntimeRundown, currentId: string): OntimeBlock | null {
let inBlock = false;
// Iterate backwards through the rundown to find the current event
for (let i = rundown.length - 1; i >= 0; i--) {
const entry = rundown[i];
if (entry.id === currentId) {
//set the flag when the current event is found
inBlock = true;
}
//the first block before the current event is the relevant one
if (inBlock && isOntimeBlock(entry)) {
return entry;
}
}
//no blocks exist before current event
return null;
}
/**
* returns all events that can be loaded
* @return {array}
*/
export function filterPlayable(rundown: OntimeRundown): OntimeEvent[] {
return rundown.filter((event) => isOntimeEvent(event) && !event.skip) as OntimeEvent[];
}
/**
* returns all events of type OntimeEvent
* @return {array}
*/
export function filterTimedEvents(rundown: OntimeRundown): OntimeEvent[] {
return rundown.filter((event) => isOntimeEvent(event)) as OntimeEvent[];
}