Skip to content

Commit a84a4fc

Browse files
committed
rewrite save queue, now for content and autotitle
1 parent c6762eb commit a84a4fc

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

src/NotesService.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

261261
export 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

361377
export 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

366383
export const noteExists = (noteId) => {

src/components/Note.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import SyncAlertIcon from 'vue-material-design-icons/SyncAlert'
9999
import PencilOffIcon from 'vue-material-design-icons/PencilOff'
100100
101101
import { config } from '../config'
102-
import { fetchNote, refreshNote, saveNote, saveNoteManually, autotitleNote, conflictSolutionLocal, conflictSolutionRemote } from '../NotesService'
102+
import { fetchNote, refreshNote, saveNoteManually, queueCommand, conflictSolutionLocal, conflictSolutionRemote } from '../NotesService'
103103
import { routeIsNewNote } from '../Util'
104104
import TheEditor from './EditorEasyMDE'
105105
import ThePreview from './EditorMarkdownIt'
@@ -334,7 +334,7 @@ export default {
334334
if (this.autosaveTimer === null) {
335335
this.autosaveTimer = setTimeout(() => {
336336
this.autosaveTimer = null
337-
saveNote(note.id)
337+
queueCommand(note.id, 'content')
338338
}, config.interval.note.autosave * 1000)
339339
}
340340
@@ -352,7 +352,7 @@ export default {
352352
this.autotitleTimer = setTimeout(() => {
353353
this.autotitleTimer = null
354354
if (this.isNewNote) {
355-
autotitleNote(note.id)
355+
queueCommand(note.id, 'autotitle')
356356
}
357357
}, config.interval.note.autotitle * 1000)
358358
}

src/store/notes.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const state = {
55
categories: [],
66
notes: [],
77
notesIds: {},
8-
unsaved: {},
98
}
109

1110
const getters = {
@@ -109,14 +108,6 @@ const mutations = {
109108
state.notesIds = {}
110109
},
111110

112-
addUnsaved(state, id) {
113-
Vue.set(state.unsaved, id, state.notesIds[id])
114-
},
115-
116-
clearUnsaved(state) {
117-
state.unsaved = {}
118-
},
119-
120111
setCategories(state, categories) {
121112
state.categories = categories
122113
},

src/store/sync.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import Vue from 'vue'
2+
13
const state = {
4+
queue: {},
25
etag: null,
36
lastModified: 0,
47
active: false,
@@ -9,6 +12,16 @@ const getters = {
912
}
1013

1114
const mutations = {
15+
addToQueue(state, { noteId, type }) {
16+
const cmd = { noteId, type }
17+
const key = noteId + '-' + type
18+
Vue.set(state.queue, key, cmd)
19+
},
20+
21+
clearQueue(state) {
22+
state.queue = {}
23+
},
24+
1225
setSyncETag(state, etag) {
1326
if (etag) {
1427
state.etag = etag

0 commit comments

Comments
 (0)