Skip to content

Commit

Permalink
Fix another possible race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Dec 14, 2020
1 parent f2fd7ed commit ea09629
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ui/src/eventStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`)
Expand All @@ -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)
}

Expand All @@ -41,7 +42,7 @@ const stopEventStream = () => {
es.close()
}
es = null
if (timeout != null) {
if (timeout) {
window.clearTimeout(timeout)
}
timeout = null
Expand Down

0 comments on commit ea09629

Please sign in to comment.