Skip to content

Commit

Permalink
fix paused (#486)
Browse files Browse the repository at this point in the history
* fix: prevent timer continue on paused count up

* refactor: throw if not found on delete
  • Loading branch information
cpvalente authored Aug 12, 2023
1 parent 023aac4 commit f361005
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 46 deletions.
9 changes: 5 additions & 4 deletions apps/server/src/services/TimerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
26 changes: 5 additions & 21 deletions apps/server/src/services/__tests__/timerUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
});
});
16 changes: 11 additions & 5 deletions apps/server/src/services/rundown-service/RundownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
}
Expand All @@ -233,7 +237,9 @@ export async function deleteEvent(eventId) {
* @returns {Promise<void>}
*/
export async function deleteAllEvents() {
await DataProvider.clearRundown();
await cachedClear();

// notify timer service of changed events
updateTimer();
forceReset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions apps/server/src/services/timerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit f361005

Please sign in to comment.