@@ -86,7 +86,7 @@ setInterval(function () {
8686 if ( note . server . isDirty ) {
8787 if ( config . debug ) logger . info ( 'updater found dirty note: ' + key )
8888 note . server . isDirty = false
89- updateNote ( note , function ( err , _note ) {
89+ exports . updateNote ( note , function ( err , _note ) {
9090 // handle when note already been clean up
9191 if ( ! notes [ key ] || ! notes [ key ] . server ) return callback ( null , null )
9292 if ( ! _note ) {
@@ -209,7 +209,7 @@ setInterval(function () {
209209 saverSleep = true
210210 }
211211 } )
212- } , 60000 * 5 )
212+ } , 5 * 60 * 1000 ) // 5 mins
213213
214214function getStatus ( callback ) {
215215 models . Note . count ( ) . then ( function ( notecount ) {
@@ -737,14 +737,60 @@ function updateHistory (userId, note, time) {
737737 if ( note . server ) history . updateHistory ( userId , noteId , note . server . document , time )
738738}
739739
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+
740786function connection ( socket ) {
741787 if ( realtime . maintenance ) return
742- parseNoteIdFromSocket ( socket , function ( err , noteId ) {
788+ exports . parseNoteIdFromSocket ( socket , function ( err , noteId ) {
743789 if ( err ) {
744- return failConnection ( 500 , err , socket )
790+ return exports . failConnection ( 500 , err , socket )
745791 }
746792 if ( ! noteId ) {
747- return failConnection ( 404 , 'note id not found' , socket )
793+ return exports . failConnection ( 404 , 'note id not found' , socket )
748794 }
749795
750796 if ( isDuplicatedInSocketQueue ( connectionSocketQueue , socket ) ) return
@@ -785,30 +831,15 @@ function connection (socket) {
785831 idle : false ,
786832 type : null
787833 }
788- updateUserData ( socket , users [ socket . id ] )
834+ exports . updateUserData ( socket , users [ socket . id ] )
789835
790836 // start connection
791837 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 )
798839 } )
799840
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 ( )
812843
813844 // received note permission change request
814845 socket . on ( 'permission' , function ( permission ) {
@@ -963,3 +994,14 @@ function connection (socket) {
963994
964995exports = module . exports = realtime
965996exports . 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