Рефакторинг

This commit is contained in:
Book Pauk
2019-03-20 15:22:34 +07:00
parent 090ffa9921
commit 3500a40599

View File

@@ -1,4 +1,5 @@
import localForage from 'localforage'; import localForage from 'localforage';
import _ from 'lodash';
import * as utils from '../../../share/utils'; import * as utils from '../../../share/utils';
import BookParser from './BookParser'; import BookParser from './BookParser';
@@ -65,12 +66,16 @@ class BookManager {
if (keySplit.length == 2 && keySplit[0] == 'bmMeta') { if (keySplit.length == 2 && keySplit[0] == 'bmMeta') {
let meta = await bmMetaStore.getItem(key); let meta = await bmMetaStore.getItem(key);
if (_.isObject(meta)) {
const oldBook = this.books[meta.key]; const oldBook = this.books[meta.key];
this.books[meta.key] = meta; this.books[meta.key] = meta;
if (oldBook && oldBook.parsed) { if (oldBook && oldBook.parsed) {
this.books[meta.key].parsed = oldBook.parsed; this.books[meta.key].parsed = oldBook.parsed;
} }
} else {
await bmMetaStore.removeItem(key);
}
} }
} }
@@ -78,7 +83,11 @@ class BookManager {
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
const key = await bmRecentStore.key(i); const key = await bmRecentStore.key(i);
let r = await bmRecentStore.getItem(key); let r = await bmRecentStore.getItem(key);
if (_.isObject(r)) {
this.recent[r.key] = r; this.recent[r.key] = r;
} else {
await bmRecentStore.removeItem(key);
}
} }
await this.cleanBooks(); await this.cleanBooks();
@@ -296,24 +305,14 @@ class BookManager {
if (!this.recent) if (!this.recent)
await this.init(); await this.init();
if (Object.keys(this.recent).length > 1000) { const sorted = this.getSortedRecent();
let min = Date.now();
let found = null; for (let i = 1000; i < sorted.length; i++) {
for (let key in this.recent) { await bmRecentStore.removeItem(sorted[i].key);
const book = this.recent[key]; delete this.recent[sorted[i].key];
if (book.touchTime < min) {
min = book.touchTime;
found = book;
}
} }
if (found) { this.sortedRecentCached = null;
await bmRecentStore.removeItem(found.key);
delete this.recent[found.key];
await this.cleanRecentBooks();
}
}
} }
mostRecentBook() { mostRecentBook() {