fix(companion): dedupe heartbeat loop scheduled timers

This commit is contained in:
William Valentin
2026-02-16 19:39:29 -08:00
parent ebb62ffb65
commit 61533bd816
3 changed files with 33 additions and 0 deletions
+17
View File
@@ -217,4 +217,21 @@ describe('CompanionHeartbeatLoop', () => {
lastFailure: null,
});
});
it('tickNow while running does not accumulate duplicate scheduled timers', async () => {
const publishHeartbeat = vi.fn(async () => buildStatusResult());
const loop = new CompanionHeartbeatLoop({ publishHeartbeat }, { intervalMs: 1000 });
loop.start(false);
expect(vi.getTimerCount()).toBe(1);
await loop.tickNow();
expect(vi.getTimerCount()).toBe(1);
expect(publishHeartbeat).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(1000);
expect(publishHeartbeat).toHaveBeenCalledTimes(2);
loop.stop();
});
});
+4
View File
@@ -138,6 +138,10 @@ export class CompanionHeartbeatLoop {
if (!this.started) {
return;
}
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
const delay = this.computeDelayMs();
this.timer = setTimeout(() => {
void this.tick();