fix(companion): dedupe heartbeat loop scheduled timers
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user