@@ -104,6 +104,8 @@ qx.Class.define("osparc.desktop.StudyEditor", {
104104 } ,
105105
106106 statics : {
107+ AUTO_SAVE_INTERVAL : 3000 ,
108+ DIFF_CHECK_INTERVAL : 300 ,
107109 READ_ONLY_TEXT : qx . locale . Manager . tr ( "You do not have writing permissions.<br>Your changes will not be saved." ) ,
108110 } ,
109111
@@ -112,6 +114,8 @@ qx.Class.define("osparc.desktop.StudyEditor", {
112114 __viewsStack : null ,
113115 __workbenchView : null ,
114116 __slideshowView : null ,
117+ __autoSaveTimer : null ,
118+ __savingTimer : null ,
115119 __studyEditorIdlingTracker : null ,
116120 __studyDataInBackend : null ,
117121 __updatingStudy : null ,
@@ -220,7 +224,10 @@ qx.Class.define("osparc.desktop.StudyEditor", {
220224 }
221225 }
222226
223- if ( ! osparc . data . model . Study . canIWrite ( study . getAccessRights ( ) ) ) {
227+ if ( osparc . data . model . Study . canIWrite ( study . getAccessRights ( ) ) ) {
228+ this . __startAutoSaveTimer ( ) ;
229+ this . __startSavingTimer ( ) ;
230+ } else {
224231 const msg = this . self ( ) . READ_ONLY_TEXT ;
225232 osparc . FlashMessenger . logAs ( msg , "WARNING" ) ;
226233 }
@@ -241,6 +248,10 @@ qx.Class.define("osparc.desktop.StudyEditor", {
241248 const nodeId = e . getData ( ) ;
242249 this . nodeSelected ( nodeId ) ;
243250 } , this ) ;
251+
252+ // listener already added in __applyStudy
253+ // workbench.addListener("updateStudyDocument", () => this.updateStudyDocument());
254+ workbench . addListener ( "restartAutoSaveTimer" , ( ) => this . __restartAutoSaveTimer ( ) ) ;
244255 } ,
245256
246257 __setStudyDataInBackend : function ( studyData ) {
@@ -793,8 +804,57 @@ qx.Class.define("osparc.desktop.StudyEditor", {
793804 } ,
794805 // ------------------ IDLING TRACKER ------------------
795806
807+ // ------------------ AUTO SAVER ------------------
808+ __startAutoSaveTimer : function ( ) {
809+ // Save every 3 seconds
810+ const timer = this . __autoSaveTimer = new qx . event . Timer ( this . self ( ) . AUTO_SAVE_INTERVAL ) ;
811+ timer . addListener ( "interval" , ( ) => {
812+ if ( ! osparc . wrapper . WebSocket . getInstance ( ) . isConnected ( ) ) {
813+ return ;
814+ }
815+ this . __checkStudyChanges ( ) ;
816+ } , this ) ;
817+ timer . start ( ) ;
818+ } ,
819+
820+ __stopAutoSaveTimer : function ( ) {
821+ if ( this . __autoSaveTimer && this . __autoSaveTimer . isEnabled ( ) ) {
822+ this . __autoSaveTimer . stop ( ) ;
823+ this . __autoSaveTimer . setEnabled ( false ) ;
824+ }
825+ } ,
826+
827+ __restartAutoSaveTimer : function ( ) {
828+ if ( this . __autoSaveTimer && this . __autoSaveTimer . isEnabled ( ) ) {
829+ this . __autoSaveTimer . restart ( ) ;
830+ }
831+ } ,
832+ // ------------------ AUTO SAVER ------------------
833+
834+ // ---------------- SAVING TIMER ------------------
835+ __startSavingTimer : function ( ) {
836+ const timer = this . __savingTimer = new qx . event . Timer ( this . self ( ) . DIFF_CHECK_INTERVAL ) ;
837+ timer . addListener ( "interval" , ( ) => {
838+ if ( ! osparc . wrapper . WebSocket . getInstance ( ) . isConnected ( ) ) {
839+ return ;
840+ }
841+ this . getStudy ( ) . setSavePending ( this . didStudyChange ( ) ) ;
842+ } , this ) ;
843+ timer . start ( ) ;
844+ } ,
845+
846+ __stopSavingTimer : function ( ) {
847+ if ( this . __savingTimer && this . __savingTimer . isEnabled ( ) ) {
848+ this . __savingTimer . stop ( ) ;
849+ this . __savingTimer . setEnabled ( false ) ;
850+ }
851+ } ,
852+ // ---------------- SAVING TIMER ------------------
853+
796854 __stopTimers : function ( ) {
797855 this . __stopIdlingTracker ( ) ;
856+ this . __stopAutoSaveTimer ( ) ;
857+ this . __stopSavingTimer ( ) ;
798858 } ,
799859
800860 __getStudyDiffs : function ( ) {
@@ -813,7 +873,7 @@ qx.Class.define("osparc.desktop.StudyEditor", {
813873 return studyDiffs ;
814874 } ,
815875
816- // this takes around 0.5ms
876+ // didStudyChange takes around 0.5ms
817877 didStudyChange : function ( ) {
818878 const studyDiffs = this . __getStudyDiffs ( ) ;
819879 const changed = Boolean ( Object . keys ( studyDiffs . delta ) . length ) ;
0 commit comments