@@ -775,39 +775,55 @@ class SocketClient {
775
775
this . socket . on ( 'version' , this . checkVersionEventHandler . bind ( this ) )
776
776
// received sync of online users request
777
777
this . socket . on ( 'online users' , this . onlineUsersEventHandler . bind ( this ) )
778
+ // reveiced when user logout or changed
779
+ this . socket . on ( 'user changed' , this . userChangedEventHandler . bind ( this ) )
778
780
}
779
781
780
- onlineUsersEventHandler ( ) {
781
- if ( ! this . isNoteAndUserExists ( ) ) return
782
-
783
- const currentNote = this . getCurrentNote ( )
784
-
785
- const currentNoteOnlineUserList = Object . keys ( currentNote . users )
786
- . map ( key => buildUserOutData ( currentNote . users [ key ] ) )
782
+ userChangedEventHandler ( ) {
783
+ logger . info ( 'user changed' )
787
784
788
- this . socket . emit ( 'online users' , {
789
- users : currentNoteOnlineUserList
790
- } )
791
- }
785
+ const note = this . getCurrentNote ( )
786
+ if ( ! note ) return
787
+ const user = note . users [ this . socket . id ]
788
+ if ( ! user ) return
792
789
793
- isNoteAndUserExists ( ) {
794
- const note = getNoteFromNotePool ( this . socket . noteId )
795
- const user = getUserFromUserPool ( this . socket . id )
796
- return note && user
790
+ exports . updateUserData ( this . socket , user )
791
+ exports . emitOnlineUsers ( this . socket )
797
792
}
798
793
799
794
getCurrentUser ( ) {
795
+ if ( ! this . socket . id ) return
800
796
return getUserFromUserPool ( this . socket . id )
801
797
}
802
798
803
799
getCurrentNote ( ) {
800
+ if ( ! this . socket . noteId ) return
804
801
return getNoteFromNotePool ( this . socket . noteId )
805
802
}
806
803
807
804
getNoteChannel ( ) {
808
805
return this . socket . broadcast . to ( this . socket . noteId )
809
806
}
810
807
808
+ isNoteAndUserExists ( ) {
809
+ const note = getNoteFromNotePool ( this . socket . noteId )
810
+ const user = getUserFromUserPool ( this . socket . id )
811
+ return note && user
812
+ }
813
+
814
+ onlineUsersEventHandler ( ) {
815
+ if ( ! this . isNoteAndUserExists ( ) ) return
816
+
817
+ const currentNote = this . getCurrentNote ( )
818
+
819
+ const currentNoteOnlineUserList = Object . keys ( currentNote . users )
820
+ . map ( key => buildUserOutData ( currentNote . users [ key ] ) )
821
+
822
+ this . socket . emit ( 'online users' , {
823
+ users : currentNoteOnlineUserList
824
+ } )
825
+ }
826
+
811
827
cursorFocusEventHandler ( data ) {
812
828
if ( ! this . isNoteAndUserExists ( ) ) return
813
829
const user = this . getCurrentUser ( )
@@ -998,17 +1014,6 @@ function connection (socket) {
998
1014
}
999
1015
}
1000
1016
} )
1001
-
1002
- // reveiced when user logout or changed
1003
- socket . on ( 'user changed' , function ( ) {
1004
- logger . info ( 'user changed' )
1005
- var noteId = socket . noteId
1006
- if ( ! noteId || ! notes [ noteId ] ) return
1007
- var user = notes [ noteId ] . users [ socket . id ]
1008
- if ( ! user ) return
1009
- updateUserData ( socket , user )
1010
- emitOnlineUsers ( socket )
1011
- } )
1012
1017
}
1013
1018
1014
1019
exports = module . exports = realtime
@@ -1023,6 +1028,7 @@ exports.startConnection = startConnection
1023
1028
exports . emitRefresh = emitRefresh
1024
1029
exports . emitUserStatus = emitUserStatus
1025
1030
exports . disconnect = disconnect
1031
+ exports . emitOnlineUsers = emitOnlineUsers
1026
1032
exports . notes = notes
1027
1033
exports . users = users
1028
1034
exports . disconnectSocketQueue = disconnectSocketQueue
0 commit comments