Закончена очередная переработка loadRecent & saveRecent

This commit is contained in:
Book Pauk
2019-10-18 15:19:53 +07:00
parent 5184661652
commit 51ebbbc569
2 changed files with 63 additions and 22 deletions

View File

@@ -141,8 +141,8 @@ class ServerStorage extends Vue {
await this.loadProfiles(force); await this.loadProfiles(force);
this.checkCurrentProfile(); this.checkCurrentProfile();
await this.currentProfileChanged(force); await this.currentProfileChanged(force);
await this.loadRecent(); const loadSuccess = await this.loadRecent();
if (force) if (loadSuccess && force)
await this.saveRecent(); await this.saveRecent();
} }
} }
@@ -381,7 +381,7 @@ class ServerStorage extends Vue {
} else if (revs.items.recentMod.rev != this.cachedRecentMod.rev) { } else if (revs.items.recentMod.rev != this.cachedRecentMod.rev) {
query = {recentMod: {}}; query = {recentMod: {}};
} else } else
return; return true;
} }
} catch(e) { } catch(e) {
this.error(`Ошибка соединения с сервером: ${e.message}`); this.error(`Ошибка соединения с сервером: ${e.message}`);
@@ -402,20 +402,25 @@ class ServerStorage extends Vue {
let newRecentPatch = recent.items.recentPatch; let newRecentPatch = recent.items.recentPatch;
let newRecentMod = recent.items.recentMod; let newRecentMod = recent.items.recentMod;
if (!newRecent) if (!newRecent) {
newRecent = _.cloneDeep(this.cachedRecent); newRecent = _.cloneDeep(this.cachedRecent);
if (!newRecentPatch) }
if (!newRecentPatch) {
newRecentPatch = _.cloneDeep(this.cachedRecentPatch); newRecentPatch = _.cloneDeep(this.cachedRecentPatch);
if (!newRecentMod) }
if (!newRecentMod) {
newRecentMod = _.cloneDeep(this.cachedRecentMod); newRecentMod = _.cloneDeep(this.cachedRecentMod);
}
if (newRecent.rev == 0) newRecent.data = {}; if (newRecent.rev == 0) newRecent.data = {};
if (newRecentPatch.rev == 0) newRecentPatch.data = {}; if (newRecentPatch.rev == 0) newRecentPatch.data = {};
if (newRecentMod.rev == 0) newRecentMod.data = {}; if (newRecentMod.rev == 0) newRecentMod.data = {};
let result = Object.assign({}, newRecent.data, newRecentPatch.data); let result = Object.assign({}, newRecent.data, newRecentPatch.data);
if (newRecentMod.key && result[newRecentMod.key])
result[newRecentMod.key] = utils.applyObjDiff(result[newRecentMod.key], newRecentMod.mod); const md = newRecentMod.data;
if (md.key && result[md.key])
result[md.key] = utils.applyObjDiff(result[md.key], md.mod);
if (newRecent.rev != this.cachedRecent.rev) if (newRecent.rev != this.cachedRecent.rev)
await this.setCachedRecent(newRecent); await this.setCachedRecent(newRecent);
@@ -432,6 +437,7 @@ class ServerStorage extends Vue {
await bookManager.setRecent(result); await bookManager.setRecent(result);
} else { } else {
this.warning(`Неверный ответ сервера: ${recent.state}`); this.warning(`Неверный ответ сервера: ${recent.state}`);
return;
} }
if (doNotifySuccess) if (doNotifySuccess)
@@ -439,6 +445,7 @@ class ServerStorage extends Vue {
} finally { } finally {
this.loadingRecent = false; this.loadingRecent = false;
} }
return true;
} }
async saveRecent(itemKey, recurse) { async saveRecent(itemKey, recurse) {
@@ -449,18 +456,52 @@ class ServerStorage extends Vue {
let needSaveRecent = false; let needSaveRecent = false;
let needSaveRecentPatch = false; let needSaveRecentPatch = false;
let needSaveRecentMod = true; let needSaveRecentMod = false;
let newRecentMod = _.cloneDeep(this.cachedRecentPatch); let applyMod = null;
newRecentMod.rev++;
let newRecentPatch = _.cloneDeep(this.cachedRecentPatch); //newRecentMod
newRecentPatch.data[itemKey] = bm.recent[itemKey]; let newRecentMod = {};
newRecentPatch.rev++;
needSaveRecentPatch = true;
let newRecent = {rev: this.cachedRecent.rev + 1, data: bm.recent}; if (itemKey && this.cachedRecentPatch.data[itemKey]) {
if (this.prevItemKey == itemKey) {//сохраняем только дифф
newRecentMod = _.cloneDeep(this.cachedRecentMod);
newRecentMod.rev++;
newRecentMod.data.key = itemKey;
newRecentMod.data.mod = utils.getObjDiff(this.cachedRecentPatch.data[itemKey], bm.recent[itemKey]);
needSaveRecentMod = true;
} else {//ключ не совпадает, надо сохранять патч
applyMod = newRecentMod.data;
}
}
this.prevItemKey = itemKey;
//newRecentPatch
let newRecentPatch = {};
if (itemKey && !needSaveRecentMod) {
newRecentPatch = _.cloneDeep(this.cachedRecentPatch);
newRecentPatch.rev++;
newRecentPatch.data[itemKey] = bm.recent[itemKey];
if (applyMod && applyMod.key && newRecentPatch.data[applyMod.key])
newRecentPatch.data[applyMod.key] = utils.applyObjDiff(newRecentPatch.data[applyMod.key], applyMod.mod);
newRecentMod = {rev: this.cachedRecentMod.rev + 1, data: {}};
needSaveRecentPatch = true;
needSaveRecentMod = true;
}
//newRecent
let newRecent = {};
if (!itemKey || (needSaveRecentPatch && Object.keys(newRecentPatch.data).length > 2)) {
newRecent = {rev: this.cachedRecent.rev + 1, data: bm.recent};
newRecentPatch = {rev: this.cachedRecentPatch.rev + 1, data: {}};
newRecentMod = {rev: this.cachedRecentMod.rev + 1, data: {}};
needSaveRecent = true;
needSaveRecentPatch = true;
needSaveRecentMod = true;
}
//query
let query = {}; let query = {};
if (needSaveRecent) { if (needSaveRecent) {
query = {recent: newRecent, recentPatch: newRecentPatch, recentMod: newRecentMod}; query = {recent: newRecent, recentPatch: newRecentPatch, recentMod: newRecentMod};
@@ -483,10 +524,10 @@ class ServerStorage extends Vue {
if (result.state == 'reject') { if (result.state == 'reject') {
await this.loadRecent(true, false); await this.loadRecent(false, false);
this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`); this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
if (!recurse) { if (!recurse && itemKey) {
this.savingRecent = false; this.savingRecent = false;
this.saveRecent(itemKey, true); this.saveRecent(itemKey, true);
return; return;
@@ -494,11 +535,11 @@ class ServerStorage extends Vue {
} else if (result.state == 'success') { } else if (result.state == 'success') {
//this.prevSavedItem = _.cloneDeep(bm.recent[itemKey]); //this.prevSavedItem = _.cloneDeep(bm.recent[itemKey]);
if (needSaveRecent) if (needSaveRecent && newRecent.rev)
await this.setCachedRecent(newRecent); await this.setCachedRecent(newRecent);
if (needSaveRecentPatch) if (needSaveRecentPatch && newRecentPatch.rev)
await this.setCachedRecentPatch(newRecentPatch); await this.setCachedRecentPatch(newRecentPatch);
if (needSaveRecentMod) if (needSaveRecentMod && newRecentMod.rev)
await this.setCachedRecentMod(newRecentMod); await this.setCachedRecentMod(newRecentMod);
} }
} finally { } finally {

View File

@@ -437,7 +437,7 @@ class BookManager {
Object.assign(mergedRecent, value); Object.assign(mergedRecent, value);
//подстраховка //подстраховка от hotReload
for (let i of Object.keys(mergedRecent)) { for (let i of Object.keys(mergedRecent)) {
if (!mergedRecent[i].key || mergedRecent[i].key !== i) if (!mergedRecent[i].key || mergedRecent[i].key !== i)
delete mergedRecent[i]; delete mergedRecent[i];