Рефакторинг

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

View File

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