Skip to content

Commit

Permalink
specific rounding for UI updating
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-Arc committed Jan 24, 2025
1 parent a1d34c8 commit eff9f1d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { millisToSeconds } from 'ontime-utils';
import { millisToUISeconds } from 'ontime-utils';

import { timerConfig } from '../../config/config.js';
import { MaybeNumber, Playback } from 'ontime-types';
Expand All @@ -13,7 +13,7 @@ export function getShouldClockUpdate(previousUpdate: number, now: number): boole
if (shouldForceUpdate) {
return true;
}
const isClockSecondAhead = millisToSeconds(now) !== millisToSeconds(previousUpdate + timerConfig.triggerAhead);
const isClockSecondAhead = millisToUISeconds(now) !== millisToUISeconds(previousUpdate + timerConfig.triggerAhead);
return isClockSecondAhead;
}

Expand All @@ -26,7 +26,7 @@ export function getShouldTimerUpdate(previousValue: number, currentValue: MaybeN
return false;
}
// we avoid trigger ahead since it can cause duplicate triggers
const shouldUpdateTimer = millisToSeconds(currentValue) !== millisToSeconds(previousValue);
const shouldUpdateTimer = millisToUISeconds(currentValue) !== millisToUISeconds(previousValue);
return shouldUpdateTimer;
}

Expand Down
1 change: 1 addition & 0 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
millisToHours,
millisToMinutes,
millisToSeconds,
millisToUISeconds,
secondsInMillis,
} from './src/date-utils/conversionUtils.js';
export { isISO8601, isTimeString } from './src/date-utils/isTimeString.js';
Expand Down
50 changes: 49 additions & 1 deletion packages/utils/src/date-utils/conversionUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
import { millisToHours, millisToMinutes, millisToSeconds, secondsInMillis } from './conversionUtils';
import { millisToHours, millisToMinutes, millisToSeconds, millisToUISeconds, secondsInMillis } from './conversionUtils';

describe('millisToUITimer()', () => {
test('null values', () => {
const t = { val: null, result: 0 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});
test('1 sec', () => {
const t = { val: 1000, result: 1 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});
test('alsmot 1 sec', () => {
const t = { val: 1032, result: 1 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});
test('just past 1 sec', () => {
const t = { val: 1000 - 32, result: 1 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});
test('half way 1 sec', () => {
const t = { val: 500, result: 1 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});

test('zero', () => {
const t = { val: 32, result: 0 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});

test('zero', () => {
const t = { val: 0, result: 0 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});

test('negative', () => {
const t = { val: -32, result: 0 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});

test('negative', () => {
const t = { val: -1000 + 33, result: 0 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});

test('negative', () => {
const t = { val: -1000 + 31, result: -1 };
expect(millisToUISeconds(t.val)).toBe(t.result);
});
});

describe('millisToSecond()', () => {
test('null values', () => {
Expand Down
27 changes: 27 additions & 0 deletions packages/utils/src/date-utils/conversionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ export function millisToSeconds(millis: MaybeNumber): number {
return convertMillis(millis, MILLIS_PER_SECOND);
}

export function millisToUISeconds(millis: MaybeNumber): number {
if (millis === null) {
return 0;
}

const isNegative = millis < 0;
const val = Math.abs(millis);

const remainder = val % MILLIS_PER_SECOND;

if (isNegative) {
if (remainder <= 1000 - 32) {
const ret = Math.ceil(millis / MILLIS_PER_SECOND);
// eslint-disable-next-line no-compare-neg-zero
return ret === -0 ? 0 : ret;
}

return Math.floor(millis / MILLIS_PER_SECOND);
}

if (remainder <= 32) {
return Math.floor(val / MILLIS_PER_SECOND);
}

return Math.ceil(val / MILLIS_PER_SECOND);
}

/**
* Converts value in milliseconds to minutes
* @param millis
Expand Down

0 comments on commit eff9f1d

Please sign in to comment.