@@ -92,27 +92,24 @@ export const handleWebsocketCollabEvents = (socket: Socket) => {
92
92
}
93
93
) ;
94
94
95
- socket . on ( CollabEvents . LEAVE , ( uid : string , roomId : string ) => {
96
- const connectionKey = `${ uid } :${ roomId } ` ;
97
- if ( ! userConnections . has ( connectionKey ) ) {
98
- return ;
99
- }
100
-
101
- clearTimeout ( userConnections . get ( connectionKey ) ! ) ;
95
+ socket . on (
96
+ CollabEvents . LEAVE ,
97
+ ( uid : string , roomId : string , isImmediate : boolean ) => {
98
+ const connectionKey = `${ uid } :${ roomId } ` ;
99
+ if ( isImmediate || ! userConnections . has ( connectionKey ) ) {
100
+ handleUserLeave ( uid , roomId , socket ) ;
101
+ return ;
102
+ }
102
103
103
- const connectionTimeout = setTimeout ( ( ) => {
104
- userConnections . delete ( connectionKey ) ;
105
- socket . leave ( roomId ) ;
106
- socket . disconnect ( ) ;
104
+ clearTimeout ( userConnections . get ( connectionKey ) ! ) ;
107
105
108
- const room = io . sockets . adapter . rooms . get ( roomId ) ;
109
- if ( ! room || room . size === 0 ) {
110
- removeCollabSession ( roomId ) ;
111
- }
112
- } , CONNECTION_DELAY ) ;
106
+ const connectionTimeout = setTimeout ( ( ) => {
107
+ handleUserLeave ( uid , roomId , socket ) ;
108
+ } , CONNECTION_DELAY ) ;
113
109
114
- userConnections . set ( connectionKey , connectionTimeout ) ;
115
- } ) ;
110
+ userConnections . set ( connectionKey , connectionTimeout ) ;
111
+ }
112
+ ) ;
116
113
117
114
socket . on ( CollabEvents . RECONNECT_REQUEST , async ( roomId : string ) => {
118
115
// TODO: Handle recconnection
@@ -168,3 +165,19 @@ const saveDocument = async (roomId: string, doc: Doc) => {
168
165
EX : EXPIRY_TIME ,
169
166
} ) ;
170
167
} ;
168
+
169
+ const handleUserLeave = ( uid : string , roomId : string , socket : Socket ) => {
170
+ const connectionKey = `${ uid } :${ roomId } ` ;
171
+ if ( userConnections . has ( connectionKey ) ) {
172
+ clearTimeout ( userConnections . get ( connectionKey ) ! ) ;
173
+ userConnections . delete ( connectionKey ) ;
174
+ }
175
+
176
+ socket . leave ( roomId ) ;
177
+ socket . disconnect ( ) ;
178
+
179
+ const room = io . sockets . adapter . rooms . get ( roomId ) ;
180
+ if ( ! room || room . size === 0 ) {
181
+ removeCollabSession ( roomId ) ;
182
+ }
183
+ } ;
0 commit comments