@@ -763,22 +763,65 @@ class SocketClient {
763763 this . socket . on ( 'refresh' , this . refreshEventHandler . bind ( this ) )
764764 // received user status
765765 this . socket . on ( 'user status' , this . userStatusEventHandler . bind ( this ) )
766-
767766 // when a new client disconnect
768767 this . socket . on ( 'disconnect' , this . disconnectEventHandler . bind ( this ) )
768+ // received cursor focus
769+ this . socket . on ( 'cursor focus' , this . cursorFocusEventHandler . bind ( this ) )
770+ // received cursor activity
771+ this . socket . on ( 'cursor activity' , this . cursorActivityEventHandler . bind ( this ) )
772+ // received cursor blur
773+ this . socket . on ( 'cursor blur' , this . cursorBlurEventHandlder . bind ( this ) )
774+ }
775+
776+ isNoteAndUserExists ( ) {
777+ const note = getNoteFromNotePool ( this . socket . noteId )
778+ const user = getUserFromUserPool ( this . socket . id )
779+ return note && user
780+ }
769781
782+ getCurrentUser ( ) {
783+ return getUserFromUserPool ( this . socket . id )
784+ }
785+
786+ getNoteChannel ( ) {
787+ return this . socket . broadcast . to ( this . socket . noteId )
788+ }
789+
790+ cursorFocusEventHandler ( data ) {
791+ if ( ! this . isNoteAndUserExists ( ) ) return
792+ const user = this . getCurrentUser ( )
793+ user . cursor = data
794+ const out = buildUserOutData ( user )
795+ this . getNoteChannel ( ) . emit ( 'cursor focus' , out )
796+ }
797+
798+ cursorActivityEventHandler ( data ) {
799+ if ( ! this . isNoteAndUserExists ( ) ) return
800+ const user = this . getCurrentUser ( )
801+ user . cursor = data
802+ const out = buildUserOutData ( user )
803+ this . getNoteChannel ( ) . emit ( 'cursor activity' , out )
804+ }
805+
806+ cursorBlurEventHandlder ( ) {
807+ if ( ! this . isNoteAndUserExists ( ) ) return
808+ const user = this . getCurrentUser ( )
809+ user . cursor = null
810+ this . getNoteChannel ( ) . emit ( 'cursor blur' , {
811+ id : this . socket . id
812+ } )
770813 }
771814
772815 refreshEventHandler ( ) {
773816 exports . emitRefresh ( this . socket )
774817 }
775818
776819 userStatusEventHandler ( data ) {
777- const noteId = this . socket . noteId
778- const user = getUserFromUserPool ( this . socket . id )
779- if ( ! noteId || ! getNoteFromNotePool ( noteId ) || ! user ) return
780-
781- if ( config . debug ) { logger . info ( 'SERVER received [' + noteId + '] user status from [' + this . socket . id + ']: ' + JSON . stringify ( data ) ) }
820+ if ( ! this . isNoteAndUserExists ( ) ) return
821+ const user = this . getCurrentUser ( )
822+ if ( config . debug ) {
823+ logger . info ( 'SERVER received [' + this . socket . noteId + '] user status from [' + this . socket . id + ']: ' + JSON . stringify ( data ) )
824+ }
782825 if ( data ) {
783826 user . idle = data . idle
784827 user . type = data . type
@@ -946,7 +989,9 @@ function connection (socket) {
946989 var users = [ ]
947990 Object . keys ( notes [ noteId ] . users ) . forEach ( function ( key ) {
948991 var user = notes [ noteId ] . users [ key ]
949- if ( user ) { users . push ( buildUserOutData ( user ) ) }
992+ if ( user ) {
993+ users . push ( buildUserOutData ( user ) )
994+ }
950995 } )
951996 var out = {
952997 users : users
@@ -961,38 +1006,6 @@ function connection (socket) {
9611006 minimumCompatibleVersion : config . minimumCompatibleVersion
9621007 } )
9631008 } )
964-
965- // received cursor focus
966- socket . on ( 'cursor focus' , function ( data ) {
967- var noteId = socket . noteId
968- var user = users [ socket . id ]
969- if ( ! noteId || ! notes [ noteId ] || ! user ) return
970- user . cursor = data
971- var out = buildUserOutData ( user )
972- socket . broadcast . to ( noteId ) . emit ( 'cursor focus' , out )
973- } )
974-
975- // received cursor activity
976- socket . on ( 'cursor activity' , function ( data ) {
977- var noteId = socket . noteId
978- var user = users [ socket . id ]
979- if ( ! noteId || ! notes [ noteId ] || ! user ) return
980- user . cursor = data
981- var out = buildUserOutData ( user )
982- socket . broadcast . to ( noteId ) . emit ( 'cursor activity' , out )
983- } )
984-
985- // received cursor blur
986- socket . on ( 'cursor blur' , function ( ) {
987- var noteId = socket . noteId
988- var user = users [ socket . id ]
989- if ( ! noteId || ! notes [ noteId ] || ! user ) return
990- user . cursor = null
991- var out = {
992- id : socket . id
993- }
994- socket . broadcast . to ( noteId ) . emit ( 'cursor blur' , out )
995- } )
9961009}
9971010
9981011exports = module . exports = realtime
0 commit comments