@@ -763,22 +763,65 @@ class SocketClient {
763
763
this . socket . on ( 'refresh' , this . refreshEventHandler . bind ( this ) )
764
764
// received user status
765
765
this . socket . on ( 'user status' , this . userStatusEventHandler . bind ( this ) )
766
-
767
766
// when a new client disconnect
768
767
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
+ }
769
781
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
+ } )
770
813
}
771
814
772
815
refreshEventHandler ( ) {
773
816
exports . emitRefresh ( this . socket )
774
817
}
775
818
776
819
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
+ }
782
825
if ( data ) {
783
826
user . idle = data . idle
784
827
user . type = data . type
@@ -946,7 +989,9 @@ function connection (socket) {
946
989
var users = [ ]
947
990
Object . keys ( notes [ noteId ] . users ) . forEach ( function ( key ) {
948
991
var user = notes [ noteId ] . users [ key ]
949
- if ( user ) { users . push ( buildUserOutData ( user ) ) }
992
+ if ( user ) {
993
+ users . push ( buildUserOutData ( user ) )
994
+ }
950
995
} )
951
996
var out = {
952
997
users : users
@@ -961,38 +1006,6 @@ function connection (socket) {
961
1006
minimumCompatibleVersion : config . minimumCompatibleVersion
962
1007
} )
963
1008
} )
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
- } )
996
1009
}
997
1010
998
1011
exports = module . exports = realtime
0 commit comments