rewrite save queue, now for content and autotitle

This commit is contained in:
korelstar 2021-07-31 11:42:23 +02:00
parent c6762ebd90
commit a84a4fcc2b
4 changed files with 50 additions and 29 deletions

View File

@ -235,7 +235,7 @@ function _updateNote(note) {
copyNote(remote, note, ['content']),
copyNote(remote, {})
)
saveNote(note.id)
queueCommand(note.id, 'content')
} else {
console.info('Note update conflict. Manual resolution required.')
store.commit('setNoteAttribute', { noteId: note.id, attribute: 'conflict', value: remote })
@ -255,7 +255,7 @@ export const conflictSolutionLocal = note => {
copyNote(note.conflict, {})
)
store.commit('setNoteAttribute', { noteId: note.id, attribute: 'conflict', value: undefined })
saveNote(note.id)
queueCommand(note.id, 'content')
}
export const conflictSolutionRemote = note => {
@ -335,32 +335,49 @@ export const setCategory = (noteId, category) => {
})
}
export const saveNote = (noteId, manualSave = false) => {
store.commit('addUnsaved', noteId)
if (manualSave) {
store.commit('setManualSave', true)
}
_saveNotes()
export const queueCommand = (noteId, type) => {
store.commit('addToQueue', { noteId, type })
_processQueue()
}
function _saveNotes() {
const unsavedNotes = Object.values(store.state.notes.unsaved)
if (store.state.app.isSaving || unsavedNotes.length === 0) {
function _processQueue() {
const queue = Object.values(store.state.sync.queue)
if (store.state.app.isSaving || queue.length === 0) {
return
}
store.commit('setSaving', true)
const promises = unsavedNotes.map(note => _updateNote(note))
store.commit('clearUnsaved')
Promise.all(promises).then(() => {
store.commit('clearQueue')
async function _executeQueueCommands() {
for (const cmd of queue) {
try {
switch (cmd.type) {
case 'content':
await _updateNote(store.state.notes.notesIds[cmd.noteId])
break
case 'autotitle':
await autotitleNote(cmd.noteId)
break
default:
console.error('Unknown queue command: ' + cmd.type)
}
} catch (e) {
console.error('Command has failed with error:')
console.error(e)
}
}
store.commit('setSaving', false)
store.commit('setManualSave', false)
_saveNotes()
})
_processQueue()
}
_executeQueueCommands()
}
export const saveNoteManually = (noteId) => {
store.commit('setNoteAttribute', { noteId, attribute: 'saveError', value: false })
saveNote(noteId, true)
store.commit('setManualSave', true)
queueCommand(noteId, 'content')
}
export const noteExists = (noteId) => {

View File

@ -99,7 +99,7 @@ import SyncAlertIcon from 'vue-material-design-icons/SyncAlert'
import PencilOffIcon from 'vue-material-design-icons/PencilOff'
import { config } from '../config'
import { fetchNote, refreshNote, saveNote, saveNoteManually, autotitleNote, conflictSolutionLocal, conflictSolutionRemote } from '../NotesService'
import { fetchNote, refreshNote, saveNoteManually, queueCommand, conflictSolutionLocal, conflictSolutionRemote } from '../NotesService'
import { routeIsNewNote } from '../Util'
import TheEditor from './EditorEasyMDE'
import ThePreview from './EditorMarkdownIt'
@ -334,7 +334,7 @@ export default {
if (this.autosaveTimer === null) {
this.autosaveTimer = setTimeout(() => {
this.autosaveTimer = null
saveNote(note.id)
queueCommand(note.id, 'content')
}, config.interval.note.autosave * 1000)
}
@ -352,7 +352,7 @@ export default {
this.autotitleTimer = setTimeout(() => {
this.autotitleTimer = null
if (this.isNewNote) {
autotitleNote(note.id)
queueCommand(note.id, 'autotitle')
}
}, config.interval.note.autotitle * 1000)
}

View File

@ -5,7 +5,6 @@ const state = {
categories: [],
notes: [],
notesIds: {},
unsaved: {},
}
const getters = {
@ -109,14 +108,6 @@ const mutations = {
state.notesIds = {}
},
addUnsaved(state, id) {
Vue.set(state.unsaved, id, state.notesIds[id])
},
clearUnsaved(state) {
state.unsaved = {}
},
setCategories(state, categories) {
state.categories = categories
},

View File

@ -1,4 +1,7 @@
import Vue from 'vue'
const state = {
queue: {},
etag: null,
lastModified: 0,
active: false,
@ -9,6 +12,16 @@ const getters = {
}
const mutations = {
addToQueue(state, { noteId, type }) {
const cmd = { noteId, type }
const key = noteId + '-' + type
Vue.set(state.queue, key, cmd)
},
clearQueue(state) {
state.queue = {}
},
setSyncETag(state, etag) {
if (etag) {
state.etag = etag