Connect eventStream after login

This commit is contained in:
Deluan
2021-11-29 18:49:29 -05:00
parent 9fe978953c
commit 9892524ab8
2 changed files with 11 additions and 11 deletions
+3 -5
View File
@@ -29,7 +29,7 @@ import {
import createAdminStore from './store/createAdminStore' import createAdminStore from './store/createAdminStore'
import { i18nProvider } from './i18n' import { i18nProvider } from './i18n'
import config from './config' import config from './config'
import { startEventStream } from './eventStream' import { setDispatch, startEventStream } from './eventStream'
import { keyMap } from './hotkeys' import { keyMap } from './hotkeys'
import useChangeThemeColor from './useChangeThemeColor' import useChangeThemeColor from './useChangeThemeColor'
@@ -69,10 +69,8 @@ const Admin = (props) => {
useChangeThemeColor() useChangeThemeColor()
useEffect(() => { useEffect(() => {
if (config.devActivityPanel) { if (config.devActivityPanel) {
authProvider setDispatch(adminStore.dispatch)
.checkAuth() authProvider.checkAuth().then(() => startEventStream(adminStore.dispatch))
.then(() => startEventStream(adminStore.dispatch))
.catch(() => {}) // ignore if not logged in
} }
}, []) }, [])
+8 -6
View File
@@ -35,7 +35,7 @@ const setTimeout = (value) => {
es.close() es.close()
} }
es = null es = null
await startEventStream(dispatch) await startEventStream()
}, currentIntervalCheck) }, currentIntervalCheck)
} }
@@ -50,6 +50,10 @@ const stopEventStream = () => {
timeout = null timeout = null
} }
const setDispatch = (dispatchFunc) => {
dispatch = dispatchFunc
}
const eventHandler = (event) => { const eventHandler = (event) => {
const data = JSON.parse(event.data) const data = JSON.parse(event.data)
if (event.type !== 'keepAlive') { if (event.type !== 'keepAlive') {
@@ -60,12 +64,10 @@ const eventHandler = (event) => {
const throttledEventHandler = throttle(eventHandler, 100, { trailing: true }) const throttledEventHandler = throttle(eventHandler, 100, { trailing: true })
const startEventStream = async (dispatchFunc) => { const startEventStream = async () => {
dispatch = dispatchFunc
setTimeout(currentIntervalCheck) setTimeout(currentIntervalCheck)
if (!localStorage.getItem('is-authenticated')) { if (!localStorage.getItem('is-authenticated')) {
console.log('Cannot create a unauthenticated EventSource connection') return Promise.resolve()
return Promise.reject()
} }
return getEventStream() return getEventStream()
.then((newStream) => { .then((newStream) => {
@@ -85,4 +87,4 @@ const startEventStream = async (dispatchFunc) => {
}) })
} }
export { startEventStream, stopEventStream } export { setDispatch, startEventStream, stopEventStream }