Работа над BookManager

This commit is contained in:
Book Pauk
2019-01-14 01:30:12 +07:00
parent 7c89bb868e
commit 0f2fe882e5
3 changed files with 95 additions and 13 deletions

View File

@@ -1,23 +1,95 @@
import 'localforage';
import localForage from 'localforage';
import path from 'path';
import BookParser from './BookParser';
class BookManager {
async addBook(book, callback) {
let meta = {url: book.url, path: book.path};
async init() {
this.books = {};
meta.key = path.basename(book.path);
const len = await localForage.length();
for (let i = 0; i < len; i++){
const key = await localForage.key(i);
const keySplit = key.split('-');
if (keySplit.length == 2 && keySplit[1] == 'meta') {
let meta = await localForage.getItem(key);
meta.data = await localForage.getItem(keySplit[0]);
if (callback)
callback(100);
return meta;
this.books[meta.key] = meta;
this.books[meta.url] = meta;
}
}
console.log(this.books);
}
async hasBook(meta) {
async addBook(newBook, callback) {
if (!this.books)
await this.init();
let meta = {url: newBook.url, path: newBook.path};
meta.key = path.basename(newBook.path);
const result = await this.parseBook(meta, newBook.data, callback);
await localForage.setItem(meta.key, result.data);
await localForage.setItem(`${meta.key}-meta`, meta);
return result;
}
async getBook(meta) {
async getBook(meta, callback) {
if (!this.books)
await this.init();
let result = undefined;
if (meta.key)
result = this.books[meta.key];
else
result = this.books[meta.url];
if (result && !result.parsed) {
result = await this.parseBook(result, result.data, callback);
}
return result;
}
async delBook(meta) {
if (!this.books)
await this.init();
let book = undefined;
if (meta.key)
book = this.books[meta.key];
else
book = this.books[meta.url];
if (book) {
await localForage.removeItem(book.key);
await localForage.removeItem(`${book.key}-meta`);
delete this.books[book.key];
delete this.books[book.url];
}
}
async parseBook(meta, data, callback) {
if (!this.books)
await this.init();
const parsed = new BookParser();
const parsedMeta = await parsed.parse(data, callback);
const result = Object.assign({}, meta, parsedMeta, {data, parsed});
this.books[meta.key] = result;
this.books[meta.url] = result;
return result;
}
metaOnly(book) {
let result = Object.assign({}, book);
delete result.data;
delete result.parsed;
return result;
}
}