From f3610059ef37be08e48a2df79bee067100710556 Mon Sep 17 00:00:00 2001 From: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Date: Sat, 12 Aug 2023 11:24:35 +0200 Subject: [PATCH] fix paused (#486) * fix: prevent timer continue on paused count up * refactor: throw if not found on delete --- apps/server/src/services/TimerService.ts | 9 ++++--- .../src/services/__tests__/timerUtils.test.ts | 26 ++++--------------- .../rundown-service/RundownService.ts | 16 ++++++++---- .../rundown-service/delayedRundown.utils.ts | 8 +++++- apps/server/src/services/timerUtils.ts | 15 ----------- 5 files changed, 28 insertions(+), 46 deletions(-) diff --git a/apps/server/src/services/TimerService.ts b/apps/server/src/services/TimerService.ts index ce76befab3..2cad606216 100644 --- a/apps/server/src/services/TimerService.ts +++ b/apps/server/src/services/TimerService.ts @@ -5,7 +5,7 @@ import { eventStore } from '../stores/EventStore.js'; import { PlaybackService } from './PlaybackService.js'; import { updateRoll } from './rollUtils.js'; import { integrationService } from './integration-service/IntegrationService.js'; -import { getCurrent, getElapsed, getExpectedFinish } from './timerUtils.js'; +import { getCurrent, getExpectedFinish } from './timerUtils.js'; import { clock } from './Clock.js'; import { logger } from '../classes/Logger.js'; @@ -280,7 +280,7 @@ export class TimerService { this.timer.current = updatedTimer; this.timer.secondaryTimer = updatedSecondaryTimer; - this.timer.elapsed = getElapsed(this.timer.startedAt, this.timer.clock); + this.timer.elapsed = this.timer.duration - this.timer.current; if (isFinished) { this.timer.selectedEventId = null; @@ -299,7 +299,8 @@ export class TimerService { this.pausedTime = this.timer.clock - this.pausedAt; } - if (this.playback === Playback.Play && this.timer.current <= 0 && this.timer.finishedAt === null) { + const finishedNow = this.timer.current <= 0 && this.timer.finishedAt === null; + if (this.playback === Playback.Play && finishedNow) { this.timer.finishedAt = this.timer.clock; this._onFinish(); } else { @@ -318,7 +319,7 @@ export class TimerService { this.pausedTime, this.timer.clock, ); - this.timer.elapsed = getElapsed(this.timer.startedAt, this.timer.clock); + this.timer.elapsed = this.timer.duration - this.timer.current; } update(force = false) { diff --git a/apps/server/src/services/__tests__/timerUtils.test.ts b/apps/server/src/services/__tests__/timerUtils.test.ts index 30d3b946d4..1d26b9ba33 100644 --- a/apps/server/src/services/__tests__/timerUtils.test.ts +++ b/apps/server/src/services/__tests__/timerUtils.test.ts @@ -1,6 +1,6 @@ import { dayInMs } from 'ontime-utils'; -import { getCurrent, getElapsed, getExpectedFinish } from '../timerUtils.js'; +import { getCurrent, getExpectedFinish } from '../timerUtils.js'; describe('getExpectedFinish()', () => { it('is null if we havent started', () => { @@ -134,23 +134,7 @@ describe('getCurrent()', () => { }); }); -describe('getElapsedTime()', () => { - it('time since we started', () => { - const startedAt = 0; - const clock = 5; - const elapsed = getElapsed(startedAt, clock); - expect(elapsed).toBe(5); - }); - it('rolls past midnight', () => { - const startedAt = 10; - const clock = 5; - const elapsed = getElapsed(startedAt, clock); - - expect(elapsed).toBe(dayInMs - startedAt + clock); - }); -}); - -describe('getExpectedFinish() getElapsedTime() and getCurrentTime() combined', () => { +describe('getExpectedFinish() and getCurrentTime() combined', () => { it('without added times, they combine to be duration', () => { const startedAt = 0; const duration = 10; @@ -159,8 +143,8 @@ describe('getExpectedFinish() getElapsedTime() and getCurrentTime() combined', ( const addedTime = 0; const clock = 0; const expectedFinish = getExpectedFinish(startedAt, finishedAt, duration, pausedTime, addedTime); - const elapsed = getElapsed(startedAt, clock); const current = getCurrent(startedAt, duration, addedTime, pausedTime, clock); + const elapsed = duration - current; expect(expectedFinish).toBe(10); expect(elapsed).toBe(0); expect(current).toBe(10); @@ -174,10 +158,10 @@ describe('getExpectedFinish() getElapsedTime() and getCurrentTime() combined', ( const addedTime = 2; const clock = 5; const expectedFinish = getExpectedFinish(startedAt, finishedAt, duration, pausedTime, addedTime); - const elapsed = getElapsed(startedAt, clock); const current = getCurrent(startedAt, duration, addedTime, pausedTime, clock); + const elapsed = duration - current; expect(expectedFinish).toBe(13); - expect(elapsed).toBe(5); + expect(elapsed).toBe(2); expect(current).toBe(8); }); }); diff --git a/apps/server/src/services/rundown-service/RundownService.ts b/apps/server/src/services/rundown-service/RundownService.ts index e752449195..bfaa2f385d 100644 --- a/apps/server/src/services/rundown-service/RundownService.ts +++ b/apps/server/src/services/rundown-service/RundownService.ts @@ -16,7 +16,14 @@ import { EventLoader, eventLoader } from '../../classes/event-loader/EventLoader import { eventTimer } from '../TimerService.js'; import { sendRefetch } from '../../adapters/websocketAux.js'; import { runtimeCacheStore } from '../../stores/cachingStore.js'; -import { cachedAdd, cachedDelete, cachedEdit, cachedReorder, delayedRundownCacheKey } from './delayedRundown.utils.js'; +import { + cachedAdd, + cachedClear, + cachedDelete, + cachedEdit, + cachedReorder, + delayedRundownCacheKey, +} from './delayedRundown.utils.js'; import { logger } from '../../classes/Logger.js'; import { clock } from '../Clock.js'; @@ -221,9 +228,6 @@ export async function deleteEvent(eventId) { // notify event loader that rundown size has changed updateChangeNumEvents(); - // invalidate cache - runtimeCacheStore.invalidate(delayedRundownCacheKey); - // advice socket subscribers of change sendRefetch(); } @@ -233,7 +237,9 @@ export async function deleteEvent(eventId) { * @returns {Promise} */ export async function deleteAllEvents() { - await DataProvider.clearRundown(); + await cachedClear(); + + // notify timer service of changed events updateTimer(); forceReset(); } diff --git a/apps/server/src/services/rundown-service/delayedRundown.utils.ts b/apps/server/src/services/rundown-service/delayedRundown.utils.ts index 1d1b2cc6d9..7fc19c12bd 100644 --- a/apps/server/src/services/rundown-service/delayedRundown.utils.ts +++ b/apps/server/src/services/rundown-service/delayedRundown.utils.ts @@ -118,7 +118,7 @@ export async function cachedDelete(eventId: string) { if (delayedRundown.findIndex((event) => event.id === eventId) >= 0) { invalidateFromError(); } - return; + throw new Error(`Event with id ${eventId} not found`); } let updatedRundown = DataProvider.getRundown(); @@ -171,6 +171,12 @@ export async function cachedReorder(eventId: string, from: number, to: number) { return reorderedEvent; } +export async function cachedClear() { + await DataProvider.clearRundown(); + runtimeCacheStore.setCached(delayedRundownCacheKey, []); + console.log(DataProvider.getRundown(), getDelayedRundown()); +} + /** * Calculates all delays in a given rundown * @param rundown diff --git a/apps/server/src/services/timerUtils.ts b/apps/server/src/services/timerUtils.ts index 619eae5bb7..6ee9993b98 100644 --- a/apps/server/src/services/timerUtils.ts +++ b/apps/server/src/services/timerUtils.ts @@ -47,18 +47,3 @@ export function getCurrent( } return startedAt + duration + addedTime + pausedTime - clock; } - -/** - * Calculates elapsed time - */ -export function getElapsed(startedAt: number | null, clock: number): number | null { - if (startedAt === null) { - return null; - } - - // we are in the day after - if (startedAt > clock) { - return dayInMs - startedAt + clock; - } - return clock - startedAt; -}