Try to avoid creating multiple eventStreams
This commit is contained in:
+21
-23
@@ -1,6 +1,6 @@
|
|||||||
import React, { useEffect } from 'react'
|
import React, { useEffect } from 'react'
|
||||||
import ReactGA from 'react-ga'
|
import ReactGA from 'react-ga'
|
||||||
import { Provider, useDispatch } from 'react-redux'
|
import { Provider } from 'react-redux'
|
||||||
import { createHashHistory } from 'history'
|
import { createHashHistory } from 'history'
|
||||||
import { Admin as RAAdmin, Resource } from 'react-admin'
|
import { Admin as RAAdmin, Resource } from 'react-admin'
|
||||||
import { HotKeys } from 'react-hotkeys'
|
import { HotKeys } from 'react-hotkeys'
|
||||||
@@ -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 { setDispatch, startEventStream } from './eventStream'
|
import { startEventStream } from './eventStream'
|
||||||
import { keyMap } from './hotkeys'
|
import { keyMap } from './hotkeys'
|
||||||
import useChangeThemeColor from './useChangeThemeColor'
|
import useChangeThemeColor from './useChangeThemeColor'
|
||||||
|
|
||||||
@@ -43,40 +43,38 @@ if (config.gaTrackingId) {
|
|||||||
ReactGA.pageview(window.location.pathname)
|
ReactGA.pageview(window.location.pathname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const adminStore = createAdminStore({
|
||||||
|
authProvider,
|
||||||
|
dataProvider,
|
||||||
|
history,
|
||||||
|
customReducers: {
|
||||||
|
player: playerReducer,
|
||||||
|
albumView: albumViewReducer,
|
||||||
|
theme: themeReducer,
|
||||||
|
addToPlaylistDialog: addToPlaylistDialogReducer,
|
||||||
|
expandInfoDialog: expandInfoDialogReducer,
|
||||||
|
listenBrainzTokenDialog: listenBrainzTokenDialogReducer,
|
||||||
|
activity: activityReducer,
|
||||||
|
settings: settingsReducer,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const App = () => (
|
const App = () => (
|
||||||
<Provider
|
<Provider store={adminStore}>
|
||||||
store={createAdminStore({
|
|
||||||
authProvider,
|
|
||||||
dataProvider,
|
|
||||||
history,
|
|
||||||
customReducers: {
|
|
||||||
player: playerReducer,
|
|
||||||
albumView: albumViewReducer,
|
|
||||||
theme: themeReducer,
|
|
||||||
addToPlaylistDialog: addToPlaylistDialogReducer,
|
|
||||||
expandInfoDialog: expandInfoDialogReducer,
|
|
||||||
listenBrainzTokenDialog: listenBrainzTokenDialogReducer,
|
|
||||||
activity: activityReducer,
|
|
||||||
settings: settingsReducer,
|
|
||||||
},
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<Admin />
|
<Admin />
|
||||||
</Provider>
|
</Provider>
|
||||||
)
|
)
|
||||||
|
|
||||||
const Admin = (props) => {
|
const Admin = (props) => {
|
||||||
useChangeThemeColor()
|
useChangeThemeColor()
|
||||||
const dispatch = useDispatch()
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (config.devActivityPanel) {
|
if (config.devActivityPanel) {
|
||||||
setDispatch(dispatch)
|
|
||||||
authProvider
|
authProvider
|
||||||
.checkAuth()
|
.checkAuth()
|
||||||
.then(() => startEventStream())
|
.then(() => startEventStream(adminStore.dispatch))
|
||||||
.catch(() => {}) // ignore if not logged in
|
.catch(() => {}) // ignore if not logged in
|
||||||
}
|
}
|
||||||
}, [dispatch])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RAAdmin
|
<RAAdmin
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ const setTimeout = (value) => {
|
|||||||
es.close()
|
es.close()
|
||||||
}
|
}
|
||||||
es = null
|
es = null
|
||||||
await startEventStream()
|
await startEventStream(dispatch)
|
||||||
}, currentIntervalCheck)
|
}, currentIntervalCheck)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,10 +50,6 @@ 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') {
|
||||||
@@ -64,7 +60,8 @@ const eventHandler = (event) => {
|
|||||||
|
|
||||||
const throttledEventHandler = throttle(eventHandler, 100, { trailing: true })
|
const throttledEventHandler = throttle(eventHandler, 100, { trailing: true })
|
||||||
|
|
||||||
const startEventStream = async () => {
|
const startEventStream = async (dispatchFunc) => {
|
||||||
|
dispatch = dispatchFunc
|
||||||
setTimeout(currentIntervalCheck)
|
setTimeout(currentIntervalCheck)
|
||||||
if (!localStorage.getItem('is-authenticated')) {
|
if (!localStorage.getItem('is-authenticated')) {
|
||||||
console.log('Cannot create a unauthenticated EventSource connection')
|
console.log('Cannot create a unauthenticated EventSource connection')
|
||||||
@@ -88,4 +85,4 @@ const startEventStream = async () => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export { setDispatch, startEventStream, stopEventStream }
|
export { startEventStream, stopEventStream }
|
||||||
|
|||||||
Reference in New Issue
Block a user