Browse Source

Улучшения ServerStorage

Book Pauk 2 years ago
parent
commit
e2c0f3658b

+ 23 - 16
client/components/Reader/Reader.vue

@@ -357,6 +357,26 @@ class Reader {
             }
             }
         }, 200);
         }, 200);
 
 
+        this.recentItemKeys = [];
+        this.saveRecentChanges = _.debounce(async() => {
+            let timer = setTimeout(() => {
+                if (!this.offlineModeActive)
+                    this.$root.notify.error('Таймаут соединения');
+            }, 10000);
+
+            try {
+                const itemKeys = this.recentItemKeys;
+                this.recentItemKeys = [];
+                //сохранение в удаленном хранилище
+                await this.$refs.serverStorage.saveRecent(itemKeys);
+            } catch (e) {
+                if (!this.offlineModeActive)
+                    this.$root.notify.error(e.message);
+            } finally {
+                clearTimeout(timer);
+            }
+        }, 500, {maxWait: 1000});
+
         document.addEventListener('fullscreenchange', () => {
         document.addEventListener('fullscreenchange', () => {
             this.fullScreenActive = (document.fullscreenElement !== null);
             this.fullScreenActive = (document.fullscreenElement !== null);
         });
         });
@@ -653,22 +673,9 @@ class Reader {
             }
             }
 
 
             //сохранение в serverStorage
             //сохранение в serverStorage
-            if (value) {
-                await utils.sleep(500);
-                
-                let timer = setTimeout(() => {
-                    if (!this.offlineModeActive)
-                        this.$root.notify.error('Таймаут соединения');
-                }, 10000);
-
-                try {
-                    await this.$refs.serverStorage.saveRecent(value);
-                } catch (e) {
-                    if (!this.offlineModeActive)
-                        this.$root.notify.error(e.message);
-                } finally {
-                    clearTimeout(timer);
-                }
+            if (value && this.recentItemKeys.indexOf(value) < 0) {
+                this.recentItemKeys.push(value);
+                this.saveRecentChanges();
             }
             }
         }
         }
     }
     }

+ 29 - 20
client/components/Reader/ServerStorage/ServerStorage.vue

@@ -12,6 +12,7 @@ import bookManager from '../share/bookManager';
 import readerApi from '../../../api/reader';
 import readerApi from '../../../api/reader';
 import * as utils from '../../../share/utils';
 import * as utils from '../../../share/utils';
 import * as cryptoUtils from '../../../share/cryptoUtils';
 import * as cryptoUtils from '../../../share/cryptoUtils';
