File tree Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,10 @@ io.on('connection', (socket) => {
39
39
socket . on ( 'open-text-editor' , data => {
40
40
socket . broadcast . emit ( "open-text-editor" , data ) ;
41
41
} )
42
+
43
+ socket . on ( 'close-text-editor' , data => {
44
+ socket . broadcast . emit ( "close-text-editor" , data ) ;
45
+ } )
42
46
} )
43
47
44
48
server . listen ( PORT , ( ) => {
Original file line number Diff line number Diff line change @@ -69,9 +69,8 @@ function Menu(){
69
69
} , [ ] )
70
70
71
71
useEffect ( ( ) => {
72
- // PRB: socket is listening but the function is not getting executed
73
72
socket . on ( "open-text-editor" , ( data ) => {
74
- handleOpenTextEditor ( true ) ; // PRB: infinite loop taking place
73
+ handleOpenTextEditor ( true ) ;
75
74
} ) ;
76
75
77
76
return ( ) => {
Original file line number Diff line number Diff line change 1
1
import { useSetRecoilState , useRecoilValue , useRecoilState } from "recoil" ;
2
2
import { showTextEditor , docState , textEditorInput } from "../atoms" ;
3
+ import socket from "../socket" ;
4
+ import { useCallback , useEffect , useRef } from "react" ;
3
5
4
6
function TextEditor ( ) {
5
7
const setTextEditorFalse = useSetRecoilState ( showTextEditor ) ;
6
8
const doc = useRecoilValue ( docState ) ;
7
9
const text = useRecoilValue ( textEditorInput ) ;
8
10
const [ input , setInput ] = useRecoilState ( textEditorInput )
11
+ const isRendering = useRef ( false ) ;
9
12
10
13
function removeTextEditor ( ) {
14
+ if ( ! isRendering . current ) {
15
+ socket . emit ( "close-text-editor" ) ;
16
+ }
11
17
setTextEditorFalse ( false ) ;
12
18
}
13
19
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
+
14
35
function handleChange ( event ) {
15
36
setInput ( event . target . value ) ;
16
37
if ( ! doc ) return ;
You can’t perform that action at this time.
0 commit comments