Работа над ServerStorage, попутные оптимизации

This commit is contained in:
Book Pauk
2019-03-20 14:34:01 +07:00
parent 826ee18666
commit b12198fdcf
2 changed files with 47 additions and 9 deletions

View File

@@ -46,6 +46,7 @@ class ServerStorage extends Vue {
this.oldProfiles = {}; this.oldProfiles = {};
this.oldSettings = {}; this.oldSettings = {};
this.oldRecent = {};
} }
async init() { async init() {
@@ -243,7 +244,7 @@ class ServerStorage extends Vue {
//проверим ревизию на сервере //проверим ревизию на сервере
if (!force) { if (!force) {
try { try {
const revs = await this.storageCheck({'profiles': {}}); const revs = await this.storageCheck({profiles: {}});
if (revs.state == 'success' && revs.items.profiles.rev == oldRev) { if (revs.state == 'success' && revs.items.profiles.rev == oldRev) {
return; return;
} }
@@ -255,7 +256,7 @@ class ServerStorage extends Vue {
let prof = null; let prof = null;
try { try {
prof = await this.storageGet({'profiles': {}}); prof = await this.storageGet({profiles: {}});
} catch(e) { } catch(e) {
this.error(`Ошибка соединения с сервером: ${e.message}`); this.error(`Ошибка соединения с сервером: ${e.message}`);
return; return;
@@ -297,7 +298,7 @@ class ServerStorage extends Vue {
let tries = 0; let tries = 0;
while (result.state != 'success' && tries < maxSetTries) { while (result.state != 'success' && tries < maxSetTries) {
try { try {
result = await this.storageSet({'profiles': {rev: this.profilesRev + 1, data: this.profiles}}); result = await this.storageSet({profiles: {rev: this.profilesRev + 1, data: this.profiles}});
} catch(e) { } catch(e) {
this.savingProfiles = false; this.savingProfiles = false;
this.commit('reader/setProfiles', this.oldProfiles); this.commit('reader/setProfiles', this.oldProfiles);
@@ -329,7 +330,46 @@ class ServerStorage extends Vue {
} }
} }
async loadRecent() { async loadRecent(force) {
if (!this.serverSyncEnabled)
return;
const oldRev = bookManager.recentRev;
//проверим ревизию на сервере
if (!force) {
try {
const revs = await this.storageCheck({recent: {}});
if (revs.state == 'success' && revs.items.recent.rev == oldRev) {
return;
}
} catch(e) {
this.error(`Ошибка соединения с сервером: ${e.message}`);
return;
}
}
let recent = null;
try {
recent = await this.storageGet({recent: {}});
} catch(e) {
this.error(`Ошибка соединения с сервером: ${e.message}`);
return;
}
if (recent.state == 'success') {
recent = recent.items.recent;
if (recent.rev == 0)
recent.data = {};
this.oldRecent = _.cloneDeep(recent.data);
bookManager.setRecent(recent.data);
bookManager.setRecentRev(recent.rev);
this.notifySuccessIfNeeded(oldRev, recent.rev);
} else {
this.warning(`Неверный ответ сервера: ${recent.state}`);
}
} }
async storageCheck(items) { async storageCheck(items) {

View File

@@ -39,7 +39,6 @@ class BookManager {
this.recentLastRev = bmRecentStore.getItem('recent-last-rev') || 0; this.recentLastRev = bmRecentStore.getItem('recent-last-rev') || 0;
this.books = Object.assign({}, this.booksCached); this.books = Object.assign({}, this.booksCached);
this.recentChanged1 = true;
this.recentChanged2 = true; this.recentChanged2 = true;
if (!this.books || !this.recent) { if (!this.books || !this.recent) {
@@ -264,7 +263,7 @@ class BookManager {
await bmCacheStore.setItem('recent-last', this.recentLast); await bmCacheStore.setItem('recent-last', this.recentLast);
this.emit('recent-last-changed'); this.emit('recent-last-changed');
this.recentChanged1 = true; this.mostRecentCached = result;
this.recentChanged2 = true; this.recentChanged2 = true;
return result; return result;
} }
@@ -284,7 +283,7 @@ class BookManager {
await bmCacheStore.setItem('recent', this.recent); await bmCacheStore.setItem('recent', this.recent);
this.emit('recent-changed'); this.emit('recent-changed');
this.recentChanged1 = true; this.mostRecentCached = null;
this.recentChanged2 = true; this.recentChanged2 = true;
} }
@@ -313,7 +312,7 @@ class BookManager {
} }
mostRecentBook() { mostRecentBook() {
if (!this.recentChanged1 && this.mostRecentCached) { if (this.mostRecentCached) {
return this.mostRecentCached; return this.mostRecentCached;
} }
@@ -327,7 +326,6 @@ class BookManager {
} }
} }
this.mostRecentCached = result; this.mostRecentCached = result;
this.recentChanged1 = false;
return result; return result;
} }