Skip to content

Commit 616b5e5

Browse files
committed
Ensure only one binding for network connect
1 parent 6b4541d commit 616b5e5

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

frontend/src/buttons/RefreshButton/RefreshButton.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import { attributeName } from '@common/nameHelper'
1313
export const RefreshButton: React.FC<ButtonProps> = props => {
1414
const dispatch = useDispatch<Dispatch>()
1515
const { deviceID } = useParams<{ deviceID?: string }>()
16-
const { fetching, device } = useSelector((state: ApplicationState) => ({
16+
const { fetching, device, networkConnected } = useSelector((state: ApplicationState) => ({
1717
fetching: getDeviceModel(state).fetching || (deviceID && state.logs.fetching) || state.ui.fetching,
1818
device: selectDevice(state, undefined, deviceID),
19+
networkConnected: state.ui.network,
1920
}))
2021

2122
const connectionPage = useRouteMatch('/connections')
@@ -66,11 +67,11 @@ export const RefreshButton: React.FC<ButtonProps> = props => {
6667
}
6768

6869
React.useEffect(() => {
69-
network.on('connect', refresh)
70-
return () => {
71-
network.off('connect', refresh)
70+
if (networkConnected) {
71+
console.log('NETWORK CONNECTED')
72+
refresh()
7273
}
73-
}, [])
74+
}, [networkConnected])
7475

7576
return (
7677
<IconButton

frontend/src/models/ui.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type UIState = {
3939
silent: string | null
4040
selected: IDevice['id'][]
4141
connected: boolean
42+
network: boolean
4243
offline?: { severity: NoticeProps['severity']; title: string; message: NoticeProps['children'] }
4344
mobileNavigation: string[]
4445
waitMessage?: string
@@ -112,6 +113,7 @@ export const defaultState: UIState = {
112113
silent: null,
113114
selected: [],
114115
connected: false,
116+
network: false,
115117
offline: undefined,
116118
mobileNavigation: [],
117119
waitMessage: undefined,

frontend/src/services/Network.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { store } from '../store'
1+
import { dispatch } from '../store'
22
import { EventEmitter } from 'events'
33

44
class Network extends EventEmitter {
@@ -50,8 +50,9 @@ class Network extends EventEmitter {
5050
offline = () => {
5151
if (navigator.onLine) return
5252
this.log('DISCONNECT')
53-
store.dispatch.ui.set({
53+
dispatch.ui.set({
5454
offline: { title: 'Disconnected', message: 'Internet access is required.', severity: 'warning' },
55+
network: false,
5556
})
5657
this.shouldConnect = true
5758
this.emit('disconnect')
@@ -60,13 +61,14 @@ class Network extends EventEmitter {
6061
online = () => {
6162
if (!navigator.onLine) return
6263
this.log('NETWORK ONLINE')
63-
store.dispatch.ui.set({ offline: undefined })
64+
dispatch.ui.set({ offline: undefined })
6465
this.connect()
6566
}
6667

6768
connect = () => {
6869
if (this.shouldConnect && this.isActive()) {
6970
this.shouldConnect = false
71+
dispatch.ui.set({ network: true })
7072
this.emit('connect')
7173
this.log('CONNECT')
7274
}

0 commit comments

Comments
 (0)