2020-11-10 15:27:28 -09:00
|
|
|
import { baseUrl } from './utils'
|
2020-11-07 20:06:48 -09:00
|
|
|
import throttle from 'lodash.throttle'
|
2020-11-13 14:40:33 -09:00
|
|
|
import { processEvent, serverDown } from './actions'
|
2020-11-20 16:22:22 -09:00
|
|
|
import { httpClient } from './dataProvider'
|
|
|
|
|
import { REST_URL } from './consts'
|
2020-11-07 20:06:48 -09:00
|
|
|
|
2020-11-13 14:40:33 -09:00
|
|
|
const defaultIntervalCheck = 20000
|
2020-11-16 11:38:03 -09:00
|
|
|
const reconnectIntervalCheck = 2000
|
|
|
|
|
let currentIntervalCheck = reconnectIntervalCheck
|
2020-11-20 16:22:22 -09:00
|
|
|
let es = null
|
|
|
|
|
let dispatch = null
|
|
|
|
|
let timeout = null
|
2020-11-07 20:06:48 -09:00
|
|
|
|
2020-11-20 16:22:22 -09:00
|
|
|
const getEventStream = async () => {
|
2020-11-07 20:06:48 -09:00
|
|
|
if (es === null) {
|
2020-11-20 16:22:22 -09:00
|
|
|
return httpClient(`${REST_URL}/keepalive/`).then(() => {
|
|
|
|
|
es = new EventSource(
|
|
|
|
|
baseUrl(`/app/api/events?jwt=${localStorage.getItem('token')}`)
|
|
|
|
|
)
|
|
|
|
|
return es
|
|
|
|
|
})
|
2020-11-07 20:06:48 -09:00
|
|
|
}
|
|
|
|
|
return es
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 11:13:32 -09:00
|
|
|
// Reestablish the event stream after 20 secs of inactivity
|
2020-11-13 14:40:33 -09:00
|
|
|
const setTimeout = (value) => {
|
|
|
|
|
currentIntervalCheck = value
|
|
|
|
|
if (timeout != null) {
|
|
|
|
|
window.clearTimeout(timeout)
|
2020-11-09 11:13:32 -09:00
|
|
|
}
|
2020-11-13 14:40:33 -09:00
|
|
|
timeout = window.setTimeout(() => {
|
2020-11-09 11:59:21 -09:00
|
|
|
if (es != null) {
|
|
|
|
|
es.close()
|
|
|
|
|
}
|
2020-11-09 11:13:32 -09:00
|
|
|
es = null
|
2020-11-13 14:40:33 -09:00
|
|
|
startEventStream(dispatch)
|
|
|
|
|
}, currentIntervalCheck)
|
2020-11-09 11:13:32 -09:00
|
|
|
}
|
|
|
|
|
|
2020-11-20 16:22:22 -09:00
|
|
|
const stopEventStream = () => {
|
|
|
|
|
if (es) {
|
|
|
|
|
es.close()
|
|
|
|
|
}
|
|
|
|
|
es = null
|
|
|
|
|
if (timeout != null) {
|
|
|
|
|
window.clearTimeout(timeout)
|
|
|
|
|
}
|
|
|
|
|
timeout = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const setDispatch = (dispatchFunc) => {
|
2020-11-13 14:40:33 -09:00
|
|
|
dispatch = dispatchFunc
|
2020-11-20 16:22:22 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const startEventStream = async () => {
|
2020-11-13 14:40:33 -09:00
|
|
|
setTimeout(currentIntervalCheck)
|
2020-11-09 11:59:21 -09:00
|
|
|
if (!localStorage.getItem('token')) {
|
2020-11-16 11:38:03 -09:00
|
|
|
console.log('Cannot create a unauthenticated EventSource connection')
|
2020-11-09 11:59:21 -09:00
|
|
|
return
|
|
|
|
|
}
|
2020-11-20 16:22:22 -09:00
|
|
|
getEventStream().then((newStream) => {
|
|
|
|
|
newStream.onmessage = throttle(
|
|
|
|
|
(msg) => {
|
|
|
|
|
const data = JSON.parse(msg.data)
|
|
|
|
|
if (data.name !== 'keepAlive') {
|
|
|
|
|
dispatch(processEvent(data))
|
|
|
|
|
}
|
|
|
|
|
setTimeout(defaultIntervalCheck) // Reset timeout on every received message
|
|
|
|
|
},
|
|
|
|
|
100,
|
|
|
|
|
{ trailing: true }
|
|
|
|
|
)
|
|
|
|
|
newStream.onerror = (e) => {
|
|
|
|
|
setTimeout(reconnectIntervalCheck)
|
|
|
|
|
dispatch(serverDown())
|
|
|
|
|
}
|
|
|
|
|
es = newStream
|
|
|
|
|
})
|
2020-11-07 20:06:48 -09:00
|
|
|
}
|
2020-11-20 16:22:22 -09:00
|
|
|
|
|
|
|
|
export { setDispatch, startEventStream, stopEventStream }
|