@@ -235,7 +235,7 @@ function _updateNote(note) {
235235 copyNote ( remote , note , [ 'content' ] ) ,
236236 copyNote ( remote , { } )
237237 )
238- saveNote ( note . id )
238+ queueCommand ( note . id , 'content' )
239239 } else {
240240 console . info ( 'Note update conflict. Manual resolution required.' )
241241 store . commit ( 'setNoteAttribute' , { noteId : note . id , attribute : 'conflict' , value : remote } )
@@ -255,7 +255,7 @@ export const conflictSolutionLocal = note => {
255255 copyNote ( note . conflict , { } )
256256 )
257257 store . commit ( 'setNoteAttribute' , { noteId : note . id , attribute : 'conflict' , value : undefined } )
258- saveNote ( note . id )
258+ queueCommand ( note . id , 'content' )
259259}
260260
261261export const conflictSolutionRemote = note => {
@@ -335,32 +335,49 @@ export const setCategory = (noteId, category) => {
335335 } )
336336}
337337
338- export const saveNote = ( noteId , manualSave = false ) => {
339- store . commit ( 'addUnsaved' , noteId )
340- if ( manualSave ) {
341- store . commit ( 'setManualSave' , true )
342- }
343- _saveNotes ( )
338+ export const queueCommand = ( noteId , type ) => {
339+ store . commit ( 'addToQueue' , { noteId, type } )
340+ _processQueue ( )
344341}
345342
346- function _saveNotes ( ) {
347- const unsavedNotes = Object . values ( store . state . notes . unsaved )
348- if ( store . state . app . isSaving || unsavedNotes . length === 0 ) {
343+ function _processQueue ( ) {
344+ const queue = Object . values ( store . state . sync . queue )
345+ if ( store . state . app . isSaving || queue . length === 0 ) {
349346 return
350347 }
351348 store . commit ( 'setSaving' , true )
352- const promises = unsavedNotes . map ( note => _updateNote ( note ) )
353- store . commit ( 'clearUnsaved' )
354- Promise . all ( promises ) . then ( ( ) => {
349+ store . commit ( 'clearQueue' )
350+
351+ async function _executeQueueCommands ( ) {
352+ for ( const cmd of queue ) {
353+ try {
354+ switch ( cmd . type ) {
355+ case 'content' :
356+ await _updateNote ( store . state . notes . notesIds [ cmd . noteId ] )
357+ break
358+ case 'autotitle' :
359+ await autotitleNote ( cmd . noteId )
360+ break
361+ default :
362+ console . error ( 'Unknown queue command: ' + cmd . type )
363+ }
364+
365+ } catch ( e ) {
366+ console . error ( 'Command has failed with error:' )
367+ console . error ( e )
368+ }
369+ }
355370 store . commit ( 'setSaving' , false )
356371 store . commit ( 'setManualSave' , false )
357- _saveNotes ( )
358- } )
372+ _processQueue ( )
373+ }
374+ _executeQueueCommands ( )
359375}
360376
361377export const saveNoteManually = ( noteId ) => {
362378 store . commit ( 'setNoteAttribute' , { noteId, attribute : 'saveError' , value : false } )
363- saveNote ( noteId , true )
379+ store . commit ( 'setManualSave' , true )
380+ queueCommand ( noteId , 'content' )
364381}
365382
366383export const noteExists = ( noteId ) => {
0 commit comments