Skip to content

Commit 2f14dd9

Browse files
committed
refactor monaco editor and fix channels
1 parent 85dd986 commit 2f14dd9

28 files changed

+444
-541
lines changed

services/app/apps/codebattle/assets/js/widgets/components/Editor.jsx

Lines changed: 159 additions & 365 deletions
Large diffs are not rendered by default.

services/app/apps/codebattle/assets/js/widgets/components/ExtendedEditor.jsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
// import cn from 'classnames';
55
// import { registerRulesForLanguage } from 'monaco-ace-tokenizer';
6-
// import { initVimMode } from 'monaco-vim';
76
// import PropTypes from 'prop-types';
87
// import MonacoEditor from 'react-monaco-editor';
98

@@ -18,23 +17,19 @@ import {
1817
} from '../selectors/index';
1918
import { actions } from '../slices';
2019

21-
import Editor from './Editor';
22-
23-
class ExtendedEditor extends Editor {
20+
class ExtendedEditor {
2421
static propTypes = {
25-
...Editor.propTypes,
2622
monacoTheme: PropTypes.string,
2723
fontFamly: PropTypes.string || undefined,
2824
};
2925

3026
static defaultProps = {
31-
...Editor.defaultProps,
3227
monacoTheme: 'default',
3328
fontFamly: undefined,
3429
}
3530

3631
constructor(props) {
37-
super(props);
32+
// super(props);
3833

3934
this.options = {
4035
fontFamily: props.fontFamily,

services/app/apps/codebattle/assets/js/widgets/components/InvitesContainer.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ function InvitesContainer() {
4343

4444
useEffect(() => {
4545
dispatch(initInvites(currentUserId));
46-
const clearPresence = initPresence(followId)(dispatch);
46+
const channel = initPresence(followId)(dispatch);
4747

48-
return clearPresence;
48+
return () => {
49+
channel.leave();
50+
};
4951
// eslint-disable-next-line react-hooks/exhaustive-deps
5052
}, []);
5153

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
22
default: 'default',
3-
vim: 'vim',
3+
// vim: 'vim',
44
};

services/app/apps/codebattle/assets/js/widgets/middlewares/Channel.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,53 @@ const nonChannelErrorMessage = "Socket channel wasn't initialize";
1010
const nonPresenceErrorMessage = "Socket channel presence wasn't initialize";
1111

1212
export default class Channel {
13+
topic;
14+
1315
listeners = {};
1416

1517
channel;
1618

1719
presence;
1820

1921
constructor(topic, params) {
22+
if (!topic) {
23+
return;
24+
}
25+
this.setupChannel(topic, params);
26+
}
27+
28+
setupChannel(topic, params) {
2029
const channel = socket.channel(
2130
topic,
2231
decamelizeKeys(params, { separator: '_' }),
2332
);
2433

34+
this.topic = topic;
2535
this.channel = channel;
2636
this.presence = new Presence(channel);
37+
38+
Object.keys(this.listeners).forEach(listenerTopic => {
39+
const listeners = this.listeners[listenerTopic];
40+
if (listeners) {
41+
const newListeners = listeners.map(listener => {
42+
const { cb } = listener;
43+
const ref = channel.on(listenerTopic, cb);
44+
45+
return { ...listener, ref };
46+
});
47+
48+
this.listeners[listenerTopic] = newListeners;
49+
}
50+
});
51+
52+
return this;
2753
}
2854

2955
addListener(topic, cb, params = {}) {
30-
if (!this.channel) {
31-
throw new Error(nonChannelErrorMessage);
32-
}
33-
3456
const currentListeners = this.listeners[topic];
35-
const newRef = this.channel.on(topic, cb);
57+
const newRef = this.channel
58+
? this.channel.on(topic, cb)
59+
: null;
3660
const newListener = { ref: newRef, callback: cb, params };
3761

3862
if (!currentListeners) {
@@ -44,11 +68,7 @@ export default class Channel {
4468
return this;
4569
}
4670

47-
clearListeners(topic, params) {
48-
if (!this.channel) {
49-
throw new Error(nonChannelErrorMessage);
50-
}
51-
71+
removeListeners(topic, params) {
5272
if (!topic) {
5373
return this.clear();
5474
}
@@ -142,6 +162,7 @@ export default class Channel {
142162

143163
const pushInstance = this.channel.leave(...params);
144164

165+
this.topic = undefined;
145166
this.channel = undefined;
146167
this.presence = undefined;
147168

services/app/apps/codebattle/assets/js/widgets/middlewares/Chat.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ import capitalize from 'lodash/capitalize';
55
import { channelMethods, channelTopics } from '../../socket';
66
import { actions } from '../slices';
77
import { getSystemMessage } from '../utils/chat';
8-
import getChatName from '../utils/names';
8+
import getChatTopic from '../utils/names';
99

1010
import Channel from './Channel';
1111

1212
const isRecord = Gon.getAsset('is_record');
1313

14-
const channel = isRecord ? null : new Channel(getChatName('channel'));
14+
const channel = new Channel();
1515

1616
export const pushCommandTypes = {
1717
cleanBanned: 'clead_banned',
1818
};
1919

20-
const establishChat = () => dispatch => {
21-
const currentChannel = channel;
20+
const establishChat = page => dispatch => {
2221
const camelizeKeysAndDispatch = actionCreator => data => dispatch(actionCreator(camelizeKeys(data)));
2322

24-
currentChannel.join().receive('ok', data => {
25-
const page = getChatName('page');
23+
channel.join().receive('ok', data => {
2624
const greetingMessage = getSystemMessage({
2725
text: `Joined channel: ${capitalize(page)}`,
2826
status: 'success',
@@ -33,23 +31,25 @@ const establishChat = () => dispatch => {
3331
dispatch(actions.updateChatChannelState(true));
3432
});
3533

36-
currentChannel.onError(() => dispatch(actions.updateChatChannelState(false)));
34+
channel.onError(() => dispatch(actions.updateChatChannelState(false)));
3735

3836
const handleUserJoined = camelizeKeysAndDispatch(actions.userJoinedChat);
3937
const handleUserLeft = camelizeKeysAndDispatch(actions.userLeftChat);
4038
const handleNewMessage = camelizeKeysAndDispatch(actions.newChatMessage);
4139
const handleUserbanned = camelizeKeysAndDispatch(actions.banUserChat);
4240

43-
return currentChannel
41+
return channel
4442
.addListener(channelTopics.chatUserJoinedTopic, handleUserJoined)
4543
.addListener(channelTopics.chatUserLeftTopic, handleUserLeft)
4644
.addListener(channelTopics.chatUserNewMsgTopic, handleNewMessage)
4745
.addListener(channelTopics.chatUserBannedTopic, handleUserbanned);
4846
};
4947

50-
export const connectToChat = (useChat = true) => dispatch => {
48+
export const connectToChat = (useChat = true, chatPage = 'channel', chatId) => dispatch => {
5149
if (!isRecord && useChat) {
52-
const currentChannel = establishChat()(dispatch);
50+
const page = getChatTopic(chatPage, chatId);
51+
channel.setupChannel(page);
52+
const currentChannel = establishChat(page)(dispatch);
5353

5454
return currentChannel;
5555
}

services/app/apps/codebattle/assets/js/widgets/middlewares/Lobby.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Gon from 'gon';
21
import { camelizeKeys } from 'humps';
32
import some from 'lodash/some';
43

@@ -9,21 +8,21 @@ import { calculateExpireDate } from '../utils/chatRoom';
98

109
import Channel from './Channel';
1110

12-
const channelName = 'lobby';
13-
const isRecord = Gon.getAsset('is_record');
14-
const channel = !isRecord ? new Channel(channelName) : null;
11+
const channel = new Channel();
1512

1613
export const fetchState = currentUserId => dispatch => {
17-
const currentChannel = channel;
14+
const channelName = 'lobby';
15+
channel.setupChannel(channelName);
16+
1817
const camelizeKeysAndDispatch = actionCreator => data => dispatch(actionCreator(camelizeKeys(data)));
1918

20-
currentChannel.join().receive('ok', camelizeKeysAndDispatch(actions.initGameList));
19+
channel.join().receive('ok', camelizeKeysAndDispatch(actions.initGameList));
2120

22-
currentChannel.onError(() => {
21+
channel.onError(() => {
2322
dispatch(actions.updateLobbyChannelState(false));
2423
});
2524

26-
currentChannel.onMessage((_event, payload) => camelizeKeys(payload));
25+
channel.onMessage((_event, payload) => camelizeKeys(payload));
2726

2827
const handleGameUpsert = data => {
2928
const {
@@ -50,7 +49,7 @@ export const fetchState = currentUserId => dispatch => {
5049
dispatch(actions.updateCheckResult(payload));
5150
};
5251

53-
return currentChannel
52+
return channel
5453
.addListener(channelTopics.lobbyGameUpsertTopic, handleGameUpsert)
5554
.addListener(channelTopics.lobbyGameCheckStartedTopic, handleGameCheckStarted)
5655
.addListener(

services/app/apps/codebattle/assets/js/widgets/middlewares/Room.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ import { addWaitingRoomListeners } from './WaitingRoom';
3636
const defaultLanguages = Gon.getAsset('langs');
3737
const gameId = Gon.getAsset('game_id');
3838
const isRecord = Gon.getAsset('is_record');
39-
let channel;
39+
const channel = new Channel();
4040

4141
export const setGameChannel = (newGameId = gameId) => {
4242
const newChannelName = `game:${newGameId}`;
43-
channel = !isRecord && newGameId ? new Channel(newChannelName) : null;
44-
console.log(channel);
45-
return channel;
43+
return channel.setupChannel(!isRecord && newGameId ? newChannelName : undefined);
4644
};
4745

4846
const initEditors = dispatch => (playbookStatusCode, players) => {
@@ -127,18 +125,18 @@ const initPlaybook = dispatch => data => {
127125
dispatch(actions.loadPlaybook(data));
128126
};
129127

130-
const initGameChannel = (gameRoomService, waitingRoomService, currentChannel) => dispatch => {
128+
const initGameChannel = (gameRoomService, waitingRoomService) => dispatch => {
131129
const onJoinFailure = payload => {
132130
gameRoomService.send('REJECT_LOADING_GAME', { payload });
133131
gameRoomService.send('FAILURE_JOIN', { payload });
134132
window.location.reload();
135133
};
136134

137-
currentChannel.onError(() => {
135+
channel.onError(() => {
138136
gameRoomService.send('FAILURE');
139137
});
140138

141-
currentChannel.onMessage((_event, payload) => camelizeKeys(payload));
139+
channel.onMessage((_event, payload) => camelizeKeys(payload));
142140

143141
const onJoinSuccess = response => {
144142
if (response.error) {
@@ -198,7 +196,7 @@ const initGameChannel = (gameRoomService, waitingRoomService, currentChannel) =>
198196
});
199197
};
200198

201-
currentChannel
199+
channel
202200
.join()
203201
.receive('ok', onJoinSuccess)
204202
.receive('error', onJoinFailure);
@@ -323,9 +321,7 @@ export const resetTextToTemplateAndSend = langSlug => (dispatch, getState) => {
323321
export const soundNotification = notification();
324322

325323
export const addCursorListeners = (userId, onChangePosition, onChangeSelection) => {
326-
const currentGameChannel = channel;
327-
328-
if (!userId || !currentGameChannel) {
324+
if (!userId) {
329325
return () => {};
330326
}
331327

@@ -345,7 +341,7 @@ export const addCursorListeners = (userId, onChangePosition, onChangeSelection)
345341

346342
const listenerParams = { userId };
347343

348-
currentGameChannel
344+
channel
349345
.addListener(
350346
channelTopics.editorCursorPositionTopic,
351347
handleNewCursorPosition,
@@ -358,13 +354,13 @@ export const addCursorListeners = (userId, onChangePosition, onChangeSelection)
358354
);
359355

360356
return () => {
361-
if (currentGameChannel) {
362-
currentGameChannel
363-
.clearListeners(
357+
if (channel) {
358+
channel
359+
.removeListeners(
364360
channelTopics.editorCursorPositionTopic,
365361
listenerParams,
366362
)
367-
.clearListeners(
363+
.removeListeners(
368364
channelTopics.editorCursorSelectionTopic,
369365
listenerParams,
370366
);
@@ -373,8 +369,6 @@ export const addCursorListeners = (userId, onChangePosition, onChangeSelection)
373369
};
374370

375371
export const activeEditorReady = (service, isBanned) => {
376-
console.log(channel);
377-
const currentGameChannel = channel;
378372
const listenerParams = { userId: service.machine.context.userId };
379373

380374
if (isBanned) {
@@ -396,27 +390,24 @@ export const activeEditorReady = (service, isBanned) => {
396390
service.send('receive_check_result', data);
397391
};
398392

399-
currentGameChannel
393+
channel
400394
.addListener(channelTopics.userStartCheckTopic, handleStartsCheck)
401395
.addListener(
402396
channelTopics.userCheckCompleteTopic,
403397
handleNewCheckResult,
404398
);
405399

406400
return () => {
407-
currentGameChannel
408-
.clearListeners(channelTopics.userStartCheckTopic, listenerParams)
409-
.clearListeners(channelTopics.userCheckCompleteTopic, listenerParams);
401+
channel
402+
.removeListeners(channelTopics.userStartCheckTopic, listenerParams)
403+
.removeListeners(channelTopics.userCheckCompleteTopic, listenerParams);
410404
};
411405
};
412406

413407
export const activeGameReady = (gameRoomService, waitingRoomService, { cancelRedirect = false }) => (dispatch, getState) => {
414-
const currentGameChannel = channel;
415-
416408
initGameChannel(
417409
gameRoomService,
418410
waitingRoomService,
419-
currentGameChannel,
420411
)(dispatch);
421412

422413
const handleNewEditorData = data => {
@@ -594,12 +585,12 @@ export const activeGameReady = (gameRoomService, waitingRoomService, { cancelRed
594585
};
595586

596587
addWaitingRoomListeners(
597-
currentGameChannel,
588+
channel,
598589
waitingRoomService,
599590
{ cancelRedirect },
600591
)(dispatch, getState);
601592

602-
return currentGameChannel
593+
return channel
603594
.addListener(channelTopics.editorDataTopic, handleNewEditorData)
604595
.addListener(
605596
channelTopics.userStartCheckTopic,

0 commit comments

Comments
 (0)