Skip to content

Commit 03dfaa2

Browse files
committed
Close text-editor when one does it
1 parent 44c3823 commit 03dfaa2

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

api/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ io.on('connection', (socket) => {
3939
socket.on('open-text-editor', data => {
4040
socket.broadcast.emit("open-text-editor", data);
4141
})
42+
43+
socket.on('close-text-editor', data => {
44+
socket.broadcast.emit("close-text-editor", data);
45+
})
4246
})
4347

4448
server.listen(PORT, ()=>{

client/src/components/Menu.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ function Menu(){
6969
}, [])
7070

7171
useEffect(() => {
72-
// PRB: socket is listening but the function is not getting executed
7372
socket.on("open-text-editor", (data) => {
74-
handleOpenTextEditor(true); // PRB: infinite loop taking place
73+
handleOpenTextEditor(true);
7574
});
7675

7776
return () => {

client/src/components/TextEditor.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
import { useSetRecoilState, useRecoilValue, useRecoilState } from "recoil";
22
import { showTextEditor, docState, textEditorInput } from "../atoms";
3+
import socket from "../socket";
4+
import { useCallback, useEffect, useRef } from "react";
35

46
function TextEditor() {
57
const setTextEditorFalse = useSetRecoilState(showTextEditor);
68
const doc = useRecoilValue(docState);
79
const text = useRecoilValue(textEditorInput);
810
const [input, setInput] = useRecoilState(textEditorInput)
11+
const isRendering = useRef(false);
912

1013
function removeTextEditor() {
14+
if (!isRendering.current) {
15+
socket.emit("close-text-editor");
16+
}
1117
setTextEditorFalse(false);
1218
}
1319

20+
const handleRemoveTextEditor = useCallback((rendering) => {
21+
isRendering.current = rendering;
22+
removeTextEditor();
23+
}, []);
24+
25+
useEffect(() => {
26+
socket.on("close-text-editor", () => {
27+
handleRemoveTextEditor(true);
28+
});
29+
30+
return () => {
31+
socket.off("close-text-editor", handleRemoveTextEditor);
32+
};
33+
}, []);
34+
1435
function handleChange(event) {
1536
setInput(event.target.value);
1637
if(!doc) return;

0 commit comments

Comments
 (0)