mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 23:19:09 +00:00
move skip logic to timerUtils
This commit is contained in:
@@ -9,5 +9,5 @@ export const config = {
|
|||||||
filename: 'override.css',
|
filename: 'override.css',
|
||||||
},
|
},
|
||||||
restoreFile: 'ontime.restore',
|
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 { 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, getExpectedFinish } from './timerUtils.js';
|
import { getCurrent, getExpectedFinish, skipedOutOfEvent } 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';
|
||||||
import type { RestorePoint } from './RestoreService.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.startedAt
|
||||||
? this.timer.expectedFinish
|
? this.timer.expectedFinish
|
||||||
: this.timer.expectedFinish + dayInMs,
|
: this.timer.expectedFinish + dayInMs,
|
||||||
_startAt: this.timer.startedAt,
|
|
||||||
clock: this.timer.clock,
|
clock: this.timer.clock,
|
||||||
secondaryTimer: this.timer.secondaryTimer,
|
secondaryTimer: this.timer.secondaryTimer,
|
||||||
secondaryTarget: this.secondaryTarget,
|
secondaryTarget: this.secondaryTarget,
|
||||||
@@ -405,7 +404,11 @@ export class TimerService {
|
|||||||
let shouldNotify = false;
|
let shouldNotify = false;
|
||||||
if (this.playback === Playback.Roll) {
|
if (this.playback === Playback.Roll) {
|
||||||
shouldNotify = true;
|
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) {
|
} else if (this.timer.startedAt !== null) {
|
||||||
// we only update timer if a timer has been started
|
// we only update timer if a timer has been started
|
||||||
shouldNotify = true;
|
shouldNotify = true;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { OntimeEvent } from 'ontime-types';
|
|||||||
import { dayInMs } from 'ontime-utils';
|
import { dayInMs } from 'ontime-utils';
|
||||||
|
|
||||||
import { getRollTimers, normaliseEndTime, sortArrayByProperty, updateRoll } from '../rollUtils.js';
|
import { getRollTimers, normaliseEndTime, sortArrayByProperty, updateRoll } from '../rollUtils.js';
|
||||||
import { config } from '../../config/config.js';
|
|
||||||
|
|
||||||
// test sortArrayByProperty()
|
// test sortArrayByProperty()
|
||||||
describe('sort simple arrays of objects', () => {
|
describe('sort simple arrays of objects', () => {
|
||||||
@@ -563,7 +562,6 @@ describe('typical scenarios', () => {
|
|||||||
selectedEventId: '1',
|
selectedEventId: '1',
|
||||||
current: 10,
|
current: 10,
|
||||||
_finishAt: 15,
|
_finishAt: 15,
|
||||||
_startAt: 9,
|
|
||||||
clock: 11,
|
clock: 11,
|
||||||
secondaryTimer: null,
|
secondaryTimer: null,
|
||||||
secondaryTarget: null,
|
secondaryTarget: null,
|
||||||
@@ -586,72 +584,11 @@ describe('typical scenarios', () => {
|
|||||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
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', () => {
|
it('it updates secondary timer', () => {
|
||||||
const timers = {
|
const timers = {
|
||||||
selectedEventId: null,
|
selectedEventId: null,
|
||||||
current: null,
|
current: null,
|
||||||
_finishAt: null,
|
_finishAt: null,
|
||||||
_startAt: null,
|
|
||||||
clock: 11,
|
clock: 11,
|
||||||
secondaryTimer: 1,
|
secondaryTimer: 1,
|
||||||
secondaryTarget: 15,
|
secondaryTarget: 15,
|
||||||
@@ -672,7 +609,6 @@ describe('typical scenarios', () => {
|
|||||||
selectedEventId: '1',
|
selectedEventId: '1',
|
||||||
current: 10,
|
current: 10,
|
||||||
_finishAt: 11,
|
_finishAt: 11,
|
||||||
_startAt: 9,
|
|
||||||
clock: 12,
|
clock: 12,
|
||||||
secondaryTimer: null,
|
secondaryTimer: null,
|
||||||
secondaryTarget: null,
|
secondaryTarget: null,
|
||||||
@@ -693,7 +629,6 @@ describe('typical scenarios', () => {
|
|||||||
selectedEventId: null,
|
selectedEventId: null,
|
||||||
current: null,
|
current: null,
|
||||||
_finishAt: null,
|
_finishAt: null,
|
||||||
_startAt: null,
|
|
||||||
clock: 16,
|
clock: 16,
|
||||||
secondaryTimer: 1,
|
secondaryTimer: 1,
|
||||||
secondaryTarget: 15,
|
secondaryTarget: 15,
|
||||||
@@ -714,7 +649,6 @@ describe('typical scenarios', () => {
|
|||||||
selectedEventId: null,
|
selectedEventId: null,
|
||||||
current: null,
|
current: null,
|
||||||
_finishAt: null,
|
_finishAt: null,
|
||||||
_startAt: null,
|
|
||||||
clock: 15,
|
clock: 15,
|
||||||
secondaryTimer: 0,
|
secondaryTimer: 0,
|
||||||
secondaryTarget: 15,
|
secondaryTarget: 15,
|
||||||
@@ -735,7 +669,6 @@ describe('typical scenarios', () => {
|
|||||||
selectedEventId: '1',
|
selectedEventId: '1',
|
||||||
current: 25,
|
current: 25,
|
||||||
_finishAt: 10 + dayInMs,
|
_finishAt: 10 + dayInMs,
|
||||||
_startAt: 10,
|
|
||||||
clock: dayInMs - 10,
|
clock: dayInMs - 10,
|
||||||
secondaryTimer: null,
|
secondaryTimer: null,
|
||||||
secondaryTarget: null,
|
secondaryTarget: null,
|
||||||
@@ -756,7 +689,6 @@ describe('typical scenarios', () => {
|
|||||||
selectedEventId: '1',
|
selectedEventId: '1',
|
||||||
current: dayInMs,
|
current: dayInMs,
|
||||||
_finishAt: 10 + dayInMs,
|
_finishAt: 10 + dayInMs,
|
||||||
_startAt: 10,
|
|
||||||
clock: 10,
|
clock: 10,
|
||||||
secondaryTimer: null,
|
secondaryTimer: null,
|
||||||
secondaryTarget: null,
|
secondaryTarget: null,
|
||||||
@@ -771,24 +703,4 @@ describe('typical scenarios', () => {
|
|||||||
|
|
||||||
expect(updateRoll(timers)).toStrictEqual(expected);
|
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 { dayInMs } from 'ontime-utils';
|
||||||
import { TimerType } from 'ontime-types';
|
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()', () => {
|
describe('getExpectedFinish()', () => {
|
||||||
it('is null if we havent started', () => {
|
it('is null if we havent started', () => {
|
||||||
@@ -354,3 +355,24 @@ describe('getExpectedFinish() and getCurrentTime() combined', () => {
|
|||||||
expect(current).toBe(8);
|
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 { OntimeEvent } from 'ontime-types';
|
||||||
import { dayInMs } from 'ontime-utils';
|
import { dayInMs } from 'ontime-utils';
|
||||||
import { config } from '../config/config.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handle events that span over midnight
|
* handle events that span over midnight
|
||||||
@@ -154,7 +153,6 @@ type CurrentTimers = {
|
|||||||
selectedEventId: string | null;
|
selectedEventId: string | null;
|
||||||
current: number | null;
|
current: number | null;
|
||||||
_finishAt: number | null;
|
_finishAt: number | null;
|
||||||
_startAt: number | null;
|
|
||||||
clock: number | null;
|
clock: number | null;
|
||||||
secondaryTimer: number | null;
|
secondaryTimer: number | null;
|
||||||
secondaryTarget: number | null;
|
secondaryTarget: number | null;
|
||||||
@@ -166,7 +164,7 @@ type CurrentTimers = {
|
|||||||
* @returns {object} object with selection variables
|
* @returns {object} object with selection variables
|
||||||
*/
|
*/
|
||||||
export const updateRoll = (currentTimers: CurrentTimers) => {
|
export const updateRoll = (currentTimers: CurrentTimers) => {
|
||||||
const { selectedEventId, current, _finishAt, _startAt, clock, secondaryTimer, secondaryTarget } = currentTimers;
|
const { selectedEventId, current, _finishAt, clock, secondaryTimer, secondaryTarget } = currentTimers;
|
||||||
|
|
||||||
// timers
|
// timers
|
||||||
let updatedTimer = current;
|
let updatedTimer = current;
|
||||||
@@ -185,15 +183,9 @@ export const updateRoll = (currentTimers: CurrentTimers) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (updatedTimer < 0) {
|
if (updatedTimer < 0) {
|
||||||
const hasSkipped = Math.abs(updatedTimer) > config.rollSkipLimit;
|
isPrimaryFinished = true;
|
||||||
if (!hasSkipped) {
|
|
||||||
isPrimaryFinished = true;
|
|
||||||
}
|
|
||||||
// we need a new event
|
// we need a new event
|
||||||
doRollLoad = true;
|
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) {
|
} else if (secondaryTimer >= 0) {
|
||||||
// if secondaryTimer is running we are in waiting to roll
|
// if secondaryTimer is running we are in waiting to roll
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { MaybeNumber, TimerType } from 'ontime-types';
|
import { MaybeNumber, TimerType } from 'ontime-types';
|
||||||
import { dayInMs } from 'ontime-utils';
|
import { dayInMs } from 'ontime-utils';
|
||||||
|
import { config } from '../config/config.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates expected finish time of a running timer
|
* Calculates expected finish time of a running timer
|
||||||
@@ -64,3 +65,15 @@ export function getCurrent(
|
|||||||
}
|
}
|
||||||
return startedAt + duration + addedTime + pausedTime - clock;
|
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