refactor: invert offset

This commit is contained in:
Carlos Valente
2024-04-26 14:58:58 +02:00
committed by Carlos Valente
parent 8b5034efbf
commit 8da8a08464
5 changed files with 34 additions and 72 deletions
@@ -116,7 +116,7 @@ function RuntimeOverview() {
const { clock, offset } = useRuntimePlaybackOverview(); const { clock, offset } = useRuntimePlaybackOverview();
const offsetText = getOffsetText(offset); const offsetText = getOffsetText(offset);
const offsetClasses = offset === null ? undefined : offset > 0 ? style.behind : style.ahead; const offsetClasses = offset === null ? undefined : offset <= 0 ? style.behind : style.ahead;
return ( return (
<> <>
@@ -38,10 +38,5 @@ export function getOffsetText(offset: MaybeNumber): string {
if (offset === null) { if (offset === null) {
return enDash; return enDash;
} }
const isAhead = offset <= 0; return millisToString(offset, { fallback: enDash });
let offsetText = millisToString(Math.abs(offset), { fallback: enDash });
if (offsetText !== enDash) {
offsetText = isAhead ? `+${offsetText}` : `${enDash}${offsetText}`;
}
return offsetText;
} }
@@ -1421,19 +1421,19 @@ describe('getRuntimeOffset()', () => {
} as RuntimeState; } as RuntimeState;
const offset = getRuntimeOffset(state); const offset = getRuntimeOffset(state);
expect(offset).toBe(50); expect(offset).toBe(-50);
}); });
it('added time adds time ahead (negative offset)', () => { it('added time adds time ahead (positive offset)', () => {
const state = { const state = {
eventNow: { eventNow: {
id: '1', id: '1',
timeStart: 100, timeStart: 100,
}, },
timer: { timer: {
startedAt: 150, startedAt: 150, // we started 50ms delayed
addedTime: 10, addedTime: 10, // we compensated with 10ms
current: 10, current: 10, // we are 10ms into the timer
}, },
_timer: { _timer: {
pausedAt: null, pausedAt: null,
@@ -1444,10 +1444,10 @@ describe('getRuntimeOffset()', () => {
} as RuntimeState; } as RuntimeState;
const offset = getRuntimeOffset(state); const offset = getRuntimeOffset(state);
expect(offset).toBe(40); expect(offset).toBe(-40);
}); });
it('considers running overtime (positive offset)', () => { it('considers running overtime (negative offset)', () => {
const state = { const state = {
eventNow: { eventNow: {
id: '1', id: '1',
@@ -1468,10 +1468,10 @@ describe('getRuntimeOffset()', () => {
} as RuntimeState; } as RuntimeState;
const offset = getRuntimeOffset(state); const offset = getRuntimeOffset(state);
expect(offset).toBe(10); expect(offset).toBe(-10);
}); });
it('accounts for paused time', () => { it('paused time is delayed time (negative offset)', () => {
const state = { const state = {
eventNow: { eventNow: {
id: '1', id: '1',
@@ -1480,12 +1480,12 @@ describe('getRuntimeOffset()', () => {
}, },
clock: 150, clock: 150,
timer: { timer: {
startedAt: 100, startedAt: 100, // started on time
current: 25, current: 25, // are 25ms into it
addedTime: 0, addedTime: 0,
}, },
_timer: { _timer: {
pausedAt: 125, pausedAt: 125, // we have been paused for 25ms (see clock)
}, },
runtime: { runtime: {
actualStart: 100, actualStart: 100,
@@ -1493,33 +1493,19 @@ describe('getRuntimeOffset()', () => {
} as RuntimeState; } as RuntimeState;
const offset = getRuntimeOffset(state); const offset = getRuntimeOffset(state);
expect(offset).toBe(25); expect(offset).toBe(-25);
}); });
it('can only count once started', () => { it('offset doesnt exist if we havent started', () => {
const state = { const state = {
clock: 78480789, clock: 78480789,
eventNow: { eventNow: {
id: 'd6a2ce', id: 'd6a2ce',
type: 'event',
title: '',
timeStart: 77400000, timeStart: 77400000,
timeEnd: 81000000, timeEnd: 81000000,
duration: 3600000, duration: 3600000,
timeStrategy: 'lock-duration', timeStrategy: 'lock-duration',
linkStart: null, linkStart: null,
endAction: 'none',
timerType: 'count-down',
isPublic: true,
skip: false,
note: '',
colour: '',
cue: '1',
revision: 0,
timeWarning: 120000,
timeDanger: 60000,
custom: {},
delay: 0,
}, },
runtime: { runtime: {
selectedEventIndex: 0, selectedEventIndex: 0,
@@ -1553,8 +1539,6 @@ describe('getRuntimeOffset()', () => {
clock: 79521653, clock: 79521653,
eventNow: { eventNow: {
id: '835242', id: '835242',
type: 'event',
title: '',
timeStart: 81000000, timeStart: 81000000,
timeEnd: 84600000, timeEnd: 84600000,
duration: 3600000, duration: 3600000,
@@ -1562,15 +1546,6 @@ describe('getRuntimeOffset()', () => {
linkStart: null, linkStart: null,
endAction: 'none', endAction: 'none',
timerType: 'count-down', timerType: 'count-down',
isPublic: true,
skip: false,
note: '',
colour: '',
cue: '2',
revision: 0,
timeWarning: 120000,
timeDanger: 60000,
custom: {},
delay: 0, delay: 0,
}, },
runtime: { runtime: {
@@ -1597,10 +1572,10 @@ describe('getRuntimeOffset()', () => {
} as RuntimeState; } as RuntimeState;
const offset = getRuntimeOffset(state); const offset = getRuntimeOffset(state);
expect(offset).toBe(79521653 - 81000000); expect(offset).toBe(81000000 - 79521653); // clock - timestart
}); });
it('handles time-to-end', () => { it('with time-to-end, offsets dont exist if we are not in overtime', () => {
const state = { const state = {
clock: 80000000, // 22:13:20 clock: 80000000, // 22:13:20
eventNow: { eventNow: {
@@ -1652,7 +1627,7 @@ describe('getRuntimeOffset()', () => {
expect(offset).toBe(0); expect(offset).toBe(0);
}); });
it('handles time-to-end with delays', () => { it('with time-to-end, offset is the overtime', () => {
const state = { const state = {
clock: 82000000, // 22:46:40 clock: 82000000, // 22:46:40
eventNow: { eventNow: {
@@ -1709,25 +1684,13 @@ describe('getRuntimeOffset()', () => {
clock: 82000000, // 22:46:40 <--- starting 16 min after the scheduled end clock: 82000000, // 22:46:40 <--- starting 16 min after the scheduled end
eventNow: { eventNow: {
id: 'd6a2ce', id: 'd6a2ce',
type: 'event',
title: '',
timeStart: 77400000, // 21:30:00 timeStart: 77400000, // 21:30:00
timeEnd: 81000000, // 22:30:00 timeEnd: 81000000, // 22:30:00
duration: 3600000, // 01:00:00 duration: 3600000, // 01:00:00
timeStrategy: TimeStrategy.LockEnd, timeStrategy: TimeStrategy.LockEnd,
linkStart: null, linkStart: null,
endAction: EndAction.None, endAction: EndAction.None,
timerType: TimerType.TimeToEnd, timerType: TimerType.TimeToEnd, // <--- but this is time to end
isPublic: true,
skip: false,
note: '',
colour: '',
cue: '1',
revision: 0,
timeWarning: 120000,
timeDanger: 60000,
custom: {},
delay: 0,
}, },
runtime: { runtime: {
selectedEventIndex: 0, selectedEventIndex: 0,
+12 -8
View File
@@ -293,12 +293,11 @@ export const updateRoll = (state: RuntimeState) => {
/** /**
* Calculates difference between the runtime and the schedule of an event * Calculates difference between the runtime and the schedule of an event
* Positive offset is a delay * Positive offset is time ahead
* Negative offset is time ahead * Negative offset is time delayed
* @param state
* @returns
*/ */
export function getRuntimeOffset(state: RuntimeState): MaybeNumber { export function getRuntimeOffset(state: RuntimeState): MaybeNumber {
// nothing to calculate if there are no loaded events or if we havent started
if (state.eventNow === null || state.runtime.actualStart === null) { if (state.eventNow === null || state.runtime.actualStart === null) {
return null; return null;
} }
@@ -307,9 +306,10 @@ export function getRuntimeOffset(state: RuntimeState): MaybeNumber {
const { timeStart, timerType } = state.eventNow; const { timeStart, timerType } = state.eventNow;
const { addedTime, current, startedAt } = state.timer; const { addedTime, current, startedAt } = state.timer;
// if we havent started, the offset is the difference to the schedule // if we havent started, but the timer is armed
// the offset is the difference to the schedule
if (startedAt === null) { if (startedAt === null) {
return clock - timeStart; return timeStart - clock;
} }
const overtime = Math.abs(Math.min(current, 0)); const overtime = Math.abs(Math.min(current, 0));
@@ -318,10 +318,14 @@ export function getRuntimeOffset(state: RuntimeState): MaybeNumber {
return overtime; return overtime;
} }
const startOffset = startedAt - timeStart; const startOffset = timeStart - startedAt;
const pausedTime = state._timer.pausedAt === null ? 0 : clock - state._timer.pausedAt; const pausedTime = state._timer.pausedAt === null ? 0 : clock - state._timer.pausedAt;
return startOffset - addedTime + pausedTime + overtime; // startOffset - difference between scheduled start and actual start
// addedTime - time added by user (positive offset)
// pausedTime - time the playback was paused (negative offset)
// overtime - how long the timer has been over-running (negative offset)
return startOffset + addedTime - pausedTime - overtime;
} }
/** /**
@@ -161,7 +161,7 @@ describe('mutation on runtimeState', () => {
newState = getState(); newState = getState();
const firstStart = newState.clock; const firstStart = newState.clock;
expect(newState.runtime.actualStart).toBe(newState.clock); expect(newState.runtime.actualStart).toBe(newState.clock);
expect(newState.runtime.offset).toBe(newState.clock - event1.timeStart); expect(newState.runtime.offset).toBe(event1.timeStart - newState.clock);
expect(newState.runtime.expectedEnd).toBe(newState.runtime.offset + event2.timeEnd); expect(newState.runtime.expectedEnd).toBe(newState.runtime.offset + event2.timeEnd);
// 3. Next event // 3. Next event
@@ -170,7 +170,7 @@ describe('mutation on runtimeState', () => {
newState = getState(); newState = getState();
expect(newState.runtime.actualStart).toBe(firstStart); expect(newState.runtime.actualStart).toBe(firstStart);
// we are over-under, the difference between the schedule and the actual start // we are over-under, the difference between the schedule and the actual start
const delayBefore = newState.clock - event2.timeStart; const delayBefore = event2.timeStart - newState.clock;
expect(newState.runtime.offset).toBe(delayBefore); expect(newState.runtime.offset).toBe(delayBefore);
// finish is the difference between the runtime and the schedule // finish is the difference between the runtime and the schedule
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset); expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset);