Event trigger (#1557)

* crude UI

* add server functionalety

* better event trigger list

* fix default value

* remove log

* add trigger to recalculate whitelist

* use DTO type

* fix rebase

* one callback for delete and submit

* refactor

* add invalid description

* clean up commits

* cleanup

* refactor: find event triggers in `triggerAutomations`

* refactor: switch to flat array for event triggers

* prevent deleting a automation that is in use

* refactor

* refactor: extract EventEditorCustom

* move trigger options to separate file

* move trigger edit down a file level

* cleanup

* spelling

* refactor: ui review proposal

* change default value

* fix delete filter

---------

Co-authored-by: Carlos Valente <carlosvalente@pm.me>
This commit is contained in:
Alex Christoffer Rasmussen
2025-04-19 21:27:07 +02:00
committed by GitHub
parent 6817263123
commit a94b10cb0f
13 changed files with 339 additions and 74 deletions
@@ -9,12 +9,13 @@ import type {
import { deleteAtIndex, generateId } from 'ontime-utils';
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
import { getTimedEvents } from '../../services/rundown-service/rundownUtils.js';
/**
* Gets a copy of the stored automation settings
*/
export function getAutomationSettings(): AutomationSettings {
return structuredClone(getDataProvider().getAutomation());
return getDataProvider().getAutomation();
}
/**
@@ -138,14 +139,25 @@ export async function deleteAutomation(id: string): Promise<void> {
if (!Object.hasOwn(automations, id)) {
return;
}
// prevent deleting a automation that is in use
const triggers = getAutomationTriggers();
for (let i = 0; i < triggers.length; i++) {
const trigger = triggers[i];
if (trigger.automationId === id) {
throw new Error(`Unable to delete automation used in trigger ${trigger.title}`);
}
// prevent deleting a automation that is in use in triggers
const triggers = getAutomationTriggers().filter((trigger) => trigger.automationId === id);
if (triggers.length) {
throw new Error(
`Unable to delete automation used in trigger ${triggers[0].title}${triggers.length > 1 ? ` and ${triggers.length - 1} more` : ''}`,
);
}
// prevent deleting a automation that is in use in events
const events = getTimedEvents().filter(
(event) => event.triggers && event.triggers.some((trigger) => trigger.automationId === id),
);
if (events.length) {
throw new Error(
`Unable to delete automation used in event: ${events[0].id}${events.length > 1 ? ` and ${events.length - 1} more` : ''}`,
);
}
delete automations[id];
await saveChanges({ automations });
}
@@ -3,10 +3,10 @@ import {
isOntimeAction,
isOSCOutput,
LogOrigin,
TimerLifeCycle,
type AutomationFilter,
type AutomationOutput,
type FilterRule,
type TimerLifeCycle,
} from 'ontime-types';
import { getPropertyFromPath } from 'ontime-utils';
@@ -23,14 +23,21 @@ import { toOntimeAction } from './clients/ontime.client.js';
/**
* Exposes a method for triggering actions based on a TimerLifeCycle event
*/
export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) {
export function triggerAutomations(cycle: TimerLifeCycle, state: RuntimeState) {
if (!getAutomationsEnabled()) {
return;
}
const triggers = getAutomationTriggers();
const triggerAutomations = triggers.filter((trigger) => trigger.trigger === event);
if (triggerAutomations.length === 0) {
let triggers = getAutomationTriggers();
// get triggers from event
if (state.eventNow?.triggers) {
triggers = triggers.concat(state.eventNow.triggers);
}
// note: there are no onStop triggers in event
const filteredTrigger = triggers.filter((trigger) => trigger.trigger === cycle);
if (filteredTrigger.length === 0) {
return;
}
@@ -39,7 +46,7 @@ export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) {
return;
}
triggerAutomations.forEach((trigger) => {
filteredTrigger.forEach((trigger) => {
const automation = automations[trigger.automationId];
if (!automation || automation.outputs.length === 0) {
return;