From 9892524ab89ac944015f8f13dd9a4249c60ad172 Mon Sep 17 00:00:00 2001 From: Deluan Date: Mon, 29 Nov 2021 18:49:29 -0500 Subject: [PATCH] Connect eventStream after login --- ui/src/App.js | 8 +++----- ui/src/eventStream.js | 14 ++++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ui/src/App.js b/ui/src/App.js index e973d491..031153ad 100644 --- a/ui/src/App.js +++ b/ui/src/App.js @@ -29,7 +29,7 @@ import { import createAdminStore from './store/createAdminStore' import { i18nProvider } from './i18n' import config from './config' -import { startEventStream } from './eventStream' +import { setDispatch, startEventStream } from './eventStream' import { keyMap } from './hotkeys' import useChangeThemeColor from './useChangeThemeColor' @@ -69,10 +69,8 @@ const Admin = (props) => { useChangeThemeColor() useEffect(() => { if (config.devActivityPanel) { - authProvider - .checkAuth() - .then(() => startEventStream(adminStore.dispatch)) - .catch(() => {}) // ignore if not logged in + setDispatch(adminStore.dispatch) + authProvider.checkAuth().then(() => startEventStream(adminStore.dispatch)) } }, []) diff --git a/ui/src/eventStream.js b/ui/src/eventStream.js index 5ab11692..911d5cfb 100644 --- a/ui/src/eventStream.js +++ b/ui/src/eventStream.js @@ -35,7 +35,7 @@ const setTimeout = (value) => { es.close() } es = null - await startEventStream(dispatch) + await startEventStream() }, currentIntervalCheck) } @@ -50,6 +50,10 @@ const stopEventStream = () => { timeout = null } +const setDispatch = (dispatchFunc) => { + dispatch = dispatchFunc +} + const eventHandler = (event) => { const data = JSON.parse(event.data) if (event.type !== 'keepAlive') { @@ -60,12 +64,10 @@ const eventHandler = (event) => { const throttledEventHandler = throttle(eventHandler, 100, { trailing: true }) -const startEventStream = async (dispatchFunc) => { - dispatch = dispatchFunc +const startEventStream = async () => { setTimeout(currentIntervalCheck) if (!localStorage.getItem('is-authenticated')) { - console.log('Cannot create a unauthenticated EventSource connection') - return Promise.reject() + return Promise.resolve() } return getEventStream() .then((newStream) => { @@ -85,4 +87,4 @@ const startEventStream = async (dispatchFunc) => { }) } -export { startEventStream, stopEventStream } +export { setDispatch, startEventStream, stopEventStream }