From 3500a4059966e2c2106e26ee269646ab6212a36e Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 20 Mar 2019 15:22:34 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/components/Reader/share/bookManager.js | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/client/components/Reader/share/bookManager.js b/client/components/Reader/share/bookManager.js index 30a51b9e..57bce049 100644 --- a/client/components/Reader/share/bookManager.js +++ b/client/components/Reader/share/bookManager.js @@ -1,4 +1,5 @@ import localForage from 'localforage'; +import _ from 'lodash'; import * as utils from '../../../share/utils'; import BookParser from './BookParser'; @@ -65,11 +66,15 @@ class BookManager { if (keySplit.length == 2 && keySplit[0] == 'bmMeta') { let meta = await bmMetaStore.getItem(key); - const oldBook = this.books[meta.key]; - this.books[meta.key] = meta; + if (_.isObject(meta)) { + const oldBook = this.books[meta.key]; + this.books[meta.key] = meta; - if (oldBook && oldBook.parsed) { - this.books[meta.key].parsed = oldBook.parsed; + if (oldBook && 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++) { const key = await bmRecentStore.key(i); let r = await bmRecentStore.getItem(key); - this.recent[r.key] = r; + if (_.isObject(r)) { + this.recent[r.key] = r; + } else { + await bmRecentStore.removeItem(key); + } } await this.cleanBooks(); @@ -296,24 +305,14 @@ class BookManager { if (!this.recent) await this.init(); - if (Object.keys(this.recent).length > 1000) { - let min = Date.now(); - let found = null; - for (let key in this.recent) { - const book = this.recent[key]; - if (book.touchTime < min) { - min = book.touchTime; - found = book; - } - } + const sorted = this.getSortedRecent(); - if (found) { - await bmRecentStore.removeItem(found.key); - delete this.recent[found.key]; - - await this.cleanRecentBooks(); - } + for (let i = 1000; i < sorted.length; i++) { + await bmRecentStore.removeItem(sorted[i].key); + delete this.recent[sorted[i].key]; } + + this.sortedRecentCached = null; } mostRecentBook() {