From 718e0dd7ed3aef345ab2196bf4d18eb2ead0762c Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 4 Jul 2024 17:55:30 +0300 Subject: [PATCH] logs: Refactor the heartbeat to use setInterval instead of setTimeout Change-type: patch --- src/features/device-logs/lib/read.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/features/device-logs/lib/read.ts b/src/features/device-logs/lib/read.ts index 728843dc8f..b0271d0377 100644 --- a/src/features/device-logs/lib/read.ts +++ b/src/features/device-logs/lib/read.ts @@ -115,26 +115,30 @@ async function handleStreamingRead( } }); - let heartbeatTimeout: ReturnType | null = null; + let heartbeatInterval: ReturnType | 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; } }