fix paused (#486)

* fix: prevent timer continue on paused count up

* refactor: throw if not found on delete
This commit is contained in:
Carlos Valente
2023-08-12 11:24:35 +02:00
committed by GitHub
parent 023aac4ead
commit f3610059ef
5 changed files with 28 additions and 46 deletions
+5 -4
View File
@@ -5,7 +5,7 @@ import { eventStore } from '../stores/EventStore.js';
import { PlaybackService } from './PlaybackService.js'; import { PlaybackService } from './PlaybackService.js';
import { updateRoll } from './rollUtils.js'; import { updateRoll } from './rollUtils.js';
import { integrationService } from './integration-service/IntegrationService.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 { clock } from './Clock.js';
import { logger } from '../classes/Logger.js'; import { logger } from '../classes/Logger.js';
@@ -280,7 +280,7 @@ export class TimerService {
this.timer.current = updatedTimer; this.timer.current = updatedTimer;
this.timer.secondaryTimer = updatedSecondaryTimer; 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) { if (isFinished) {
this.timer.selectedEventId = null; this.timer.selectedEventId = null;
@@ -299,7 +299,8 @@ export class TimerService {
this.pausedTime = this.timer.clock - this.pausedAt; 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.timer.finishedAt = this.timer.clock;
this._onFinish(); this._onFinish();
} else { } else {
@@ -318,7 +319,7 @@ export class TimerService {
this.pausedTime, this.pausedTime,
this.timer.clock, 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) { update(force = false) {
@@ -1,6 +1,6 @@
import { dayInMs } from 'ontime-utils'; import { dayInMs } from 'ontime-utils';
import { getCurrent, getElapsed, getExpectedFinish } from '../timerUtils.js'; import { getCurrent, getExpectedFinish } from '../timerUtils.js';
describe('getExpectedFinish()', () => { describe('getExpectedFinish()', () => {
it('is null if we havent started', () => { it('is null if we havent started', () => {
@@ -134,23 +134,7 @@ describe('getCurrent()', () => {
}); });
}); });
describe('getElapsedTime()', () => { describe('getExpectedFinish() and getCurrentTime() combined', () => {
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', () => {
it('without added times, they combine to be duration', () => { it('without added times, they combine to be duration', () => {
const startedAt = 0; const startedAt = 0;
const duration = 10; const duration = 10;
@@ -159,8 +143,8 @@ describe('getExpectedFinish() getElapsedTime() and getCurrentTime() combined', (
const addedTime = 0; const addedTime = 0;
const clock = 0; const clock = 0;
const expectedFinish = getExpectedFinish(startedAt, finishedAt, duration, pausedTime, addedTime); const expectedFinish = getExpectedFinish(startedAt, finishedAt, duration, pausedTime, addedTime);
const elapsed = getElapsed(startedAt, clock);
const current = getCurrent(startedAt, duration, addedTime, pausedTime, clock); const current = getCurrent(startedAt, duration, addedTime, pausedTime, clock);
const elapsed = duration - current;
expect(expectedFinish).toBe(10); expect(expectedFinish).toBe(10);
expect(elapsed).toBe(0); expect(elapsed).toBe(0);
expect(current).toBe(10); expect(current).toBe(10);
@@ -174,10 +158,10 @@ describe('getExpectedFinish() getElapsedTime() and getCurrentTime() combined', (
const addedTime = 2; const addedTime = 2;
const clock = 5; const clock = 5;
const expectedFinish = getExpectedFinish(startedAt, finishedAt, duration, pausedTime, addedTime); const expectedFinish = getExpectedFinish(startedAt, finishedAt, duration, pausedTime, addedTime);
const elapsed = getElapsed(startedAt, clock);
const current = getCurrent(startedAt, duration, addedTime, pausedTime, clock); const current = getCurrent(startedAt, duration, addedTime, pausedTime, clock);
const elapsed = duration - current;
expect(expectedFinish).toBe(13); expect(expectedFinish).toBe(13);
expect(elapsed).toBe(5); expect(elapsed).toBe(2);
expect(current).toBe(8); expect(current).toBe(8);
}); });
}); });
@@ -16,7 +16,14 @@ import { EventLoader, eventLoader } from '../../classes/event-loader/EventLoader
import { eventTimer } from '../TimerService.js'; import { eventTimer } from '../TimerService.js';
import { sendRefetch } from '../../adapters/websocketAux.js'; import { sendRefetch } from '../../adapters/websocketAux.js';
import { runtimeCacheStore } from '../../stores/cachingStore.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 { logger } from '../../classes/Logger.js';
import { clock } from '../Clock.js'; import { clock } from '../Clock.js';
@@ -221,9 +228,6 @@ export async function deleteEvent(eventId) {
// notify event loader that rundown size has changed // notify event loader that rundown size has changed
updateChangeNumEvents(); updateChangeNumEvents();
// invalidate cache
runtimeCacheStore.invalidate(delayedRundownCacheKey);
// advice socket subscribers of change // advice socket subscribers of change
sendRefetch(); sendRefetch();
} }
@@ -233,7 +237,9 @@ export async function deleteEvent(eventId) {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
export async function deleteAllEvents() { export async function deleteAllEvents() {
await DataProvider.clearRundown(); await cachedClear();
// notify timer service of changed events
updateTimer(); updateTimer();
forceReset(); forceReset();
} }
@@ -118,7 +118,7 @@ export async function cachedDelete(eventId: string) {
if (delayedRundown.findIndex((event) => event.id === eventId) >= 0) { if (delayedRundown.findIndex((event) => event.id === eventId) >= 0) {
invalidateFromError(); invalidateFromError();
} }
return; throw new Error(`Event with id ${eventId} not found`);
} }
let updatedRundown = DataProvider.getRundown(); let updatedRundown = DataProvider.getRundown();
@@ -171,6 +171,12 @@ export async function cachedReorder(eventId: string, from: number, to: number) {
return reorderedEvent; return reorderedEvent;
} }
export async function cachedClear() {
await DataProvider.clearRundown();
runtimeCacheStore.setCached(delayedRundownCacheKey, []);
console.log(DataProvider.getRundown(), getDelayedRundown());
}
/** /**
* Calculates all delays in a given rundown * Calculates all delays in a given rundown
* @param rundown * @param rundown
-15
View File
@@ -47,18 +47,3 @@ export function getCurrent(
} }
return startedAt + duration + addedTime + pausedTime - clock; 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;
}