From 6904cfd2246f7214bfda4eb1302d5e422119f9c8 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 13 Mar 2019 17:41:32 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=B6=D0=B0=D1=82=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BA=D0=BD=D0=B8=D0=B3=D0=B8=20=D0=B2=20=D0=BA?= =?UTF-8?q?=D0=B5=D1=88=D0=B5?= 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, 35 insertions(+), 6 deletions(-) diff --git a/client/components/Reader/share/bookManager.js b/client/components/Reader/share/bookManager.js index df2c9306..4146cb7a 100644 --- a/client/components/Reader/share/bookManager.js +++ b/client/components/Reader/share/bookManager.js @@ -3,7 +3,7 @@ import localForage from 'localforage'; import * as utils from '../../../share/utils'; import BookParser from './BookParser'; -const maxDataSize = 500*1024*1024;//chars, not bytes +const maxDataSize = 300*1024*1024;//compressed bytes const bmMetaStore = localForage.createInstance({ name: 'bmMetaStore' @@ -97,7 +97,8 @@ class BookManager { let toDel = null; for (let key in this.books) { let book = this.books[key]; - size += (book.length ? book.length : 0); + const bookLength = (book.length ? book.length : 0); + size += (book.dataCompressedLength ? book.dataCompressedLength : bookLength); if (book.addTime < min) { toDel = book; @@ -120,15 +121,29 @@ class BookManager { meta.key = this.keyFromUrl(meta.url); meta.addTime = Date.now(); - const result = await this.parseBook(meta, newBook.data, callback); + const cb = (perc) => { + const p = Math.round(80*perc/100); + callback(p); + }; + + const result = await this.parseBook(meta, newBook.data, cb); + result.dataCompressed = true; + + let data = newBook.data; + if (result.dataCompressed) { + data = utils.pako.deflate(data, {level: 9}); + result.dataCompressedLength = data.byteLength; + } + callback(90); this.books[meta.key] = result; this.booksCached[meta.key] = this.metaOnly(result); await bmMetaStore.setItem(`bmMeta-${meta.key}`, this.metaOnly(result)); - await bmDataStore.setItem(`bmData-${meta.key}`, newBook.data); + await bmDataStore.setItem(`bmData-${meta.key}`, data); await bmCacheStore.setItem('books', this.booksCached); + callback(100); return result; } @@ -149,11 +164,25 @@ class BookManager { let result = undefined; if (!meta.key) meta.key = this.keyFromUrl(meta.url); + result = this.books[meta.key]; if (result && !result.parsed) { - const data = await bmDataStore.getItem(`bmData-${meta.key}`); - result = await this.parseBook(result, data, callback); + let data = await bmDataStore.getItem(`bmData-${meta.key}`); + callback(10); + await utils.sleep(10); + + if (result.dataCompressed) { + data = utils.pako.inflate(data, {to: 'string'}); + } + callback(20); + + const cb = (perc) => { + const p = 20 + Math.round(80*perc/100); + callback(p); + }; + + result = await this.parseBook(result, data, cb); this.books[meta.key] = result; }