Do not create the EventStream if unauthenticated

This commit is contained in:
Deluan 2020-11-09 15:59:21 -05:00
parent a2e76d6898
commit 1b624b2505
1 changed files with 8 additions and 2 deletions

View File

@ -20,13 +20,20 @@ const setTimeout = () => {
window.clearTimeout(timeOut) window.clearTimeout(timeOut)
} }
timeOut = window.setTimeout(() => { timeOut = window.setTimeout(() => {
es.close() if (es != null) {
es.close()
}
es = null es = null
startEventStream(onMessageHandler) startEventStream(onMessageHandler)
}, 20000) }, 20000)
} }
export const startEventStream = (messageHandler) => { export const startEventStream = (messageHandler) => {
setTimeout()
if (!localStorage.getItem('token')) {
console.log('Cannot create a unauthenticated EventSource')
return
}
const es = getEventStream() const es = getEventStream()
onMessageHandler = messageHandler onMessageHandler = messageHandler
es.onmessage = throttle( es.onmessage = throttle(
@ -40,5 +47,4 @@ export const startEventStream = (messageHandler) => {
100, 100,
{ trailing: true } { trailing: true }
) )
setTimeout()
} }