@@ -55,6 +55,7 @@ define([
5555 this . ping = false ; // ping
5656 this . participants = [ ] ; // participants list
5757 this . syncState = false ; // sync connexion state
58+ this . modified = false ;
5859
5960 // Add timer for ping
6061 this . timer = setInterval ( _ . bind ( this . _intervalPing , this ) , 15 * 1000 ) ;
@@ -222,7 +223,7 @@ define([
222223 logging . log ( "socket connecting ..." ) ;
223224 } ) ;
224225 socket . on ( 'message' , function ( data ) {
225- logging . log ( "socket editor packet " , data ) ;
226+ logging . log ( "socket receive packet " , data ) ;
226227 self . ping = true ;
227228 if ( data . action == null || data . path == null || self . file . path ( ) != data . path ) {
228229 return ;
@@ -252,12 +253,20 @@ define([
252253 if ( data . participants != null ) {
253254 self . setParticipants ( data . participants )
254255 }
256+ if ( data . state != null ) {
257+ self . modifiedState ( data . state ) ;
258+ }
255259 break ;
256260 case "patch" :
257261 if ( data . patch != null ) {
258262 self . patchContent ( data ) ;
259263 }
260264 break ;
265+ case "modified" :
266+ if ( data . state != null ) {
267+ self . modifiedState ( data . state ) ;
268+ }
269+ break ;
261270 }
262271 self . setSyncState ( true ) ;
263272 } ) ;
@@ -267,6 +276,15 @@ define([
267276 }
268277 } ,
269278
279+ /*
280+ * Set modified (and not saved) state
281+ */
282+ modifiedState : function ( state ) {
283+ if ( this . modified == state ) return ;
284+ this . modified = state ;
285+ this . trigger ( "sync:modified" , this . modified ) ;
286+ } ,
287+
270288 /*
271289 * Set file to the editor
272290 */
@@ -290,8 +308,14 @@ define([
290308
291309 /* Socket for the connexion */
292310 socket : function ( ) {
311+ var that = this ;
312+
313+ if ( this . _socket ) return Q ( this . _socket ) ;
293314 if ( this . file != null ) {
294- return this . file . codebox . socket ( "filesync" ) ;
315+ return this . file . codebox . socket ( "filesync" , true ) . then ( function ( s ) {
316+ that . _socket = s ;
317+ return that . _socket ;
318+ } )
295319 } else {
296320 throw new Error ( "need 'file' to create sync socket" ) ;
297321 }
@@ -486,7 +510,7 @@ define([
486510 this . participants = _ . compact ( _ . map ( parts , function ( participant , i ) {
487511 participant . user = collaborators . getById ( participant . userId ) ;
488512 if ( ! participant . user ) {
489- logger . error ( "participant non user:" , participant . userId ) ;
513+ logging . error ( "participant non user:" , participant . userId ) ;
490514 return null ;
491515 }
492516 participant . color = this . options . colors [ i % this . options . colors . length ] ;
@@ -522,6 +546,7 @@ define([
522546 'path' : this . file . path ( )
523547 } ) ;
524548
549+ logging . log ( "send packet" , data ) ;
525550 this . socket ( ) . then ( function ( socket ) {
526551 socket . json . send ( data ) ;
527552 } )
@@ -602,11 +627,19 @@ define([
602627 return true ;
603628 } ,
604629
630+ /*
631+ * Save the file
632+ */
633+ save : function ( ) {
634+ return this . send ( "save" ) ;
635+ } ,
636+
605637 /*
606638 * Close the connexion
607639 */
608640 close : function ( ) {
609- return this . send ( "close" ) ;
641+ this . send ( "close" ) ;
642+ this . off ( ) ;
610643 } ,
611644 } ) ;
612645
0 commit comments