Relative run mode (#1512)

* inti relative mode

* send imediat update on mode change

* add relative to test

* don't persist offset mode

* add test relative and update calc function

* pass data from ssocket

* add dev guard

* spelling and comments

* show relative in offset overview

* remove comments

* refactor: move totalDelay to _rundown

* update  clear naming and comments

* remove old testing values
This commit is contained in:
Alex Christoffer Rasmussen
2025-04-19 21:36:54 +02:00
committed by GitHub
parent a94b10cb0f
commit dce87c3a81
17 changed files with 543 additions and 114 deletions
@@ -4,6 +4,7 @@ import { EndAction, Playback, TimeStrategy, TimerPhase, TimerType } from 'ontime
import {
getCurrent,
getExpectedFinish,
getRelativeOffset,
getRuntimeOffset,
getTimerPhase,
normaliseEndTime,
@@ -1046,6 +1047,84 @@ describe('getRuntimeOffset()', () => {
});
});
describe('getRelativeOffset()', () => {
it('relative offset is 0 when starting at the planed time', () => {
const state = {
eventNow: {
id: '1',
timeStart: 150,
},
timer: {
startedAt: 150,
addedTime: 0,
current: 0,
},
_timer: {
pausedAt: null,
},
runtime: {
actualStart: 150,
plannedStart: 150,
},
} as RuntimeState;
state.runtime.offset = getRuntimeOffset(state);
expect(state.runtime.offset).toBe(0);
const relativeOffsetoffset = getRelativeOffset(state);
expect(relativeOffsetoffset).toBe(0);
});
it('relative offset is 0 when starting after the planed time', () => {
const state = {
eventNow: {
id: '1',
timeStart: 100,
},
timer: {
startedAt: 150,
addedTime: 0,
current: 0,
},
_timer: {
pausedAt: null,
},
runtime: {
actualStart: 150,
plannedStart: 100,
},
} as RuntimeState;
state.runtime.offset = getRuntimeOffset(state);
expect(state.runtime.offset).toBe(-50);
const relativeOffsetoffset = getRelativeOffset(state);
expect(relativeOffsetoffset).toBe(0);
});
it('relative offset is 0 when starting before the planed time', () => {
const state = {
eventNow: {
id: '1',
timeStart: 150,
},
timer: {
startedAt: 100,
addedTime: 0,
current: 0,
},
_timer: {
pausedAt: null,
},
runtime: {
actualStart: 100,
plannedStart: 150,
},
} as RuntimeState;
state.runtime.offset = getRuntimeOffset(state);
expect(state.runtime.offset).toBe(50);
const relativeOffsetoffset = getRelativeOffset(state);
expect(relativeOffsetoffset).toBe(0);
});
});
describe('getTimerPhase()', () => {
it('should be None if the timer is not running', () => {
const state = {
@@ -1169,9 +1248,11 @@ describe('getTimerPhase()', () => {
},
_timer: {
forceFinish: null,
totalDelay: 0,
pausedAt: null,
},
_rundown: {
totalDelay: 0,
},
} as RuntimeState;
const phase = getTimerPhase(state);
@@ -1208,9 +1289,11 @@ describe('getTimerPhase()', () => {
},
_timer: {
forceFinish: null,
totalDelay: 0,
pausedAt: null,
},
_rundown: {
totalDelay: 0,
},
} as RuntimeState;
const phase = getTimerPhase(state);
@@ -4,6 +4,7 @@ import {
isPlayableEvent,
LogOrigin,
MaybeNumber,
OffsetMode,
OntimeEvent,
Playback,
TimerLifeCycle,
@@ -68,6 +69,11 @@ class RuntimeService {
RuntimeService.previousState = {} as RuntimeState;
}
@broadcastResult
setOffsetMode(mode: OffsetMode) {
runtimeState.setOffsetMode(mode);
}
/**
* Checks result of an update and notifies integrations as needed
* This is the only exception of a private method that has broadcast result
@@ -697,11 +703,14 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
// for the very fist run there will be nothing in the previousState so we force an update
const justStarted = !RuntimeService.previousState?.timer;
// offset mode has been changed
const offsetModeChanged = RuntimeService.previousState?.runtime?.offsetMode !== state.runtime.offsetMode;
// if playback changes most things should update
const hasChangedPlayback = RuntimeService.previousState.timer?.playback !== state.timer.playback;
// combine all big changes
const hasImmediateChanges = hasNewLoaded || justStarted || hasChangedPlayback;
const hasImmediateChanges = hasNewLoaded || justStarted || hasChangedPlayback || offsetModeChanged;
/**
* Timer should be updated if
+18 -1
View File
@@ -156,6 +156,23 @@ export function getRuntimeOffset(state: RuntimeState): number {
return startOffset - addedTime - pausedTime + overtime;
}
/**
* Calculates relative offset
* should always be calculated after the absolute offset
*/
export function getRelativeOffset(state: RuntimeState): number {
const { actualStart, plannedStart, offset } = state.runtime;
// eslint-disable-next-line no-unused-labels -- dev code path
DEV: {
// we know actualStart and plannedStart exists as long as a timer is running
if (actualStart === null || plannedStart === null) {
throw new Error('timerUtils.calculate: actualStart and plannedStart must be set');
}
}
const relativeStartOffset = actualStart - plannedStart;
return offset + relativeStartOffset;
}
/**
* Calculates the expected end of the rundown
*/
@@ -164,7 +181,7 @@ export function getExpectedEnd(state: RuntimeState): MaybeNumber {
if (state.runtime.actualStart === null || state.runtime.plannedEnd === null) {
return null;
}
return state.runtime.plannedEnd - state.runtime.offset + state._timer.totalDelay;
return state.runtime.plannedEnd - state.runtime.offset + state._rundown.totalDelay;
}
/**