123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- import localForage from 'localforage';
- import * as utils from '../../../share/utils';
- import BookParser from './BookParser';
- const maxDataSize = 500*1024*1024;//chars, not bytes
- const bmMetaStore = localForage.createInstance({
- name: 'bmMetaStore'
- });
- const bmDataStore = localForage.createInstance({
- name: 'bmDataStore'
- });
- const bmRecentStore = localForage.createInstance({
- name: 'bmRecentStore'
- });
- class BookManager {
- async init(settings) {
- this.settings = settings;
- this.books = {};
- this.recent = {};
- this.recentChanged1 = true;
- this.recentChanged2 = true;
- let len = await bmMetaStore.length();
- for (let i = 0; i < len; i++) {
- const key = await bmMetaStore.key(i);
- const keySplit = key.split('-');
- if (keySplit.length == 2 && keySplit[0] == 'bmMeta') {
- let meta = await bmMetaStore.getItem(key);
- this.books[meta.key] = meta;
- }
- }
- len = await bmRecentStore.length();
- for (let i = 0; i < len; i++) {
- const key = await bmRecentStore.key(i);
- let r = await bmRecentStore.getItem(key);
- this.recent[r.key] = r;
- }
- await this.cleanBooks();
- }
- async cleanBooks() {
- while (1) {// eslint-disable-line no-constant-condition
- let size = 0;
- let min = Date.now();
- let toDel = null;
- for (let key in this.books) {
- let book = this.books[key];
- size += (book.length ? book.length : 0);
- if (book.addTime < min) {
- toDel = book;
- min = book.addTime;
- }
- }
- if (size > maxDataSize && toDel) {
- await this.delBook(toDel);
- } else {
- break;
- }
- }
- }
- async addBook(newBook, callback) {
- if (!this.books)
- await this.init();
- let meta = {url: newBook.url, path: newBook.path};
- meta.key = this.keyFromUrl(meta.url);
- meta.addTime = Date.now();
- const result = await this.parseBook(meta, newBook.data, callback);
- this.books[meta.key] = result;
- await bmMetaStore.setItem(`bmMeta-${meta.key}`, this.metaOnly(result));
- await bmDataStore.setItem(`bmData-${meta.key}`, result.data);
- return result;
- }
- hasBookParsed(meta) {
- if (!this.books)
- return false;
- if (!meta.url)
- return false;
- if (!meta.key)
- meta.key = this.keyFromUrl(meta.url);
- let book = this.books[meta.key];
- return !!(book && book.parsed);
- }
- async getBook(meta, callback) {
- if (!this.books)
- await this.init();
- let result = undefined;
- if (!meta.key)
- meta.key = this.keyFromUrl(meta.url);
- result = this.books[meta.key];
- if (result && !result.data) {
- result.data = await bmDataStore.getItem(`bmData-${meta.key}`);
- this.books[meta.key] = result;
- }
- if (result && !result.parsed) {
- result = await this.parseBook(result, result.data, callback);
- this.books[meta.key] = result;
- }
- return result;
- }
- async delBook(meta) {
- if (!this.books)
- await this.init();
- await bmMetaStore.removeItem(`bmMeta-${meta.key}`);
- await bmDataStore.removeItem(`bmData-${meta.key}`);
- delete this.books[meta.key];
- }
- async parseBook(meta, data, callback) {
- if (!this.books)
- await this.init();
- const parsed = new BookParser(this.settings);
- const parsedMeta = await parsed.parse(data, callback);
- const result = Object.assign({}, meta, parsedMeta, {
- length: data.length,
- textLength: parsed.textLength,
- data,
- parsed
- });
- return result;
- }
- metaOnly(book) {
- let result = Object.assign({}, book);
- delete result.data;
- delete result.parsed;
- return result;
- }
- keyFromUrl(url) {
- return utils.stringToHex(url);
- }
- async setRecentBook(value, noTouch) {
- if (!this.recent)
- await this.init();
- const result = Object.assign({}, value);
- if (!noTouch)
- Object.assign(result, {touchTime: Date.now()});
- if (result.textLength && !result.bookPos && result.bookPosPercent)
- result.bookPos = Math.round(result.bookPosPercent*result.textLength);
- this.recent[result.key] = result;
- await bmRecentStore.setItem(result.key, result);
- await this.cleanRecentBooks();
- this.recentChanged1 = true;
- this.recentChanged2 = true;
- return result;
- }
- async getRecentBook(value) {
- if (!this.recent)
- await this.init();
- return this.recent[value.key];
- }
- async delRecentBook(value) {
- if (!this.recent)
- await this.init();
- await bmRecentStore.removeItem(value.key);
- delete this.recent[value.key];
- this.recentChanged1 = true;
- this.recentChanged2 = true;
- }
- async cleanRecentBooks() {
- 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;
- }
- }
- if (found) {
- await this.delRecentBook(found);
- await this.cleanRecentBooks();
- }
- }
- }
- mostRecentBook() {
- if (!this.recentChanged1 && this.mostRecentCached) {
- return this.mostRecentCached;
- }
- let max = 0;
- let result = null;
- for (let key in this.recent) {
- const book = this.recent[key];
- if (book.touchTime > max) {
- max = book.touchTime;
- result = book;
- }
- }
- this.mostRecentCached = result;
- this.recentChanged1 = false;
- return result;
- }
- getSortedRecent() {
- if (!this.recentChanged2 && this.sortedRecentCached) {
- return this.sortedRecentCached;
- }
- let result = Object.values(this.recent);
- result.sort((a, b) => b.touchTime - a.touchTime);
- this.sortedRecentCached = result;
- this.recentChanged2 = false;
- return result;
- }
- }
- export default new BookManager();
|