Refactor: better rounding (#1594)

This commit is contained in:
Alex Christoffer Rasmussen
2025-06-05 06:13:49 +02:00
committed by Carlos Valente
parent c1054711b0
commit b9ab1c6fd7
10 changed files with 93 additions and 181 deletions
@@ -717,6 +717,11 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
// combine all big changes
const hasImmediateChanges = hasNewLoaded || justStarted || hasChangedPlayback || offsetModeChanged;
// we would like the wall clock to tick on a regular rate
const normalClockUpdate =
getShouldClockUpdate(RuntimeService.previousClockUpdate, state.clock) ||
getForceUpdate(RuntimeService.previousClockUpdate, state.clock);
/**
* Timer should be updated if
* - big changes
@@ -733,6 +738,7 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
/**
* Runtime should be updated if
* - clock tick
* - big changes
* - the timer is updating so runtime also updates to keep them in sync ???
* - notification rate has been exceeded
@@ -740,7 +746,10 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
* Then check if there is actually a change in the data
*/
const shouldRuntimeUpdate =
(hasImmediateChanges || shouldUpdateTimer || getForceUpdate(RuntimeService.previousRuntimeUpdate, state.clock)) &&
(normalClockUpdate ||
hasImmediateChanges ||
shouldUpdateTimer ||
getForceUpdate(RuntimeService.previousRuntimeUpdate, state.clock)) &&
!deepEqual(RuntimeService.previousState?.runtime, state.runtime);
/**
@@ -751,13 +760,9 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
/**
* Many other values are calculated based on the clock
* so if any of them are updated we also need to send the clock
* in case nothing else is updating the clock will bw updated at the notification rate
* in case nothing else is updating the clock will be updated at the notification rate
*/
const shouldUpdateClock =
shouldUpdateTimer ||
shouldRuntimeUpdate ||
shouldBlockUpdate ||
getForceUpdate(RuntimeService.previousClockUpdate, state.clock);
const shouldUpdateClock = shouldRuntimeUpdate || shouldBlockUpdate || normalClockUpdate;
//Now we set all the updates on the eventstore and update the previous value
if (hasChangedPlayback) {
@@ -828,15 +833,19 @@ function broadcastResult(_target: any, _propertyKey: string, descriptor: Propert
// Helper function to save the restore state
function saveRestoreState(state: runtimeState.RuntimeState) {
restoreService.save({
playback: state.timer.playback,
selectedEventId: state.eventNow?.id ?? null,
startedAt: state.timer.startedAt,
addedTime: state.timer.addedTime,
pausedAt: state._timer.pausedAt,
firstStart: state.runtime.actualStart,
blockStartAt: state.currentBlock.startedAt,
});
restoreService
.save({
playback: state.timer.playback,
selectedEventId: state.eventNow?.id ?? null,
startedAt: state.timer.startedAt,
addedTime: state.timer.addedTime,
pausedAt: state._timer.pausedAt,
firstStart: state.runtime.actualStart,
blockStartAt: state.currentBlock.startedAt,
})
.catch((_e) => {
//we don't do anything with the error here
});
}
batch.send();
@@ -1,20 +1,16 @@
import { millisToSeconds } from 'ontime-utils';
import { MaybeNumber } from 'ontime-types';
import { MaybeNumber, TimerType } from 'ontime-types';
import { timerConfig } from '../../setup/config.js';
/**
* Checks whether we should update the clock value
* - clock has slid
* - we have rolled into a new seconds unit
* this is different from the timer update as it looks at the clock as counting up
*/
export function getShouldClockUpdate(previousUpdate: number, now: number): boolean {
const shouldForceUpdate = getForceUpdate(previousUpdate, now);
if (shouldForceUpdate) {
return true;
}
const isClockSecondAhead = millisToSeconds(now) !== millisToSeconds(previousUpdate + timerConfig.triggerAhead);
return isClockSecondAhead;
const newSeconds = millisToSeconds(now, TimerType.CountUp) !== millisToSeconds(previousUpdate, TimerType.CountUp);
return newSeconds;
}
/**
@@ -22,10 +18,6 @@ export function getShouldClockUpdate(previousUpdate: number, now: number): boole
* - we have rolled into a new seconds unit
*/
export function getShouldTimerUpdate(previousValue: MaybeNumber, currentValue: MaybeNumber): boolean {
if (currentValue === null) {
return false;
}
// we avoid trigger ahead since it can cause duplicate triggers
const shouldUpdateTimer = millisToSeconds(currentValue) !== millisToSeconds(previousValue);
return shouldUpdateTimer;
}
@@ -39,6 +31,5 @@ export function getShouldTimerUpdate(previousValue: MaybeNumber, currentValue: M
export function getForceUpdate(previousUpdate: number, now: number): boolean {
const isClockBehind = now < previousUpdate;
const hasExceededRate = now - previousUpdate >= timerConfig.notificationRate;
const newSeconds = millisToSeconds(previousUpdate) !== millisToSeconds(now);
return isClockBehind || hasExceededRate || newSeconds;
return isClockBehind || hasExceededRate;
}