Изменения в механизме хранения книг:
теперь ориентируемся на "ключ-filepath", а не "ключ-url"
This commit is contained in:
@@ -900,7 +900,7 @@ class Reader {
|
|||||||
|
|
||||||
refreshBook() {
|
refreshBook() {
|
||||||
const mrb = this.mostRecentBook();
|
const mrb = this.mostRecentBook();
|
||||||
this.loadBook({url: mrb.url, uploadFileName: mrb.uploadFileName, force: true});
|
this.loadBook(Object.assign({}, mrb, {force: true}));
|
||||||
}
|
}
|
||||||
|
|
||||||
undoAction() {
|
undoAction() {
|
||||||
@@ -1091,8 +1091,7 @@ class Reader {
|
|||||||
progress.setState({state: 'parse'});
|
progress.setState({state: 'parse'});
|
||||||
|
|
||||||
// есть ли среди недавних
|
// есть ли среди недавних
|
||||||
const key = bookManager.keyFromUrl(url);
|
let wasOpened = bookManager.findRecentByUrl(url);
|
||||||
let wasOpened = await bookManager.getRecentBook({key});
|
|
||||||
wasOpened = (wasOpened ? wasOpened : {});
|
wasOpened = (wasOpened ? wasOpened : {});
|
||||||
const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
|
const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
|
||||||
const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
|
const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
|
||||||
@@ -1102,7 +1101,7 @@ class Reader {
|
|||||||
|
|
||||||
if (!opts.force) {
|
if (!opts.force) {
|
||||||
// пытаемся загрузить и распарсить книгу в менеджере из локального кэша
|
// пытаемся загрузить и распарсить книгу в менеджере из локального кэша
|
||||||
const bookParsed = await bookManager.getBook({url, path: opts.path}, (prog) => {
|
const bookParsed = await bookManager.getBook({path: (opts.path? opts.path : wasOpened.path)}, (prog) => {
|
||||||
progress.setState({progress: prog});
|
progress.setState({progress: prog});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import localForage from 'localforage';
|
import localForage from 'localforage';
|
||||||
|
import path from 'path-browserify';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
import * as utils from '../../../share/utils';
|
import * as utils from '../../../share/utils';
|
||||||
import BookParser from './BookParser';
|
import BookParser from './BookParser';
|
||||||
|
|
||||||
const maxDataSize = 500*1024*1024;//compressed bytes
|
const maxDataSize = 500*1024*1024;//compressed bytes
|
||||||
|
const maxRecentLength = 5000;
|
||||||
|
|
||||||
//локальный кэш метаданных книг, ограничение maxDataSize
|
//локальный кэш метаданных книг, ограничение maxDataSize
|
||||||
const bmMetaStore = localForage.createInstance({
|
const bmMetaStore = localForage.createInstance({
|
||||||
@@ -36,7 +38,7 @@ class BookManager {
|
|||||||
|
|
||||||
this.saveRecentItem = _.debounce(() => {
|
this.saveRecentItem = _.debounce(() => {
|
||||||
bmRecentStoreNew.setItem('recent-item', this.recentItem);
|
bmRecentStoreNew.setItem('recent-item', this.recentItem);
|
||||||
this.recentRev = (this.recentRev < 1000 ? this.recentRev + 1 : 1);
|
this.recentRev = (this.recentRev < maxRecentLength ? this.recentRev + 1 : 1);
|
||||||
bmRecentStoreNew.setItem('rev', this.recentRev);
|
bmRecentStoreNew.setItem('rev', this.recentRev);
|
||||||
}, 200, {maxWait: 300});
|
}, 200, {maxWait: 300});
|
||||||
|
|
||||||
@@ -51,6 +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();
|
||||||
|
|
||||||
this.recentLastKey = await bmRecentStoreNew.getItem('recent-last-key');
|
this.recentLastKey = await bmRecentStoreNew.getItem('recent-last-key');
|
||||||
if (this.recentLastKey) {
|
if (this.recentLastKey) {
|
||||||
const meta = await bmMetaStore.getItem(`bmMeta-${this.recentLastKey}`);
|
const meta = await bmMetaStore.getItem(`bmMeta-${this.recentLastKey}`);
|
||||||
@@ -67,6 +71,36 @@ class BookManager {
|
|||||||
this.loadStored();//no await
|
this.loadStored();//no await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: убрать в 2025г
|
||||||
|
convertRecent() {
|
||||||
|
let converted = false;
|
||||||
|
|
||||||
|
for (const key in this.recent) {
|
||||||
|
const book = this.recent[key];
|
||||||
|
|
||||||
|
if (!book.path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log(converted);
|
||||||
|
if (converted) {
|
||||||
|
(async() => {
|
||||||
|
await utils.sleep(5000);
|
||||||
|
this.saveRecent();
|
||||||
|
this.emit('recent-changed');
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Ленивая асинхронная загрузка bmMetaStore
|
//Ленивая асинхронная загрузка bmMetaStore
|
||||||
async loadStored() {
|
async loadStored() {
|
||||||
//даем время для загрузки последней читаемой книги, чтобы не блокировать приложение
|
//даем время для загрузки последней читаемой книги, чтобы не блокировать приложение
|
||||||
@@ -193,7 +227,7 @@ class BookManager {
|
|||||||
|
|
||||||
async addBook(newBook, callback) {
|
async addBook(newBook, callback) {
|
||||||
let meta = {url: newBook.url, path: newBook.path};
|
let meta = {url: newBook.url, path: newBook.path};
|
||||||
meta.key = this.keyFromUrl(meta.url);
|
meta.key = this.keyFromPath(meta.path);
|
||||||
meta.addTime = Date.now();
|
meta.addTime = Date.now();
|
||||||
|
|
||||||
const cb = (perc) => {
|
const cb = (perc) => {
|
||||||
@@ -229,10 +263,10 @@ class BookManager {
|
|||||||
async hasBookParsed(meta) {
|
async hasBookParsed(meta) {
|
||||||
if (!this.books)
|
if (!this.books)
|
||||||
return false;
|
return false;
|
||||||
if (!meta.url)
|
if (!meta.path)
|
||||||
return false;
|
return false;
|
||||||
if (!meta.key)
|
if (!meta.key)
|
||||||
meta.key = this.keyFromUrl(meta.url);
|
meta.key = this.keyFromPath(meta.path);
|
||||||
|
|
||||||
let book = this.books[meta.key];
|
let book = this.books[meta.key];
|
||||||
|
|
||||||
@@ -247,8 +281,12 @@ class BookManager {
|
|||||||
|
|
||||||
async getBook(meta, callback) {
|
async getBook(meta, callback) {
|
||||||
let result = undefined;
|
let result = undefined;
|
||||||
|
|
||||||
|
if (!meta.path)
|
||||||
|
return;
|
||||||
|
|
||||||
if (!meta.key)
|
if (!meta.key)
|
||||||
meta.key = this.keyFromUrl(meta.url);
|
meta.key = this.keyFromPath(meta.path);
|
||||||
|
|
||||||
result = this.books[meta.key];
|
result = this.books[meta.key];
|
||||||
|
|
||||||
@@ -258,11 +296,6 @@ class BookManager {
|
|||||||
this.books[meta.key] = result;
|
this.books[meta.key] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Если файл на сервере изменился, считаем, что в кеше его нету
|
|
||||||
if (meta.path && result && meta.path != result.path) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result && !result.parsed) {
|
if (result && !result.parsed) {
|
||||||
let data = await bmDataStore.getItem(`bmData-${meta.key}`);
|
let data = await bmDataStore.getItem(`bmData-${meta.key}`);
|
||||||
callback(5);
|
callback(5);
|
||||||
@@ -322,10 +355,13 @@ class BookManager {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
keyFromUrl(url) {
|
/*keyFromUrl(url) {
|
||||||
return utils.stringToHex(url);
|
return utils.stringToHex(url);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
keyFromPath(bookPath) {
|
||||||
|
return path.basename(bookPath);
|
||||||
|
}
|
||||||
//-- recent --------------------------------------------------------------
|
//-- recent --------------------------------------------------------------
|
||||||
async recentSetItem(item = null, skipCheck = false) {
|
async recentSetItem(item = null, skipCheck = false) {
|
||||||
const rev = await bmRecentStoreNew.getItem('rev');
|
const rev = await bmRecentStoreNew.getItem('rev');
|
||||||
@@ -398,7 +434,7 @@ class BookManager {
|
|||||||
const sorted = this.getSortedRecent();
|
const sorted = this.getSortedRecent();
|
||||||
|
|
||||||
let isDel = false;
|
let isDel = false;
|
||||||
for (let i = 1000; i < sorted.length; i++) {
|
for (let i = maxRecentLength; i < sorted.length; i++) {
|
||||||
delete this.recent[sorted[i].key];
|
delete this.recent[sorted[i].key];
|
||||||
isDel = true;
|
isDel = true;
|
||||||
}
|
}
|
||||||
@@ -418,7 +454,7 @@ class BookManager {
|
|||||||
|
|
||||||
let max = 0;
|
let max = 0;
|
||||||
let result = null;
|
let result = null;
|
||||||
for (let key in this.recent) {
|
for (const key in this.recent) {
|
||||||
const book = this.recent[key];
|
const book = this.recent[key];
|
||||||
if (!book.deleted && book.touchTime > max) {
|
if (!book.deleted && book.touchTime > max) {
|
||||||
max = book.touchTime;
|
max = book.touchTime;
|
||||||
@@ -449,6 +485,21 @@ class BookManager {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findRecentByUrl(url) {
|
||||||
|
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) {
|
||||||
|
max = book.addTime;
|
||||||
|
result = book;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
async setRecent(value) {
|
async setRecent(value) {
|
||||||
const mergedRecent = _.cloneDeep(this.recent);
|
const mergedRecent = _.cloneDeep(this.recent);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user