diff options
author | korelstar <korelstar@users.noreply.github.com> | 2021-07-31 12:42:23 +0300 |
---|---|---|
committer | korelstar <korelstar@users.noreply.github.com> | 2021-07-31 19:07:23 +0300 |
commit | a84a4fcc2b1bf64fe3e906c944b76732f5ef2ec8 (patch) | |
tree | 814f5b7b0065ffbd8786665b99164153aef64503 /src | |
parent | c6762ebd90c329b116b48b1c9537de3e85db70fa (diff) |
rewrite save queue, now for content and autotitle
Diffstat (limited to 'src')
-rw-r--r-- | src/NotesService.js | 51 | ||||
-rw-r--r-- | src/components/Note.vue | 6 | ||||
-rw-r--r-- | src/store/notes.js | 9 | ||||
-rw-r--r-- | src/store/sync.js | 13 |
4 files changed, 50 insertions, 29 deletions
diff --git a/src/NotesService.js b/src/NotesService.js index d1b1ebc3..5d08ed78 100644 --- a/src/NotesService.js +++ b/src/NotesService.js @@ -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) => { diff --git a/src/components/Note.vue b/src/components/Note.vue index f8df87d6..92c45a09 100644 --- a/src/components/Note.vue +++ b/src/components/Note.vue @@ -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) } diff --git a/src/store/notes.js b/src/store/notes.js index 45395f88..24289e18 100644 --- a/src/store/notes.js +++ b/src/store/notes.js @@ -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 }, diff --git a/src/store/sync.js b/src/store/sync.js index 159106d7..24e5dbb8 100644 --- a/src/store/sync.js +++ b/src/store/sync.js @@ -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 |