Skip to content

Commit

Permalink
logs: Refactor the heartbeat to use setInterval instead of setTimeout
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi committed Jul 4, 2024
1 parent 1fb51de commit 718e0dd
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/features/device-logs/lib/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,30 @@ async function handleStreamingRead(
}
});

let heartbeatTimeout: ReturnType<typeof setTimeout> | null = null;
let heartbeatInterval: ReturnType<typeof setInterval> | null = null;

function heartbeat() {
if (state !== StreamState.Closed) {
// In order to keep the connection alive, output new lines every now and then
write('\n');
heartbeatTimeout = setTimeout(heartbeat, LOGS_HEARTBEAT_INTERVAL);
return;
}
if (heartbeatInterval != null) {
clearInterval(heartbeatInterval);
heartbeatInterval = null;
}
}

heartbeatTimeout = setTimeout(heartbeat, LOGS_HEARTBEAT_INTERVAL);
heartbeatInterval = setInterval(heartbeat, LOGS_HEARTBEAT_INTERVAL);

function close() {
if (state !== StreamState.Closed) {
state = StreamState.Closed;
getBackend().unsubscribe(ctx, onLog);
}
if (heartbeatTimeout != null) {
clearTimeout(heartbeatTimeout);
heartbeatTimeout = null;
if (heartbeatInterval != null) {
clearInterval(heartbeatInterval);
heartbeatInterval = null;
}
}

Expand Down

0 comments on commit 718e0dd

Please sign in to comment.