Allow time change from api (#1009)

Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com>
This commit is contained in:
Alex Christoffer Rasmussen
2024-06-02 15:21:09 +02:00
committed by GitHub
parent d8626ff324
commit 3895b37572
6 changed files with 88 additions and 14 deletions
@@ -10,6 +10,10 @@ import { eventStore } from '../stores/EventStore.js';
import * as assert from '../utils/assert.js';
import { isEmptyObject } from '../utils/parserUtils.js';
import { parseProperty, updateEvent } from './integration.utils.js';
import { throttle } from '../utils/throttle.js';
import { willCauseRegeneration } from '../services/rundown-service/rundownCacheUtils.js';
const throttledUpdateEvent = throttle(updateEvent, 20);
export function dispatchFromAdapter(type: string, payload: unknown, _source?: 'osc' | 'ws' | 'http') {
const action = type.toLowerCase();
@@ -43,6 +47,8 @@ const actionHandlers: Record<string, ActionHandler> = {
const data = payload[id as keyof typeof payload];
const patchEvent: Partial<OntimeEvent> & { id: string } = { id };
let shouldThrottle = false;
Object.entries(data).forEach(([property, value]) => {
if (typeof property !== 'string' || value === undefined) {
throw new Error('Invalid property or value');
@@ -50,6 +56,9 @@ const actionHandlers: Record<string, ActionHandler> = {
const newObjectProperty = parseProperty(property, value);
const key = Object.keys(newObjectProperty)[0] as keyof OntimeEvent;
shouldThrottle = willCauseRegeneration(key) || shouldThrottle;
if (patchEvent.custom && newObjectProperty.custom) {
Object.assign(patchEvent.custom, newObjectProperty.custom);
} else {
@@ -57,8 +66,13 @@ const actionHandlers: Record<string, ActionHandler> = {
}
});
updateEvent(patchEvent);
if (shouldThrottle) {
if (throttledUpdateEvent(patchEvent)) {
return { payload: 'throttled' };
}
} else {
updateEvent(patchEvent);
}
return { payload: 'success' };
},
/* Message Service */
@@ -1,24 +1,45 @@
import { OntimeEvent, isKeyOfType, isOntimeEvent } from 'ontime-types';
import { EndAction, OntimeEvent, TimerType, isKeyOfType, isOntimeEvent } from 'ontime-types';
import { MILLIS_PER_SECOND, maxDuration } from 'ontime-utils';
import { DataProvider } from '../classes/data-provider/DataProvider.js';
import { editEvent } from '../services/rundown-service/RundownService.js';
import { getEventWithId } from '../services/rundown-service/rundownUtils.js';
import { coerceBoolean, coerceColour, coerceNumber, coerceString } from '../utils/coerceType.js';
import { coerceBoolean, coerceColour, coerceEnum, coerceNumber, coerceString } from '../utils/coerceType.js';
const whitelistedPayload = {
/**
*
* @param {number} value time amount in seconds
* @returns {number} time in milliseconds clamped to 0 and max duration
*/
function clampDuration(value: number) {
const valueInMillis = value * MILLIS_PER_SECOND;
if (valueInMillis > maxDuration || valueInMillis < 0) {
throw new Error('Times should be from 0 to 23:59:59');
}
return valueInMillis;
}
const propertyConversion = {
title: coerceString,
note: coerceString,
cue: coerceString,
duration: (value: unknown) => Math.max(coerceNumber(value) * MILLIS_PER_SECOND, maxDuration),
isPublic: coerceBoolean,
skip: coerceBoolean,
colour: coerceColour,
custom: coerceString,
timeWarning: (value: unknown) => clampDuration(coerceNumber(value)),
timeDanger: (value: unknown) => clampDuration(coerceNumber(value)),
endAction: (value: unknown) => coerceEnum<EndAction>(value, EndAction),
timerType: (value: unknown) => coerceEnum<TimerType>(value, TimerType),
duration: (value: unknown) => clampDuration(coerceNumber(value)),
timeStart: (value: unknown) => clampDuration(coerceNumber(value)),
timeEnd: (value: unknown) => clampDuration(coerceNumber(value)),
};
export function parseProperty(property: string, value: unknown) {
@@ -27,13 +48,13 @@ export function parseProperty(property: string, value: unknown) {
if (!(customKey in DataProvider.getCustomFields())) {
throw new Error(`Custom field ${customKey} not found`);
}
const parserFn = whitelistedPayload.custom;
const parserFn = propertyConversion.custom;
return { custom: { [customKey]: parserFn(value) } };
}
if (!isKeyOfType(property, whitelistedPayload)) {
if (!isKeyOfType(property, propertyConversion)) {
throw new Error(`Property ${property} not permitted`);
}
const parserFn = whitelistedPayload[property];
const parserFn = propertyConversion[property];
return { [property]: parserFn(value) };
}
@@ -50,6 +71,5 @@ export function updateEvent(patchEvent: Partial<OntimeEvent> & { id: string }) {
if (!isOntimeEvent(event)) {
throw new Error('Can only update events');
}
editEvent(patchEvent);
}