Skip to content

Commit a86a725

Browse files
committed
timer: normalize timeout for any ETimer type
Signed-off-by: 🕷️ <[email protected]>
1 parent 6d3cb2a commit a86a725

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

src/api-monitor-cs-main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import diff from './api/diff.ts';
1515
let originalMetrics: TTelemetry | null;
1616
let currentMetrics: TTelemetry | null;
1717
const eachSecond = new Timer(
18-
{ type: ETimer.TIMEOUT, delay: 1e3 },
18+
{ type: ETimer.TIMEOUT, timeout: 1e3 },
1919
function apiMonitorEachSecond() {
2020
onEachSecond();
2121
eachSecond.start();
@@ -24,7 +24,7 @@ const eachSecond = new Timer(
2424
const tick = new Timer({
2525
type: ETimer.TASK,
2626
priority: 'background',
27-
delay: TELEMETRY_FREQUENCY_1PS,
27+
timeout: TELEMETRY_FREQUENCY_1PS,
2828
}, function apiMonitorTelemetryTick() {
2929
const now = Date.now();
3030
currentMetrics = structuredClone(collectMetrics());
@@ -54,7 +54,7 @@ const tick = new Timer({
5454

5555
windowListen((o) => {
5656
if (EMsg.TELEMETRY_ACKNOWLEDGED === o.msg) {
57-
tick.delay = adjustTelemetryDelay(o.timeOfCollection);
57+
tick.timeout = adjustTelemetryDelay(o.timeOfCollection);
5858
originalMetrics = currentMetrics;
5959
const shouldRun = eachSecond.isPending() && !tick.isPending();
6060
shouldRun && tick.start();

src/api/time.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type TTimerMeasurable = {
101101
};
102102
type TTimerTimeout = TTimerMeasurable & {
103103
type: ETimer.TIMEOUT;
104-
delay: number;
104+
timeout: number;
105105
};
106106
type TTimerAnimation = TTimerMeasurable & {
107107
type: ETimer.ANIMATION;
@@ -112,7 +112,7 @@ type TTimerIdle = TTimerMeasurable & {
112112
};
113113
type TTimerTask = TTimerMeasurable & {
114114
type: ETimer.TASK;
115-
delay: number;
115+
timeout: number;
116116
priority?: TTaskPriority;
117117
};
118118
type TTimerOptions =
@@ -159,7 +159,6 @@ const timerApi = __mirror__
159159
* in javascript event-loop
160160
*/
161161
export class Timer {
162-
delay: number = 0;
163162
timeout: number = 0;
164163
/** callback's self-time in milliseconds */
165164
callbackSelfTime: number = -1;
@@ -175,10 +174,9 @@ export class Timer {
175174

176175
if (
177176
this.#options.type === ETimer.TIMEOUT ||
177+
this.#options.type === ETimer.IDLE ||
178178
this.#options.type === ETimer.TASK
179179
) {
180-
this.delay = this.#options.delay;
181-
} else if (this.#options.type === ETimer.IDLE) {
182180
this.timeout = this.#options.timeout;
183181
}
184182

@@ -198,7 +196,7 @@ export class Timer {
198196
this.#handler = timerApi.setTimeout(() => {
199197
this.#handler = 0;
200198
this.trigger(...args);
201-
}, this.delay);
199+
}, this.timeout);
202200
} else if (
203201
this.#options.type === ETimer.ANIMATION
204202
) {
@@ -218,7 +216,7 @@ export class Timer {
218216
// nullifying AFTER the trigger to allow use-case when aborting from the callback
219217
this.#abortController = null;
220218
}, {
221-
delay: this.delay,
219+
delay: this.timeout,
222220
signal: this.#abortController.signal,
223221
priority: this.#options.priority,
224222
}).catch(NOOP);
@@ -282,7 +280,7 @@ export class Fps {
282280
#eachSecond: Timer;
283281

284282
constructor(callback?: (value: number) => void) {
285-
this.#eachSecond = new Timer({ type: ETimer.TIMEOUT, delay: 1e3 }, () => {
283+
this.#eachSecond = new Timer({ type: ETimer.TIMEOUT, timeout: 1e3 }, () => {
286284
this.value = this.#ticks;
287285
this.#ticks = 0;
288286
callback?.(this.value);

src/view/ConnectionAlert.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
let tabReloadAlertEl: Alert | null = null;
1414
let devtoolsReloadAlertEl: Alert | null = null;
1515
const delayedAlert = new Timer(
16-
{ type: ETimer.TIMEOUT, delay: INJECTION_ALERT_TIMEOUT },
16+
{ type: ETimer.TIMEOUT, timeout: INJECTION_ALERT_TIMEOUT },
1717
() => void tabReloadAlertEl?.show(),
1818
);
1919
const extensionUpdateSensor = new Timer(
20-
{ type: ETimer.TIMEOUT, delay: UPDATE_SENSOR_INTERVAL },
20+
{ type: ETimer.TIMEOUT, timeout: UPDATE_SENSOR_INTERVAL },
2121
() => {
2222
whenUpdateDetected(() => {
2323
devtoolsReloadAlertEl?.show();

src/view/menu/SummaryBarItem.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
} = $props();
2323
let enabled: boolean = $derived.by(() => panel.visible && count > 0);
2424
const stopAnimate = new Timer(
25-
{ type: ETimer.TIMEOUT, delay: 512 },
25+
{ type: ETimer.TIMEOUT, timeout: 512 },
2626
(el: HTMLElement | unknown) =>
2727
void requestAnimationFrame(() => {
2828
if (el instanceof HTMLElement) {

src/view/panels/shared/CellSelfTimeStats.svelte

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
mean: time,
1717
max: time,
1818
});
19-
const eachSecond = new Timer({ type: ETimer.TIMEOUT, delay: 1e3 }, () => {
20-
if (!mean.samples) {
21-
return;
22-
}
19+
const eachSecond = new Timer(
20+
{ type: ETimer.TIMEOUT, timeout: 1e3 },
21+
() => {
22+
if (!mean.samples) {
23+
return;
24+
}
2325
24-
vs.stdDev = mean.sampleStdDev().toFixed(1);
25-
vs.mean = mean.mean;
26-
vs.max = mean.max;
26+
vs.stdDev = mean.sampleStdDev().toFixed(1);
27+
vs.mean = mean.mean;
28+
vs.max = mean.max;
2729
28-
mean.reset();
29-
eachSecond.start();
30-
});
30+
mean.reset();
31+
eachSecond.start();
32+
},
33+
);
3134
3235
$effect(() => void mean.add(time));
3336

src/view/shared/Variable.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
);
1010
let lastUpdated: number = Date.now();
1111
const stopAnimate = new Timer(
12-
{ type: ETimer.TIMEOUT, delay: 100 },
12+
{ type: ETimer.TIMEOUT, timeout: 100 },
1313
() => (isAnimated = false),
1414
);
1515

tests/time_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('Stopper', () => {
6060
describe('Timer - default options', () => {
6161
test('start', async () => {
6262
let counter = 0;
63-
const timeout = new Timer({ type: ETimer.TIMEOUT, delay: DELAY }, () => {
63+
const timeout = new Timer({ type: ETimer.TIMEOUT, timeout: DELAY }, () => {
6464
counter++;
6565
});
6666

@@ -75,7 +75,7 @@ describe('Timer - default options', () => {
7575
test('stop before expected', async () => {
7676
let counter = 0;
7777
const timeout = new Timer(
78-
{ type: ETimer.TIMEOUT, delay: 2 * DELAY },
78+
{ type: ETimer.TIMEOUT, timeout: 2 * DELAY },
7979
() => {
8080
counter++;
8181
},
@@ -94,7 +94,7 @@ describe('Timer - default options', () => {
9494
describe('Timer - repeatable', () => {
9595
test('start/stop', async () => {
9696
let counter = 0;
97-
const interval = new Timer({ type: ETimer.TIMEOUT, delay: DELAY }, () => {
97+
const interval = new Timer({ type: ETimer.TIMEOUT, timeout: DELAY }, () => {
9898
counter++;
9999
interval.start();
100100
});
@@ -169,7 +169,7 @@ describe('Timer - idle', () => {
169169
describe('Timer - task', () => {
170170
test('start/stop', async () => {
171171
let counter = 0;
172-
const task = new Timer({ type: ETimer.TASK, delay: DELAY }, () => {
172+
const task = new Timer({ type: ETimer.TASK, timeout: DELAY }, () => {
173173
counter++;
174174
});
175175

0 commit comments

Comments
 (0)