@@ -86,7 +86,7 @@ setInterval(function () {
86
86
if ( note . server . isDirty ) {
87
87
if ( config . debug ) logger . info ( 'updater found dirty note: ' + key )
88
88
note . server . isDirty = false
89
- updateNote ( note , function ( err , _note ) {
89
+ exports . updateNote ( note , function ( err , _note ) {
90
90
// handle when note already been clean up
91
91
if ( ! notes [ key ] || ! notes [ key ] . server ) return callback ( null , null )
92
92
if ( ! _note ) {
@@ -209,7 +209,7 @@ setInterval(function () {
209
209
saverSleep = true
210
210
}
211
211
} )
212
- } , 60000 * 5 )
212
+ } , 5 * 60 * 1000 ) // 5 mins
213
213
214
214
function getStatus ( callback ) {
215
215
models . Note . count ( ) . then ( function ( notecount ) {
@@ -737,14 +737,60 @@ function updateHistory (userId, note, time) {
737
737
if ( note . server ) history . updateHistory ( userId , noteId , note . server . document , time )
738
738
}
739
739
740
+ function getUserPool ( ) {
741
+ return users
742
+ }
743
+
744
+ function getUserFromUserPool ( userId ) {
745
+ return users [ userId ]
746
+ }
747
+
748
+ function getNotePool ( ) {
749
+ return notes
750
+ }
751
+
752
+ function getNoteFromNotePool ( noteId ) {
753
+ return notes [ noteId ]
754
+ }
755
+
756
+ class SocketClient {
757
+ constructor ( socket ) {
758
+ this . socket = socket
759
+ }
760
+
761
+ registerEventHandler ( ) {
762
+ // received client refresh request
763
+ this . socket . on ( 'refresh' , this . refreshEventHandler . bind ( this ) )
764
+ // received user status
765
+ this . socket . on ( 'user status' , this . userStatusEventHandler . bind ( this ) )
766
+ }
767
+
768
+ refreshEventHandler ( ) {
769
+ exports . emitRefresh ( this . socket )
770
+ }
771
+
772
+ userStatusEventHandler ( data ) {
773
+ const noteId = this . socket . noteId
774
+ const user = getUserFromUserPool ( this . socket . id )
775
+ if ( ! noteId || ! getNoteFromNotePool ( noteId ) || ! user ) return
776
+
777
+ if ( config . debug ) { logger . info ( 'SERVER received [' + noteId + '] user status from [' + this . socket . id + ']: ' + JSON . stringify ( data ) ) }
778
+ if ( data ) {
779
+ user . idle = data . idle
780
+ user . type = data . type
781
+ }
782
+ exports . emitUserStatus ( this . socket )
783
+ }
784
+ }
785
+
740
786
function connection ( socket ) {
741
787
if ( realtime . maintenance ) return
742
- parseNoteIdFromSocket ( socket , function ( err , noteId ) {
788
+ exports . parseNoteIdFromSocket ( socket , function ( err , noteId ) {
743
789
if ( err ) {
744
- return failConnection ( 500 , err , socket )
790
+ return exports . failConnection ( 500 , err , socket )
745
791
}
746
792
if ( ! noteId ) {
747
- return failConnection ( 404 , 'note id not found' , socket )
793
+ return exports . failConnection ( 404 , 'note id not found' , socket )
748
794
}
749
795
750
796
if ( isDuplicatedInSocketQueue ( connectionSocketQueue , socket ) ) return
@@ -785,30 +831,15 @@ function connection (socket) {
785
831
idle : false ,
786
832
type : null
787
833
}
788
- updateUserData ( socket , users [ socket . id ] )
834
+ exports . updateUserData ( socket , users [ socket . id ] )
789
835
790
836
// start connection
791
837
connectionSocketQueue . push ( socket )
792
- startConnection ( socket )
793
- } )
794
-
795
- // received client refresh request
796
- socket . on ( 'refresh' , function ( ) {
797
- emitRefresh ( socket )
838
+ exports . startConnection ( socket )
798
839
} )
799
840
800
- // received user status
801
- socket . on ( 'user status' , function ( data ) {
802
- var noteId = socket . noteId
803
- var user = users [ socket . id ]
804
- if ( ! noteId || ! notes [ noteId ] || ! user ) return
805
- if ( config . debug ) { logger . info ( 'SERVER received [' + noteId + '] user status from [' + socket . id + ']: ' + JSON . stringify ( data ) ) }
806
- if ( data ) {
807
- user . idle = data . idle
808
- user . type = data . type
809
- }
810
- emitUserStatus ( socket )
811
- } )
841
+ const socketClient = new SocketClient ( socket )
842
+ socketClient . registerEventHandler ( )
812
843
813
844
// received note permission change request
814
845
socket . on ( 'permission' , function ( permission ) {
@@ -963,3 +994,14 @@ function connection (socket) {
963
994
964
995
exports = module . exports = realtime
965
996
exports . extractNoteIdFromSocket = extractNoteIdFromSocket
997
+ exports . parseNoteIdFromSocket = parseNoteIdFromSocket
998
+ exports . updateNote = updateNote
999
+ exports . finishUpdateNote = finishUpdateNote
1000
+ exports . failConnection = failConnection
1001
+ exports . isDuplicatedInSocketQueue = isDuplicatedInSocketQueue
1002
+ exports . updateUserData = updateUserData
1003
+ exports . startConnection = startConnection
1004
+ exports . emitRefresh = emitRefresh
1005
+ exports . emitUserStatus = emitUserStatus
1006
+ exports . notes = notes
1007
+ exports . users = users
0 commit comments