From 08b4afd287682ed30967b3efc3f4c2299ecbd210 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Fri, 30 Oct 2020 12:01:22 +0700 Subject: [PATCH] =?UTF-8?q?Libs=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B2=20=D1=81=D0=B8=D0=BD=D1=85=D1=80?= =?UTF-8?q?=D0=BE=D0=BD=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Reader/ServerStorage/ServerStorage.vue | 92 ++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/client/components/Reader/ServerStorage/ServerStorage.vue b/client/components/Reader/ServerStorage/ServerStorage.vue index 5988faf2..d28e3dd0 100644 --- a/client/components/Reader/ServerStorage/ServerStorage.vue +++ b/client/components/Reader/ServerStorage/ServerStorage.vue @@ -35,6 +35,9 @@ export default @Component({ currentProfile: function() { this.currentProfileChanged(true); }, + libs: function() { + this.debouncedSaveLibs(); + }, }, }) class ServerStorage extends Vue { @@ -49,12 +52,17 @@ class ServerStorage extends Vue { this.saveSettings(); }, 500); + this.debouncedSaveLibs = _.debounce(() => { + this.saveLibs(); + }, 500); + this.debouncedNotifySuccess = _.debounce(() => { this.success('Данные синхронизированы с сервером'); }, 1000); this.oldProfiles = {}; this.oldSettings = {}; + this.oldLibs = {}; } async init() { @@ -124,6 +132,8 @@ class ServerStorage extends Vue { await this.loadProfiles(force); this.checkCurrentProfile(); await this.currentProfileChanged(force); + await this.loadLibs(force); + const loadSuccess = await this.loadRecent(); if (loadSuccess && force) await this.saveRecent(); @@ -169,6 +179,14 @@ class ServerStorage extends Vue { return this.settings.showServerStorageMessages; } + get libs() { + return this.$store.state.reader.libs; + } + + get libsRev() { + return this.$store.state.reader.libsRev; + } + checkCurrentProfile() { if (!this.profiles[this.currentProfile]) { this.commit('reader/setCurrentProfile', ''); @@ -338,13 +356,85 @@ class ServerStorage extends Vue { this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`); } else if (result.state == 'success') { this.oldProfiles = _.cloneDeep(this.profiles); - this.commit('reader/setProfilesRev', this.profilesRev + 1); + this.commit('reader/setProfilesRev', this.profilesRev + 1); } } finally { this.savingProfiles = false; } } + async loadLibs(force = false, doNotifySuccess = true) { + if (!this.keyInited || !this.serverSyncEnabled) + return; + + const oldRev = this.libsRev; + //проверим ревизию на сервере + if (!force) { + try { + const revs = await this.storageCheck({libs: {}}); + if (revs.state == 'success' && revs.items.libs.rev == oldRev) { + return; + } + } catch(e) { + this.error(`Ошибка соединения с сервером: ${e.message}`); + return; + } + } + + let libs = null; + try { + libs = await this.storageGet({libs: {}}); + } catch(e) { + this.error(`Ошибка соединения с сервером: ${e.message}`); + return; + } + + if (libs.state == 'success') { + libs = libs.items.libs; + + if (libs.rev == 0) + libs.data = {}; + + this.oldLibs = _.cloneDeep(libs.data); + this.commit('reader/setLibs', libs.data); + this.commit('reader/setLibsRev', libs.rev); + + if (doNotifySuccess) + this.debouncedNotifySuccess(); + } else { + this.warning(`Неверный ответ сервера: ${libs.state}`); + } + } + + async saveLibs() { + if (!this.keyInited || !this.serverSyncEnabled || this.savingLibs) + return; + + const diff = utils.getObjDiff(this.oldLibs, this.libs); + if (utils.isEmptyObjDiff(diff)) + return; + + this.savingLibs = true; + try { + let result = {state: ''}; + try { + result = await this.storageSet({libs: {rev: this.libsRev + 1, data: this.libs}}); + } catch(e) { + this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`); + } + + if (result.state == 'reject') { + await this.loadLibs(true, false); + this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`); + } else if (result.state == 'success') { + this.oldLibs = _.cloneDeep(this.libs); + this.commit('reader/setLibsRev', this.libsRev + 1); + } + } finally { + this.savingLibs = false; + } + } + async loadRecent(skipRevCheck = false, doNotifySuccess = true) { if (!this.keyInited || !this.serverSyncEnabled || this.loadingRecent) return;