Доделки ServerStorage

This commit is contained in:
Book Pauk
2019-09-06 22:07:15 +07:00
parent 96945dfc4a
commit 1c246f71f8
2 changed files with 75 additions and 37 deletions

View File

@@ -44,8 +44,8 @@ class ServerStorage extends Vue {
this.saveSettings(); this.saveSettings();
}, 500); }, 500);
this.debouncedSaveRecent = _.debounce(() => { this.debouncedSaveRecent = _.debounce((itemKey) => {
this.saveRecent(); this.saveRecent(itemKey);
}, 1000); }, 1000);
this.debouncedNotifySuccess = _.debounce(() => { this.debouncedNotifySuccess = _.debounce(() => {
@@ -54,8 +54,7 @@ class ServerStorage extends Vue {
this.oldProfiles = {}; this.oldProfiles = {};
this.oldSettings = {}; this.oldSettings = {};
this.oldRecent = {}; this.oldRecent = null;
this.oldRecentDiff = {};
} }
async init() { async init() {
@@ -72,18 +71,9 @@ class ServerStorage extends Vue {
} }
} }
async bookManagerEvent(eventName) { async bookManagerEvent(eventName, itemKey) {
if (eventName == 'load-stored-finish') {
this.oldRecent = _.cloneDeep(bookManager.recent);
this.oldRecentLast = _.cloneDeep(bookManager.recentLast) || {};
}
if (eventName == 'recent-changed') { if (eventName == 'recent-changed') {
let i = 0; this.debouncedSaveRecent(itemKey);
while (!bookManager.loaded && i++ < 1000) await utils.sleep(100);
i = 0;
while (!this.inited && i++ < 1000) await utils.sleep(100);
this.debouncedSaveRecent();
} }
} }
@@ -356,7 +346,6 @@ class ServerStorage extends Vue {
} }
} }
console.log('load');
let recentDiff = null; let recentDiff = null;
if (force || revs.items.recentDiff.rev != oldRecentDiffRev) { if (force || revs.items.recentDiff.rev != oldRecentDiffRev) {
try { try {
@@ -372,8 +361,6 @@ console.log('load');
if (recentDiff.rev == 0) if (recentDiff.rev == 0)
recentDiff.data = {}; recentDiff.data = {};
this.oldRecentDiff = _.cloneDeep(recentDiff.data);
await bookManager.setRecentDiffRev(recentDiff.rev); await bookManager.setRecentDiffRev(recentDiff.rev);
} else { } else {
this.warning(`Неверный ответ сервера: ${recentDiff.state}`); this.warning(`Неверный ответ сервера: ${recentDiff.state}`);
@@ -403,44 +390,95 @@ console.log('load');
newRecent = recent.data; newRecent = recent.data;
} }
this.oldRecent = _.cloneDeep(newRecent); this.oldRecent = _.cloneDeep(recent.data);//!!!
await bookManager.setRecent(newRecent); await bookManager.setRecent(newRecent);
await bookManager.setRecentRev(recent.rev); await bookManager.setRecentRev(recent.rev);
} else { } else {
this.warning(`Неверный ответ сервера: ${recent.state}`); this.warning(`Неверный ответ сервера: ${recent.state}`);
} }
} else if (revs.items.recent.rev == oldRecentRev && recentDiff && recentDiff.data) {
let i = 0;
while (!bookManager.loaded && i++ < 1000) utils.sleep(100);
const newRecent = utils.applyObjDiff(bookManager.recent, recentDiff.data);
await bookManager.setRecent(newRecent);
} }
if (doNotifySuccess) if (doNotifySuccess)
this.debouncedNotifySuccess(); this.debouncedNotifySuccess();
} }
async saveRecent() { async saveRecent(itemKey) {
if (!this.keyInited || !this.serverSyncEnabled || this.savingRecent) if (!this.keyInited || !this.serverSyncEnabled || this.savingRecent)
return; return;
const bm = bookManager; const bm = bookManager;
const diff = utils.getObjDiff(this.oldRecent, bm.recent); //несколько замудреная инициализация oldRecent
if (!this.oldRecent) {
this.oldRecent = _.cloneDeep(bookManager.recent);
}
if (bookManager.loaded && !this.oldRecentInited) {
this.oldRecent = _.cloneDeep(bookManager.recent);
this.oldRecentInited = true;
}
//вычисляем дифф
let diff = null;
if (itemKey) {
//ускорение вычисления диффа
const itemDiff = utils.getObjDiff({[itemKey]: (this.oldRecentInited ? this.oldRecent[itemKey] : {})}, {[itemKey]: bm.recent[itemKey]});
if (this.recentDiff) {
diff = this.recentDiff;
diff.change[itemKey] = itemDiff.change[itemKey];
} else {
diff = itemDiff;
}
} else {
if (this.oldRecentInited) {
diff = utils.getObjDiff(this.oldRecent, bm.recent);
} else
return;
}
if (utils.isEmptyObjDiff(diff)) if (utils.isEmptyObjDiff(diff))
return; return;
console.log('save');
this.savingRecent = true; this.recentDiff = diff;
this.savingRecent = true;
try { try {
let result = {state: ''}; if (JSON.stringify(this.recentDiff).length > 1000) {//сохраняем recent целиком
let result = {state: ''};
try { try {
result = await this.storageSet({recent: {rev: bm.recentRev + 1, data: bm.recent}}); result = await this.storageSet({recent: {rev: bm.recentRev + 1, data: bm.recent}});
} catch(e) { } catch(e) {
this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`); this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`);
} }
if (result.state == 'reject') { if (result.state == 'reject') {
await this.loadRecent(true, false); await this.loadRecent(true, false);
this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`); this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
} else if (result.state == 'success') { } else if (result.state == 'success') {
this.oldRecent = _.cloneDeep(bm.recent); this.oldRecent = _.cloneDeep(bm.recent);
await bm.setRecentRev(bm.recentRev + 1); this.recentDiff = null;
await bm.setRecentRev(bm.recentRev + 1);
}
} else {//сохраняем только дифф
let result = {state: ''};
try {
result = await this.storageSet({recentDiff: {rev: bm.recentDiffRev + 1, data: this.recentDiff}});
} catch(e) {
this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`);
}
if (result.state == 'reject') {
await this.loadRecent(false, false);
this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
} else if (result.state == 'success') {
await bm.setRecentDiffRev(bm.recentDiffRev + 1);
}
} }
} finally { } finally {
this.savingRecent = false; this.savingRecent = false;

View File

@@ -343,7 +343,7 @@ class BookManager {
await bmRecentStore.setItem('recent-last', this.recentLast); await bmRecentStore.setItem('recent-last', this.recentLast);
this.recentChanged = true; this.recentChanged = true;
this.emit('recent-changed'); this.emit('recent-changed', result.key);
return result; return result;
} }
@@ -364,7 +364,7 @@ class BookManager {
this.recentLast = null; this.recentLast = null;
await bmRecentStore.setItem('recent-last', this.recentLast); await bmRecentStore.setItem('recent-last', this.recentLast);
} }
this.emit('recent-changed'); this.emit('recent-changed', value.key);
} }
async cleanRecentBooks() { async cleanRecentBooks() {