From cfcf6a56847a6f44870a058c48065d48dd53b893 Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Wed, 18 Dec 2024 12:44:48 +0100 Subject: [PATCH] extract isPlaybackActive to a global util function (#1393) --- apps/server/src/services/timerUtils.ts | 19 +++---------------- .../src/stores/__tests__/runtimeState.test.ts | 1 + apps/server/src/stores/runtimeState.ts | 12 +++++++++--- packages/utils/index.ts | 2 ++ .../utils/src/playback-utils/playbackstate.ts | 10 ++++++++++ 5 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 packages/utils/src/playback-utils/playbackstate.ts diff --git a/apps/server/src/services/timerUtils.ts b/apps/server/src/services/timerUtils.ts index df7d3f7c2..3b56e0e12 100644 --- a/apps/server/src/services/timerUtils.ts +++ b/apps/server/src/services/timerUtils.ts @@ -1,5 +1,5 @@ -import { MaybeNumber, Playback, TimerPhase } from 'ontime-types'; -import { dayInMs } from 'ontime-utils'; +import { MaybeNumber, TimerPhase } from 'ontime-types'; +import { dayInMs, isPlaybackActive } from 'ontime-utils'; import { RuntimeState } from '../stores/runtimeState.js'; /** @@ -187,25 +187,12 @@ export function getExpectedEnd(state: RuntimeState): MaybeNumber { return state.runtime.plannedEnd - state.runtime.offset + state._timer.totalDelay; } -/** - * Utility checks whether the playback is considered to be active - * @param state - * @returns - */ -export function isPlaybackActive(state: RuntimeState): boolean { - return ( - state.timer.playback === Playback.Play || - state.timer.playback === Playback.Pause || - state.timer.playback === Playback.Roll - ); -} - /** * Checks running timer to see which phase it currently is in * @param state */ export function getTimerPhase(state: RuntimeState): TimerPhase { - if (!isPlaybackActive(state)) { + if (!isPlaybackActive(state.timer.playback)) { return TimerPhase.None; } diff --git a/apps/server/src/stores/__tests__/runtimeState.test.ts b/apps/server/src/stores/__tests__/runtimeState.test.ts index 372c0f07f..adf309731 100644 --- a/apps/server/src/stores/__tests__/runtimeState.test.ts +++ b/apps/server/src/stores/__tests__/runtimeState.test.ts @@ -175,6 +175,7 @@ describe('mutation on runtimeState', () => { expect(newState.runtime.plannedStart).toBe(0); expect(newState.runtime.plannedEnd).toBe(1500); expect(newState.currentBlock.block).toBeNull(); + expect(newState.runtime.offset).toBe(0); // 2. Start event start(); diff --git a/apps/server/src/stores/runtimeState.ts b/apps/server/src/stores/runtimeState.ts index f11dec294..55b0eba37 100644 --- a/apps/server/src/stores/runtimeState.ts +++ b/apps/server/src/stores/runtimeState.ts @@ -11,7 +11,14 @@ import { TimerPhase, TimerState, } from 'ontime-types'; -import { calculateDuration, checkIsNow, dayInMs, filterTimedEvents, getPreviousBlock } from 'ontime-utils'; +import { + calculateDuration, + checkIsNow, + dayInMs, + filterTimedEvents, + getPreviousBlock, + isPlaybackActive, +} from 'ontime-utils'; import { clock } from '../services/Clock.js'; import { RestorePoint } from '../services/RestoreService.js'; @@ -21,7 +28,6 @@ import { getExpectedFinish, getRuntimeOffset, getTimerPhase, - isPlaybackActive, } from '../services/timerUtils.js'; import { timerConfig } from '../config/config.js'; import { loadRoll, normaliseRollStart } from '../services/rollUtils.js'; @@ -485,7 +491,7 @@ export function update(): UpdateResult { runtimeState.clock = clock.timeNow(); // we update the clock on every update call // 1. is playback idle? - if (!isPlaybackActive(runtimeState)) { + if (!isPlaybackActive(runtimeState.timer.playback)) { return updateIfIdle(); } diff --git a/packages/utils/index.ts b/packages/utils/index.ts index 1429824ee..138b3c70a 100644 --- a/packages/utils/index.ts +++ b/packages/utils/index.ts @@ -89,3 +89,5 @@ export { defaultImportMap, isImportMap, } from './src/feature/spreadsheet-import/spreadsheetImport.js'; + +export { isPlaybackActive } from './src/playback-utils/playbackstate.js'; diff --git a/packages/utils/src/playback-utils/playbackstate.ts b/packages/utils/src/playback-utils/playbackstate.ts new file mode 100644 index 000000000..ed0717fbf --- /dev/null +++ b/packages/utils/src/playback-utils/playbackstate.ts @@ -0,0 +1,10 @@ +import { Playback } from 'ontime-types'; + +/** + * Utility checks whether the playback is considered to be active + * @param state + * @returns + */ +export function isPlaybackActive(state: Playback): boolean { + return state === Playback.Play || state === Playback.Pause || state === Playback.Roll; +}