Skip to content

Commit 2aead00

Browse files
authored
Make the reconnect action of live more robust (codesandbox#2928)
* Make the reconnect action of live more robust * Move disconnect reason to an enum
1 parent 1a295fd commit 2aead00

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

packages/app/src/app/overmind/effects/live/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ export default {
131131
}) => {}
132132
) {
133133
channel.onMessage = (event: any, data: any) => {
134-
const disconnected = data == null && event === 'phx_error';
134+
const disconnected =
135+
(data == null || Object.keys(data).length === 0) &&
136+
event === 'phx_error';
135137
const alteredEvent = disconnected ? 'connection-loss' : event;
136138

137139
const _isOwnMessage = Boolean(

packages/app/src/app/overmind/namespaces/live/liveMessageOperators.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import {
2-
LiveMessageEvent,
32
LiveMessage,
43
Module,
54
Directory,
65
Selection,
6+
LiveDisconnectReason,
77
} from '@codesandbox/common/lib/types';
88
import { mutate, json } from 'overmind';
99
import { Operator } from 'app/overmind';
1010
import { camelizeKeys } from 'humps';
1111
import { NotificationStatus } from '@codesandbox/notifications/lib/state';
1212

13-
export const onJoin: Operator<LiveMessage<LiveMessageEvent.JOIN>> = mutate(
14-
({ effects, state }) => {
15-
effects.notificationToast.success(
16-
state.live.isTeam ? 'Connected to Live Team!' : 'Connected to Live!'
17-
);
13+
export const onJoin: Operator<
14+
LiveMessage<{ status: 'connected'; live_user_id: string }>
15+
> = mutate(({ effects, state }, { data }) => {
16+
state.live.liveUserId = data.live_user_id;
1817

19-
if (state.live.reconnecting) {
20-
effects.live.getAllClients().forEach(client => {
21-
client.serverReconnect();
22-
});
23-
}
18+
effects.notificationToast.success(
19+
state.live.isTeam ? 'Connected to Live Team!' : 'Connected to Live!'
20+
);
2421

25-
state.live.reconnecting = false;
22+
if (state.live.reconnecting) {
23+
effects.live.getAllClients().forEach(client => {
24+
client.serverReconnect();
25+
});
2626
}
27-
);
27+
28+
state.live.reconnecting = false;
29+
});
2830

2931
export const onModuleState: Operator<
3032
LiveMessage<{
@@ -84,12 +86,14 @@ export const onUserLeft: Operator<
8486
const { users } = state.live.roomInfo;
8587
const user = users ? users.find(u => u.id === data.left_user_id) : null;
8688

87-
effects.notificationToast.add({
88-
message: user
89-
? `${user.username} left the live session.`
90-
: 'Someone left the live session',
91-
status: NotificationStatus.NOTICE,
92-
});
89+
if (user.id !== state.live.liveUserId) {
90+
effects.notificationToast.add({
91+
message: user
92+
? `${user.username} left the live session.`
93+
: 'Someone left the live session',
94+
status: NotificationStatus.NOTICE,
95+
});
96+
}
9397
}
9498

9599
actions.live.internal.clearUserSelections(data.left_user_id);
@@ -397,12 +401,9 @@ export const onConnectionLoss: Operator<LiveMessage> = mutate(
397401
);
398402

399403
export const onDisconnect: Operator<
400-
LiveMessage<{
401-
reason: string;
402-
}>
404+
LiveMessage<{ reason: LiveDisconnectReason }>
403405
> = mutate(({ state, actions }, { data }) => {
404406
actions.live.internal.disconnect();
405-
406407
state.editor.currentSandbox.owned = state.live.isOwner;
407408

408409
actions.modalOpened({

packages/common/src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,6 @@ export enum PatronBadge {
670670
FOURTH = 'patron-4',
671671
}
672672

673+
export type LiveDisconnectReason = 'close' | 'inactivity';
674+
673675
export type PatronTier = 1 | 2 | 3 | 4;

0 commit comments

Comments
 (0)