Рефакторинг, небольшие улучшения

This commit is contained in:
Book Pauk
2022-11-28 16:11:54 +07:00
parent a6aa8d8e95
commit af575a87a2
5 changed files with 19 additions and 13 deletions

View File

@@ -253,7 +253,7 @@ export default class BaseList {
result = await this.api.getAuthorBookList(authorId); result = await this.api.getAuthorBookList(authorId);
} }
return (result.books ? JSON.parse(result.books) : []); return result.books;
} catch (e) { } catch (e) {
this.$root.stdDialog.alert(e.message, 'Ошибка'); this.$root.stdDialog.alert(e.message, 'Ошибка');
} }
@@ -276,7 +276,7 @@ export default class BaseList {
result = await this.api.getSeriesBookList(series); result = await this.api.getSeriesBookList(series);
} }
return (result.books ? JSON.parse(result.books) : []); return result.books;
} catch (e) { } catch (e) {
this.$root.stdDialog.alert(e.message, 'Ошибка'); this.$root.stdDialog.alert(e.message, 'Ошибка');
} }

View File

@@ -8,6 +8,8 @@ const abStore = localForage.createInstance({
name: 'authorBooksStorage' name: 'authorBooksStorage'
}); });
const storageVersion = '1';
class AuthorBooksStorage { class AuthorBooksStorage {
constructor() { constructor() {
} }
@@ -17,6 +19,8 @@ class AuthorBooksStorage {
} }
async setData(key, data) { async setData(key, data) {
key += storageVersion;
if (typeof data !== 'string') if (typeof data !== 'string')
throw new Error('AuthorBooksStorage: data must be a string'); throw new Error('AuthorBooksStorage: data must be a string');
@@ -25,6 +29,8 @@ class AuthorBooksStorage {
} }
async getData(key) { async getData(key) {
key += storageVersion;
const item = await abStore.getItem(key); const item = await abStore.getItem(key);
//обновим addTime //обновим addTime
@@ -34,9 +40,9 @@ class AuthorBooksStorage {
return item; return item;
} }
async removeData(key) { async _removeData(fullKey) {
await abStore.removeItem(key); await abStore.removeItem(fullKey);
await abStore.removeItem(`addTime-${key}`); await abStore.removeItem(`addTime-${fullKey}`);
} }
async cleanStorage() { async cleanStorage() {
@@ -62,7 +68,7 @@ class AuthorBooksStorage {
} }
if (size > maxDataSize && toDel) { if (size > maxDataSize && toDel) {
await this.removeData(toDel); await this._removeData(toDel);
} else { } else {
break; break;
} }

View File

@@ -625,14 +625,14 @@ class DbSearcher {
const rows = await this.restoreBooks('author', [authorId]); const rows = await this.restoreBooks('author', [authorId]);
let authorName = ''; let authorName = '';
let books = ''; let books = [];
if (rows.length) { if (rows.length) {
authorName = rows[0].name; authorName = rows[0].name;
books = rows[0].books; books = rows[0].books;
} }
return {author: authorName, books: (books && books.length ? JSON.stringify(books) : '')}; return {author: authorName, books};
} finally { } finally {
this.searchFlag--; this.searchFlag--;
} }
@@ -659,7 +659,7 @@ class DbSearcher {
where: `return Array.from(@dirtyIndexLR('value', ${db.esc(series)}, ${db.esc(series)}))` where: `return Array.from(@dirtyIndexLR('value', ${db.esc(series)}, ${db.esc(series)}))`
}); });
let books; let books = [];
if (rows.length && rows[0].rawResult.length) { if (rows.length && rows[0].rawResult.length) {
//выборка книг серии //выборка книг серии
const bookRows = await this.restoreBooks('series', [rows[0].rawResult[0]]) const bookRows = await this.restoreBooks('series', [rows[0].rawResult[0]])
@@ -668,7 +668,7 @@ class DbSearcher {
books = bookRows[0].books; books = bookRows[0].books;
} }
return {books: (books && books.length ? JSON.stringify(books) : '')}; return {books};
} finally { } finally {
this.searchFlag--; this.searchFlag--;
} }

View File

@@ -79,7 +79,7 @@ class AuthorPage extends BasePage {
const bookList = await this.webWorker.getSeriesBookList(query.series); const bookList = await this.webWorker.getSeriesBookList(query.series);
if (bookList.books) { if (bookList.books) {
let books = JSON.parse(bookList.books); let books = bookList.books;
const booksAll = this.filterBooks(books, {del: 0}); const booksAll = this.filterBooks(books, {del: 0});
const filtered = (query.all ? booksAll : this.filterBooks(books, query)); const filtered = (query.all ? booksAll : this.filterBooks(books, query));
const sorted = this.sortSeriesBooks(filtered); const sorted = this.sortSeriesBooks(filtered);
@@ -122,7 +122,7 @@ class AuthorPage extends BasePage {
const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1)); const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
if (bookList.books) { if (bookList.books) {
let books = JSON.parse(bookList.books); let books = bookList.books;
books = this.sortBooks(this.filterBooks(books, query)); books = this.sortBooks(this.filterBooks(books, query));
for (const b of books) { for (const b of books) {

View File

@@ -44,7 +44,7 @@ class SeriesPage extends BasePage {
const bookList = await this.webWorker.getSeriesBookList(query.series.substring(1)); const bookList = await this.webWorker.getSeriesBookList(query.series.substring(1));
if (bookList.books) { if (bookList.books) {
let books = JSON.parse(bookList.books); let books = bookList.books;
const booksAll = this.filterBooks(books, {del: 0}); const booksAll = this.filterBooks(books, {del: 0});
const filtered = (query.all ? booksAll : this.filterBooks(books, query)); const filtered = (query.all ? booksAll : this.filterBooks(books, query));
const sorted = this.sortSeriesBooks(filtered); const sorted = this.sortSeriesBooks(filtered);