refactor: migrate triggers to new service

This commit is contained in:
Carlos Valente
2025-01-11 16:02:23 +01:00
committed by Carlos Valente
parent 6c4d80158c
commit 1f71d4578c
3 changed files with 26 additions and 13 deletions
@@ -17,6 +17,13 @@ export function getAutomationSettings(): AutomationSettings {
return structuredClone(getDataProvider().getAutomation());
}
/**
* Gets the enabled status of the automations
*/
export function getAutomationsEnabled(): boolean {
return getAutomationSettings().enabledAutomations;
}
/**
* Gets a copy of the stored automations
*/
@@ -12,13 +12,17 @@ import { getState, type RuntimeState } from '../../stores/runtimeState.js';
import { emitOSC } from './clients/osc.client.js';
import { emitHTTP } from './clients/http.client.js';
import { getAutomations, getBlueprints } from './automation.dao.js';
import { getAutomations, getAutomationsEnabled, getBlueprints } from './automation.dao.js';
import { isOntimeCloud } from '../../externals.js';
/**
* Exposes a method for triggering actions based on a TimerLifeCycle event
*/
export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) {
if (!getAutomationsEnabled()) {
return;
}
const automations = getAutomations();
const triggerAutomations = automations.filter((automation) => automation.trigger === event);
if (triggerAutomations.length === 0) {
@@ -36,6 +36,7 @@ import { integrationService } from '../integration-service/IntegrationService.js
import { getForceUpdate, getShouldClockUpdate, getShouldTimerUpdate } from './rundownService.utils.js';
import { skippedOutOfEvent } from '../timerUtils.js';
import { triggerAutomations } from '../../api-data/automation/automation.service.js';
/**
* Service manages runtime status of app
@@ -77,11 +78,11 @@ class RuntimeService {
if (timerPhaseChanged) {
if (newState.timer.phase === TimerPhase.Warning) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onWarning);
triggerAutomations(TimerLifeCycle.onWarning, newState);
});
} else if (newState.timer.phase === TimerPhase.Danger) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onDanger);
triggerAutomations(TimerLifeCycle.onDanger, newState);
});
}
}
@@ -97,7 +98,7 @@ class RuntimeService {
} else if (hasTimerFinished) {
// if the timer has finished, we need to load next and keep rolling
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onFinish);
triggerAutomations(TimerLifeCycle.onFinish, newState);
});
this.handleLoadNext();
this.rollLoaded(keepOffset);
@@ -117,7 +118,7 @@ class RuntimeService {
// 3. find if we need to process actions related to the timer finishing
if (newState.timer.playback === Playback.Play && hasTimerFinished) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onFinish);
triggerAutomations(TimerLifeCycle.onFinish, newState);
});
// handle end action if there was a timer playing
@@ -137,7 +138,7 @@ class RuntimeService {
const shouldUpdateTimer = getShouldTimerUpdate(this.lastIntegrationTimerValue, newState.timer.current);
if (shouldUpdateTimer) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onUpdate);
triggerAutomations(TimerLifeCycle.onUpdate, newState);
});
this.lastIntegrationTimerValue = newState.timer.current ?? -1;
@@ -147,7 +148,7 @@ class RuntimeService {
const shouldUpdateClock = getShouldClockUpdate(this.lastIntegrationClockUpdate, newState.clock);
if (shouldUpdateClock) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onClock);
triggerAutomations(TimerLifeCycle.onClock, newState);
});
this.lastIntegrationClockUpdate = newState.clock;
@@ -294,7 +295,7 @@ class RuntimeService {
if (success) {
logger.info(LogOrigin.Playback, `Loaded event with ID ${event.id}`);
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onLoad);
triggerAutomations(TimerLifeCycle.onLoad, runtimeState.getState());
});
}
return success;
@@ -473,7 +474,7 @@ class RuntimeService {
if (didStart) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onStart);
triggerAutomations(TimerLifeCycle.onStart, newState);
});
}
return didStart;
@@ -526,7 +527,7 @@ class RuntimeService {
const newState = runtimeState.getState();
logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`);
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onPause);
triggerAutomations(TimerLifeCycle.onPause, newState);
});
}
@@ -545,7 +546,7 @@ class RuntimeService {
const newState = runtimeState.getState();
logger.info(LogOrigin.Playback, `Play Mode ${newState.timer.playback.toUpperCase()}`);
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onStop);
triggerAutomations(TimerLifeCycle.onStop, newState);
});
return true;
@@ -593,16 +594,17 @@ class RuntimeService {
try {
const rundown = getRundown();
const result = runtimeState.roll(rundown);
const newState = runtimeState.getState();
if (result.eventId !== previousState.eventNow?.id) {
logger.info(LogOrigin.Playback, `Loaded event with ID ${result.eventId}`);
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onLoad);
triggerAutomations(TimerLifeCycle.onLoad, newState);
});
}
if (result.didStart) {
process.nextTick(() => {
integrationService.dispatch(TimerLifeCycle.onStart);
triggerAutomations(TimerLifeCycle.onStart, newState);
});
}
} catch (error) {