+import LockQueue from '../../../share/LockQueue';
 
 
 import localForage from 'localforage';
 import localForage from 'localforage';
 const ssCacheStore = localForage.createInstance({
 const ssCacheStore = localForage.createInstance({
@@ -48,6 +49,8 @@ class ServerStorage {
         this.keyInited = false;
         this.keyInited = false;
         this.commit = this.$store.commit;
         this.commit = this.$store.commit;
         this.prevServerStorageKey = null;
         this.prevServerStorageKey = null;
+        this.lock = new LockQueue(100);
+
         this.$root.generateNewServerStorageKey = () => {this.generateNewServerStorageKey()};
         this.$root.generateNewServerStorageKey = () => {this.generateNewServerStorageKey()};
 
 
         this.debouncedSaveSettings = _.debounce(() => {
         this.debouncedSaveSettings = _.debounce(() => {
@@ -542,14 +545,16 @@ class ServerStorage {
         return true;
         return true;
     }
     }
 
 
-    async saveRecent(itemKey, recurse) {
-        while (!this.inited || this.savingRecent)
+    async saveRecent(itemKeys, recurse) {
+        while (!this.inited)
             await utils.sleep(100);
             await utils.sleep(100);
 
 
-        if (!this.keyInited || !this.serverSyncEnabled || this.savingRecent)
+        if (!this.keyInited || !this.serverSyncEnabled)
             return;
             return;
 
 
-        this.savingRecent = true;
+        let needRecurseCall = false;
+
+        await this.lock.get();
         try {        
         try {        
             const bm = bookManager;
             const bm = bookManager;
 
 
@@ -559,22 +564,29 @@ class ServerStorage {
 
 
             //newRecentMod
             //newRecentMod
             let newRecentMod = {};
             let newRecentMod = {};
-            if (itemKey && this.cachedRecentPatch.data[itemKey] && this.prevItemKey == itemKey) {
+            let oneItemKey = null;
+            if (itemKeys.length == 1)
+                oneItemKey = itemKeys[0];
+
+            if (oneItemKey && this.cachedRecentPatch.data[oneItemKey] && this.prevItemKey == oneItemKey) {
                 newRecentMod = _.cloneDeep(this.cachedRecentMod);
                 newRecentMod = _.cloneDeep(this.cachedRecentMod);
                 newRecentMod.rev++;
                 newRecentMod.rev++;
 
 
-                newRecentMod.data.key = itemKey;
-                newRecentMod.data.mod = utils.getObjDiff(this.cachedRecentPatch.data[itemKey], bm.recent[itemKey]);
+                newRecentMod.data.key = oneItemKey;
+                newRecentMod.data.mod = utils.getObjDiff(this.cachedRecentPatch.data[oneItemKey], bm.recent[oneItemKey]);
                 needSaveRecentMod = true;
                 needSaveRecentMod = true;
             }
             }
-            this.prevItemKey = itemKey;
+            this.prevItemKey = oneItemKey;
 
 
             //newRecentPatch
             //newRecentPatch
             let newRecentPatch = {};
             let newRecentPatch = {};
-            if (itemKey && !needSaveRecentMod) {
+            if (itemKeys && !needSaveRecentMod) {
                 newRecentPatch = _.cloneDeep(this.cachedRecentPatch);
                 newRecentPatch = _.cloneDeep(this.cachedRecentPatch);
                 newRecentPatch.rev++;
                 newRecentPatch.rev++;
-                newRecentPatch.data[itemKey] = _.cloneDeep(bm.recent[itemKey]);
+                
+                for (const key of itemKeys) {
+                    newRecentPatch.data[key] = _.cloneDeep(bm.recent[key]);
+                }
 
 
                 const applyMod = this.cachedRecentMod.data;
                 const applyMod = this.cachedRecentMod.data;
                 if (applyMod && applyMod.key && newRecentPatch.data[applyMod.key])
                 if (applyMod && applyMod.key && newRecentPatch.data[applyMod.key])
@@ -587,11 +599,7 @@ class ServerStorage {
 
 
             //newRecent
             //newRecent
             let newRecent = {};
             let newRecent = {};
-            if (!itemKey || (needSaveRecentPatch && Object.keys(newRecentPatch.data).length > 10)) {
-                //ждем весь bm.recent
-                /*while (!bookManager.loaded)
-                    await utils.sleep(100);*/
-
+            if (!itemKeys || (needSaveRecentPatch && Object.keys(newRecentPatch.data).length > 10)) {
                 newRecent = {rev: this.cachedRecent.rev + 1, data: _.cloneDeep(bm.recent)};
                 newRecent = {rev: this.cachedRecent.rev + 1, data: _.cloneDeep(bm.recent)};
                 newRecentPatch = {rev: this.cachedRecentPatch.rev + 1, data: {}};
                 newRecentPatch = {rev: this.cachedRecentPatch.rev + 1, data: {}};
                 newRecentMod = {rev: this.cachedRecentMod.rev + 1, data: {}};
                 newRecentMod = {rev: this.cachedRecentMod.rev + 1, data: {}};
@@ -625,10 +633,8 @@ class ServerStorage {
 
 
                 if (res)
                 if (res)
                     this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
                     this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
-                if (!recurse && itemKey) {
-                    this.savingRecent = false;
-                    await this.saveRecent(itemKey, true);
-                    return;
+                if (!recurse && itemKeys) {
+                    needRecurseCall = true;
                 }
                 }
             } else if (result.state == 'success') {
             } else if (result.state == 'success') {
                 if (needSaveRecent && newRecent.rev)
                 if (needSaveRecent && newRecent.rev)
@@ -639,8 +645,11 @@ class ServerStorage {
                     await this.setCachedRecentMod(newRecentMod);
                     await this.setCachedRecentMod(newRecentMod);
             }
             }
         } finally {
         } finally {
-            this.savingRecent = false;
+            this.lock.ret();
         }
         }
+
+        if (needRecurseCall)
+            await this.saveRecent(itemKeys, true);
     }
     }
 
 
     async storageCheck(items) {
     async storageCheck(items) {