mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 09:23:51 +00:00
move skip logic to timerUtils
This commit is contained in:
@@ -9,5 +9,5 @@ export const config = {
|
||||
filename: 'override.css',
|
||||
},
|
||||
restoreFile: 'ontime.restore',
|
||||
rollSkipLimit: 3 * 32,
|
||||
timeSkipLimit: 3 * 32,
|
||||
};
|
||||
|
||||
@@ -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, getExpectedFinish } from './timerUtils.js';
|
||||
import { getCurrent, getExpectedFinish, skipedOutOfEvent } from './timerUtils.js';
|
||||
import { clock } from './Clock.js';
|
||||
import { logger } from '../classes/Logger.js';
|
||||
import type { RestorePoint } from './RestoreService.js';
|
||||
@@ -339,7 +339,6 @@ export class TimerService {
|
||||
this.timer.expectedFinish >= this.timer.startedAt
|
||||
? this.timer.expectedFinish
|
||||
: this.timer.expectedFinish + dayInMs,
|
||||
_startAt: this.timer.startedAt,
|
||||
clock: this.timer.clock,
|
||||
secondaryTimer: this.timer.secondaryTimer,
|
||||
secondaryTarget: this.secondaryTarget,
|
||||
@@ -405,7 +404,11 @@ export class TimerService {
|
||||
let shouldNotify = false;
|
||||
if (this.playback === Playback.Roll) {
|
||||
shouldNotify = true;
|
||||
this.updateRoll();
|
||||
if (skipedOutOfEvent(previousTime, this.timer.clock, this.timer.startedAt, this.timer.finishedAt)) {
|
||||
PlaybackService.roll();
|
||||
} else {
|
||||
this.updateRoll();
|
||||
}
|
||||
} else if (this.timer.startedAt !== null) {
|
||||
// we only update timer if a timer has been started
|
||||
shouldNotify = true;
|
||||
|
||||
@@ -2,7 +2,6 @@ import { OntimeEvent } from 'ontime-types';
|
||||
import { dayInMs } from 'ontime-utils';
|
||||
|
||||
import { getRollTimers, normaliseEndTime, sortArrayByProperty, updateRoll } from '../rollUtils.js';
|
||||
import { config } from '../../config/config.js';
|
||||
|
||||
// test sortArrayByProperty()
|
||||
describe('sort simple arrays of objects', () => {
|
||||
@@ -563,7 +562,6 @@ describe('typical scenarios', () => {
|
||||
selectedEventId: '1',
|
||||
current: 10,
|
||||
_finishAt: 15,
|
||||
_startAt: 9,
|
||||
clock: 11,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
@@ -586,72 +584,11 @@ describe('typical scenarios', () => {
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
});
|
||||
|
||||
it('dose not skip isFinished under limit', () => {
|
||||
const timers = {
|
||||
selectedEventId: '1',
|
||||
current: 13,
|
||||
_finishAt: 15,
|
||||
_startAt: 9,
|
||||
clock: 14,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
};
|
||||
|
||||
const expected = {
|
||||
updatedTimer: timers._finishAt - timers.clock,
|
||||
updatedSecondaryTimer: null,
|
||||
doRollLoad: false,
|
||||
isFinished: false,
|
||||
};
|
||||
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
|
||||
// test that it can jump time
|
||||
timers._finishAt = 14;
|
||||
timers.clock += config.rollSkipLimit - 1;
|
||||
expected.updatedTimer = timers._finishAt - timers.clock;
|
||||
expected.doRollLoad = true;
|
||||
expected.isFinished = true;
|
||||
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
});
|
||||
|
||||
it('dose skips isFinished over limit', () => {
|
||||
const timers = {
|
||||
selectedEventId: '1',
|
||||
current: 13,
|
||||
_finishAt: 15,
|
||||
_startAt: 9,
|
||||
clock: 14,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
};
|
||||
|
||||
const expected = {
|
||||
updatedTimer: timers._finishAt - timers.clock,
|
||||
updatedSecondaryTimer: null,
|
||||
doRollLoad: false,
|
||||
isFinished: false,
|
||||
};
|
||||
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
|
||||
// test that it can jump time
|
||||
timers._finishAt = 14;
|
||||
timers.clock += config.rollSkipLimit + 1;
|
||||
expected.updatedTimer = timers._finishAt - timers.clock;
|
||||
expected.doRollLoad = true;
|
||||
expected.isFinished = false;
|
||||
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
});
|
||||
|
||||
it('it updates secondary timer', () => {
|
||||
const timers = {
|
||||
selectedEventId: null,
|
||||
current: null,
|
||||
_finishAt: null,
|
||||
_startAt: null,
|
||||
clock: 11,
|
||||
secondaryTimer: 1,
|
||||
secondaryTarget: 15,
|
||||
@@ -672,7 +609,6 @@ describe('typical scenarios', () => {
|
||||
selectedEventId: '1',
|
||||
current: 10,
|
||||
_finishAt: 11,
|
||||
_startAt: 9,
|
||||
clock: 12,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
@@ -693,7 +629,6 @@ describe('typical scenarios', () => {
|
||||
selectedEventId: null,
|
||||
current: null,
|
||||
_finishAt: null,
|
||||
_startAt: null,
|
||||
clock: 16,
|
||||
secondaryTimer: 1,
|
||||
secondaryTarget: 15,
|
||||
@@ -714,7 +649,6 @@ describe('typical scenarios', () => {
|
||||
selectedEventId: null,
|
||||
current: null,
|
||||
_finishAt: null,
|
||||
_startAt: null,
|
||||
clock: 15,
|
||||
secondaryTimer: 0,
|
||||
secondaryTarget: 15,
|
||||
@@ -735,7 +669,6 @@ describe('typical scenarios', () => {
|
||||
selectedEventId: '1',
|
||||
current: 25,
|
||||
_finishAt: 10 + dayInMs,
|
||||
_startAt: 10,
|
||||
clock: dayInMs - 10,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
@@ -756,7 +689,6 @@ describe('typical scenarios', () => {
|
||||
selectedEventId: '1',
|
||||
current: dayInMs,
|
||||
_finishAt: 10 + dayInMs,
|
||||
_startAt: 10,
|
||||
clock: 10,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
@@ -771,24 +703,4 @@ describe('typical scenarios', () => {
|
||||
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
});
|
||||
|
||||
it('rolls backwards', () => {
|
||||
const timers = {
|
||||
selectedEventId: '1',
|
||||
current: 11,
|
||||
_startAt: 10,
|
||||
_finishAt: 15,
|
||||
clock: 9,
|
||||
secondaryTimer: null,
|
||||
secondaryTarget: null,
|
||||
};
|
||||
|
||||
const expected = {
|
||||
updatedTimer: timers._finishAt - timers.clock,
|
||||
updatedSecondaryTimer: null,
|
||||
doRollLoad: true,
|
||||
isFinished: false,
|
||||
};
|
||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { dayInMs } from 'ontime-utils';
|
||||
import { TimerType } from 'ontime-types';
|
||||
|
||||
import { getCurrent, getExpectedFinish } from '../timerUtils.js';
|
||||
import { getCurrent, getExpectedFinish, skipedOutOfEvent } from '../timerUtils.js';
|
||||
import { config } from '../../config/config.js';
|
||||
|
||||
describe('getExpectedFinish()', () => {
|
||||
it('is null if we havent started', () => {
|
||||
@@ -354,3 +355,24 @@ describe('getExpectedFinish() and getCurrentTime() combined', () => {
|
||||
expect(current).toBe(8);
|
||||
});
|
||||
});
|
||||
|
||||
describe('skipedOutOfEvent()', () => {
|
||||
it('without added times, they combine to be duration', () => {
|
||||
const startedAt = 1000;
|
||||
const duration = 1000;
|
||||
const pausedTime = 0;
|
||||
const finishedAt = null;
|
||||
const addedTime = 0;
|
||||
const timerType = TimerType.CountDown;
|
||||
const expectedFinish = startedAt + duration;
|
||||
const previousTime = 1000;
|
||||
let clock = previousTime;
|
||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||
clock += config.timeSkipLimit + 10;
|
||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||
clock = expectedFinish + 1;
|
||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
||||
clock = startedAt - config.timeSkipLimit - 1;
|
||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { OntimeEvent } from 'ontime-types';
|
||||
import { dayInMs } from 'ontime-utils';
|
||||
import { config } from '../config/config.js';
|
||||
|
||||
/**
|
||||
* handle events that span over midnight
|
||||
@@ -154,7 +153,6 @@ type CurrentTimers = {
|
||||
selectedEventId: string | null;
|
||||
current: number | null;
|
||||
_finishAt: number | null;
|
||||
_startAt: number | null;
|
||||
clock: number | null;
|
||||
secondaryTimer: number | null;
|
||||
secondaryTarget: number | null;
|
||||
@@ -166,7 +164,7 @@ type CurrentTimers = {
|
||||
* @returns {object} object with selection variables
|
||||
*/
|
||||
export const updateRoll = (currentTimers: CurrentTimers) => {
|
||||
const { selectedEventId, current, _finishAt, _startAt, clock, secondaryTimer, secondaryTarget } = currentTimers;
|
||||
const { selectedEventId, current, _finishAt, clock, secondaryTimer, secondaryTarget } = currentTimers;
|
||||
|
||||
// timers
|
||||
let updatedTimer = current;
|
||||
@@ -185,15 +183,9 @@ export const updateRoll = (currentTimers: CurrentTimers) => {
|
||||
}
|
||||
|
||||
if (updatedTimer < 0) {
|
||||
const hasSkipped = Math.abs(updatedTimer) > config.rollSkipLimit;
|
||||
if (!hasSkipped) {
|
||||
isPrimaryFinished = true;
|
||||
}
|
||||
isPrimaryFinished = true;
|
||||
// we need a new event
|
||||
doRollLoad = true;
|
||||
} else if (clock < _startAt) {
|
||||
// we have rolled back befor this evet start so we need a new one
|
||||
doRollLoad = true;
|
||||
}
|
||||
} else if (secondaryTimer >= 0) {
|
||||
// if secondaryTimer is running we are in waiting to roll
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { MaybeNumber, TimerType } from 'ontime-types';
|
||||
import { dayInMs } from 'ontime-utils';
|
||||
import { config } from '../config/config.js';
|
||||
|
||||
/**
|
||||
* Calculates expected finish time of a running timer
|
||||
@@ -64,3 +65,15 @@ export function getCurrent(
|
||||
}
|
||||
return startedAt + duration + addedTime + pausedTime - clock;
|
||||
}
|
||||
|
||||
export function skipedOutOfEvent(previousTime: number, clock: number, startedAt: number, finishAt): boolean {
|
||||
const skipTime = previousTime - clock;
|
||||
const hasSkipped = Math.abs(skipTime) > config.timeSkipLimit;
|
||||
if (hasSkipped) {
|
||||
if (clock > finishAt || clock < startedAt) {
|
||||
return true;
|
||||
}
|
||||
// otherwise we just skipped within the event
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user