Рефакторинг

This commit is contained in:
Book Pauk
2022-07-10 18:27:05 +07:00
parent 5edeed0747
commit 26ab5d6765
2 changed files with 38 additions and 25 deletions

View File

@@ -1092,24 +1092,28 @@ class Reader {
// есть ли среди недавних
let wasOpened = bookManager.findRecentByUrlAndPath(url, opts.path);
wasOpened = (wasOpened ? wasOpened : {});
const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
const uploadFileName = (opts.uploadFileName ? opts.uploadFileName : '');
wasOpened = (wasOpened ? _.cloneDeep(wasOpened) : {});
wasOpened = Object.assign(wasOpened, {
path: (opts.path !== undefined ? opts.path : wasOpened.path),
bookPos: (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos),
bookPosSeen: (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen),
uploadFileName: (opts.uploadFileName ? opts.uploadFileName : wasOpened.uploadFileName),
});
let book = null;
if (!opts.force) {
// пытаемся загрузить и распарсить книгу в менеджере из локального кэша
const bookParsed = await bookManager.getBook({path: (opts.path ? opts.path : wasOpened.path)}, (prog) => {
const bookParsed = await bookManager.getBook(wasOpened, (prog) => {
progress.setState({progress: prog});
});
// если есть в локальном кэше
if (bookParsed) {
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
await bookManager.setRecentBook(Object.assign(wasOpened, bookParsed));
this.mostRecentBook();
this.addAction(bookPos);
this.addAction(wasOpened.bookPos);
this.loaderActive = false;
progress.hide(); this.progressActive = false;
this.blinkCachedLoadMessage();
@@ -1140,7 +1144,7 @@ class Reader {
if (!book) {
book = await readerApi.loadBook({
url,
uploadFileName,
uploadFileName: wasOpened.uploadFileName,
enableSitesFilter: this.enableSitesFilter,
skipHtmlCheck: (this.splitToPara ? true : false),
isText: (this.splitToPara ? true : false),
@@ -1157,14 +1161,15 @@ class Reader {
// добавляем в bookManager
progress.setState({state: 'parse', step: 5});
const addedBook = await bookManager.addBook(book, (prog) => {
progress.setState({progress: prog});
});
// добавляем в историю
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, uploadFileName}, addedBook));
await bookManager.setRecentBook(Object.assign(wasOpened, addedBook));
this.mostRecentBook();
this.addAction(bookPos);
this.addAction(wasOpened.bookPos);
this.updateRoute(true);
this.loaderActive = false;

View File

@@ -53,7 +53,8 @@ class BookManager {
if (this.recentItem)
this.recent[this.recentItem.key] = this.recentItem;
this.convertRecent();
//конвертируем в новые ключи
await this.convertRecent();
this.recentLastKey = await bmRecentStoreNew.getItem('recent-last-key');
if (this.recentLastKey) {
@@ -72,12 +73,14 @@ class BookManager {
}
//TODO: убрать в 2025г
convertRecent() {
let converted = false;
async convertRecent() {
const converted = await bmRecentStoreNew.getItem('recent-converted');
if (converted)
return;
for (const key in this.recent) {
const book = this.recent[key];
if (!book.path) {
continue;
}
@@ -85,20 +88,18 @@ class BookManager {
const newKey = this.keyFromPath(book.path);
if (!book.deleted && key !== newKey) {
this.recent[newKey] = _.cloneDeep(this.recent[key]);
this.recent[key].deleted = 1;
converted = true;
this.recent[newKey] = _.cloneDeep(book);
book.deleted = 1;
}
}
//console.log(converted);
if (converted) {
(async() => {
await utils.sleep(5000);
this.saveRecent();
this.emit('recent-changed');
})();
}
(async() => {
await utils.sleep(3000);
this.saveRecent();
this.emit('recent-changed');
await bmRecentStoreNew.setItem('recent-converted', true);
})();
}
//Ленивая асинхронная загрузка bmMetaStore
@@ -486,12 +487,19 @@ class BookManager {
}
findRecentByUrlAndPath(url, bookPath) {
if (bookPath) {
const key = this.keyFromPath(bookPath);
const book = this.recent[key];
if (book && !book.deleted)
return book;
}
let max = 0;
let result = null;
for (const key in this.recent) {
const book = this.recent[key];
if (!book.deleted && book.url == url && book.addTime > max && (!bookPath || book.path == bookPath)) {
if (!book.deleted && book.url == url && book.addTime > max) {
max = book.addTime;
result = book;
}