From ea096298033689333bde268280584ad508660c0e Mon Sep 17 00:00:00 2001 From: Deluan Date: Sun, 13 Dec 2020 20:17:20 -0500 Subject: [PATCH] Fix another possible race condition --- ui/src/eventStream.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ui/src/eventStream.js b/ui/src/eventStream.js index a7f161ad5..fc8d64f6f 100644 --- a/ui/src/eventStream.js +++ b/ui/src/eventStream.js @@ -12,7 +12,8 @@ let dispatch = null let timeout = null const getEventStream = async () => { - if (es === null) { + if (!es) { + // Call `keepalive` to refresh the jwt token await httpClient(`${REST_URL}/keepalive/eventSource`) es = new EventSource( baseUrl(`${REST_URL}/events?jwt=${localStorage.getItem('token')}`) @@ -24,15 +25,15 @@ const getEventStream = async () => { // Reestablish the event stream after 20 secs of inactivity const setTimeout = (value) => { currentIntervalCheck = value - if (timeout != null) { + if (timeout) { window.clearTimeout(timeout) } - timeout = window.setTimeout(() => { - if (es != null) { + timeout = window.setTimeout(async () => { + if (es) { es.close() } es = null - startEventStream(dispatch) + await startEventStream(dispatch) }, currentIntervalCheck) } @@ -41,7 +42,7 @@ const stopEventStream = () => { es.close() } es = null - if (timeout != null) { + if (timeout) { window.clearTimeout(timeout) } timeout = null