fix: apply delay (#494)

* fix: bug with applying delays

* fix: show delay data only in production screens

* chore: cover delay with feature test

* refactor: simplify type assertion
This commit is contained in:
Carlos Valente
2023-08-23 22:31:15 +02:00
committed by GitHub
parent 657cc22b44
commit e5fdf27f6c
16 changed files with 517 additions and 431 deletions
@@ -4,7 +4,6 @@ import {
OntimeBlock,
OntimeDelay,
OntimeEvent,
OntimeRundown,
Playback,
SupportedEvent,
} from 'ontime-types';
@@ -18,6 +17,7 @@ import { sendRefetch } from '../../adapters/websocketAux.js';
import { runtimeCacheStore } from '../../stores/cachingStore.js';
import {
cachedAdd,
cachedApplyDelay,
cachedClear,
cachedDelete,
cachedEdit,
@@ -166,7 +166,7 @@ export async function addEvent(eventData: Partial<OntimeEvent> | Partial<OntimeD
const id = generateId();
let insertIndex = 0;
if (eventData?.after !== 'undefined') {
if (eventData?.after !== undefined) {
const index = DataProvider.getIndexOf(eventData.after);
if (index < 0) {
logger.warning(LogOrigin.Server, `Could not find event with id ${eventData.after}`);
@@ -268,46 +268,14 @@ export async function reorderEvent(eventId: string, from: number, to: number) {
return reorderedItem;
}
export function _applyDelay(
eventId: string,
rundown: OntimeRundown,
): {
delayIndex: number | null;
updatedRundown: OntimeRundown;
} {
const updatedRundown = [...rundown];
let delayIndex = null;
let delayValue = 0;
export async function applyDelay(eventId: string) {
await cachedApplyDelay(eventId);
for (const [index, event] of updatedRundown.entries()) {
// look for delay
if (delayIndex === null) {
if (event.type === SupportedEvent.Delay && event.id === eventId) {
delayValue = event.duration;
delayIndex = index;
// notify timer service of changed events
updateTimer();
if (delayValue === 0) {
// nothing to apply
break;
}
}
continue;
}
// once delay is found, apply delay value to all items until block or end
if (event.type === SupportedEvent.Event) {
updatedRundown[index] = {
...event,
timeStart: Math.max(0, event.timeStart + delayValue),
timeEnd: Math.max(event.duration, event.timeEnd + delayValue),
revision: event.revision + 1,
};
} else if (event.type === SupportedEvent.Block) {
break;
}
}
return { delayIndex, updatedRundown };
// advice socket subscribers of change
sendRefetch();
}
/**
@@ -326,22 +294,6 @@ export async function swapEvents(from: string, to: string) {
sendRefetch();
}
/**
* applies delay value for given event
* @param eventId
* @returns {Promise<void>}
*/
export async function applyDelay(eventId: string) {
const rundown: OntimeRundown = DataProvider.getRundown();
const { delayIndex, updatedRundown } = _applyDelay(eventId, rundown);
if (delayIndex === null) {
throw new Error(`Delay event with ID ${eventId} not found`);
}
await DataProvider.setRundown(updatedRundown);
await deleteEvent(eventId);
}
/**
* Forces update in the store
* Called when we make changes to the rundown object