Skip to content

Commit 8b5fafb

Browse files
committed
Fix status check and clean up group connection
1 parent 454dfa7 commit 8b5fafb

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

lego-webapp/components/WebsocketGroupProvider/index.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ReactNode, useEffect } from 'react';
44
import { Websockets as WebsocketsAT } from '~/redux/actionTypes';
55
import { useAppDispatch, useAppSelector } from '~/redux/hooks';
66
import { WebsocketsStatus as WebsocketsStatusType } from '~/redux/models/Websockets';
7-
import { GROUP_STATUS_PENDING } from '~/redux/slices/websockets';
7+
import { STATUS_ERROR } from '~/redux/slices/websockets';
88
import styles from './WebsocketStatus.module.css';
99

1010
type ChildrenProps = {
@@ -27,8 +27,14 @@ const WebsocketGroupProvider = ({ group, children }: Props) => {
2727
const groupStatus =
2828
useAppSelector(
2929
(state) => state.websockets.groups.find((g) => g.group === group)?.status,
30-
) || GROUP_STATUS_PENDING;
30+
) || STATUS_ERROR;
3131

32+
const status = {
33+
connected: websocketsStatus.connected && groupStatus.connected,
34+
pending: websocketsStatus.pending || groupStatus.pending,
35+
error: websocketsStatus.error || groupStatus.error,
36+
};
37+
// Ensure proper order of connection
3238
useEffect(() => {
3339
if (websocketsStatus.connected && !websocketsStatus.error) {
3440
dispatch({
@@ -39,10 +45,23 @@ const WebsocketGroupProvider = ({ group, children }: Props) => {
3945
// eslint-disable-next-line react-hooks/exhaustive-deps
4046
}, [websocketsStatus.connected, websocketsStatus.error]);
4147

48+
// Clean up
49+
useEffect(() => {
50+
return () => {
51+
if (websocketsStatus.connected && !websocketsStatus.error) {
52+
dispatch({
53+
type: WebsocketsAT.GROUP_LEAVE.BEGIN,
54+
payload: { group: group },
55+
});
56+
}
57+
};
58+
// eslint-disable-next-line react-hooks/exhaustive-deps
59+
}, []);
60+
4261
const WebsocketsStatusComponent = () => {
43-
const iconNode = groupStatus?.connected ? (
62+
const iconNode = status?.connected ? (
4463
<Radio />
45-
) : groupStatus?.pending ? (
64+
) : status?.pending ? (
4665
<LoaderCircle className={styles.spin} />
4766
) : (
4867
<Unplug />
@@ -63,7 +82,7 @@ const WebsocketGroupProvider = ({ group, children }: Props) => {
6382

6483
return children({
6584
WebsocketStatus: () => <WebsocketsStatusComponent />,
66-
...groupStatus,
85+
...status,
6786
});
6887
};
6988

lego-webapp/redux/slices/websockets.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@ import { Websockets as WebsocketsAT } from '../actionTypes';
33
import { Websockets, WebsocketsGroup } from '../models/Websockets';
44
import { EntityType } from '../models/entities';
55

6-
const GROUP_STATUS_INITIAL = {
6+
const STATUS_INITIAL = {
77
connected: false,
88
pending: false,
99
error: false,
1010
};
1111

12-
export const GROUP_STATUS_PENDING = {
13-
...GROUP_STATUS_INITIAL,
14-
pending: true,
12+
const STATUS_CONNECTED = {
13+
...STATUS_INITIAL,
14+
connected: true,
1515
};
1616

17-
const GROUP_STATUS_CONNECTED = {
18-
...GROUP_STATUS_INITIAL,
19-
connected: true,
17+
export const STATUS_PENDING = {
18+
...STATUS_INITIAL,
19+
pending: true,
2020
};
2121

22-
export const GROUP_STATUS_ERROR = {
23-
...GROUP_STATUS_INITIAL,
22+
const STATUS_PINGING = {
23+
...STATUS_CONNECTED,
24+
pending: true,
25+
}
26+
27+
export const STATUS_ERROR = {
28+
...STATUS_INITIAL,
2429
error: true,
2530
};
2631

2732
const initialState: Websockets = {
28-
status: {
29-
connected: false,
30-
error: false,
31-
},
33+
status: STATUS_INITIAL,
3234
groups: [] as WebsocketsGroup[],
3335
};
3436

@@ -38,32 +40,40 @@ const websocketsSlice = createSlice({
3840
reducers: {},
3941
extraReducers: ({ addCase }) => {
4042
addCase(WebsocketsAT.CONNECTED, (state) => {
41-
state.status.connected = true;
43+
state.status = STATUS_CONNECTED;
4244
});
4345
addCase(WebsocketsAT.CLOSED, (state) => {
44-
state.status.connected = false;
46+
state.status = STATUS_INITIAL;
47+
state.groups = []
4548
});
4649
addCase(WebsocketsAT.ERROR, (state) => {
47-
state.status.error = true;
50+
state.status = STATUS_ERROR;
51+
state.groups = []
4852
});
53+
addCase(WebsocketsAT.PING.BEGIN, (state) => {
54+
state.status = STATUS_PINGING;
55+
})
56+
addCase(WebsocketsAT.PING.SUCCESS, (state) => {
57+
state.status = STATUS_CONNECTED;
58+
})
4959
addCase(WebsocketsAT.GROUP_JOIN.BEGIN, (state, action) => {
50-
if (!setGroupStatus(state, action, GROUP_STATUS_PENDING))
60+
if (!setGroupStatus(state, action, STATUS_PENDING))
5161
state.groups.push({
5262
group: action.payload.group,
53-
status: GROUP_STATUS_PENDING,
63+
status: STATUS_PENDING,
5464
});
5565
});
5666
addCase(WebsocketsAT.GROUP_JOIN.SUCCESS, (state, action) => {
57-
setGroupStatus(state, action, GROUP_STATUS_CONNECTED);
67+
setGroupStatus(state, action, STATUS_CONNECTED);
5868
});
5969
addCase(WebsocketsAT.GROUP_JOIN.FAILURE, (state, action) => {
60-
setGroupStatus(state, action, GROUP_STATUS_ERROR);
70+
setGroupStatus(state, action, STATUS_ERROR);
6171
});
6272
addCase(WebsocketsAT.GROUP_LEAVE.BEGIN, (state, action) => {
63-
setGroupStatus(state, action, GROUP_STATUS_PENDING);
73+
setGroupStatus(state, action, STATUS_PENDING);
6474
});
6575
addCase(WebsocketsAT.GROUP_LEAVE.SUCCESS, (state, action) => {
66-
setGroupStatus(state, action, GROUP_STATUS_INITIAL);
76+
setGroupStatus(state, action, STATUS_INITIAL);
6777
});
6878
},
6979
});

0 commit comments

Comments
 (0)