Работа над ServerStorage, попутный рефакторинг
This commit is contained in:
@@ -208,11 +208,10 @@ class Reader extends Vue {
|
|||||||
(async() => {
|
(async() => {
|
||||||
await bookManager.init(this.settings);
|
await bookManager.init(this.settings);
|
||||||
bookManager.addEventListener(this.bookManagerEvent);
|
bookManager.addEventListener(this.bookManagerEvent);
|
||||||
await this.$refs.serverStorage.init();
|
|
||||||
|
|
||||||
if (this.$root.rootRoute == '/reader') {
|
if (this.$root.rootRoute == '/reader') {
|
||||||
if (this.routeParamUrl) {
|
if (this.routeParamUrl) {
|
||||||
this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
|
await this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
|
||||||
} else {
|
} else {
|
||||||
this.loaderActive = true;
|
this.loaderActive = true;
|
||||||
}
|
}
|
||||||
@@ -290,13 +289,22 @@ class Reader extends Vue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bookManagerEvent(eventName) {
|
bookManagerEvent(eventName) {
|
||||||
if (eventName == 'recent-changed') {
|
const serverStorage = this.$refs.serverStorage;
|
||||||
const oldBook = this.mostRecentBookReactive;
|
if (eventName == 'load-meta-finish') {
|
||||||
const newBook = bookManager.mostRecentBook();
|
serverStorage.init();
|
||||||
|
}
|
||||||
|
|
||||||
if (oldBook && newBook && oldBook.key != newBook.key) {
|
if (eventName == 'recent-changed') {
|
||||||
this.loadBook(newBook);
|
(async() => {
|
||||||
}
|
const oldBook = this.mostRecentBookReactive;
|
||||||
|
const newBook = bookManager.mostRecentBook();
|
||||||
|
|
||||||
|
if (oldBook && newBook && oldBook.key != newBook.key) {
|
||||||
|
await this.loadBook(newBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
await serverStorage.saveRecent();
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -649,7 +657,7 @@ class Reader extends Vue {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadBook(opts) {
|
async loadBook(opts) {
|
||||||
if (!opts || !opts.url) {
|
if (!opts || !opts.url) {
|
||||||
this.mostRecentBook();
|
this.mostRecentBook();
|
||||||
return;
|
return;
|
||||||
@@ -668,118 +676,120 @@ class Reader extends Vue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.progressActive = true;
|
this.progressActive = true;
|
||||||
this.$nextTick(async() => {
|
|
||||||
const progress = this.$refs.page;
|
|
||||||
|
|
||||||
this.actionList = [];
|
await this.$nextTick()
|
||||||
this.actionCur = -1;
|
|
||||||
|
|
||||||
try {
|
const progress = this.$refs.page;
|
||||||
progress.show();
|
|
||||||
progress.setState({state: 'parse'});
|
|
||||||
|
|
||||||
// есть ли среди недавних
|
this.actionList = [];
|
||||||
const key = bookManager.keyFromUrl(url);
|
this.actionCur = -1;
|
||||||
let wasOpened = await bookManager.getRecentBook({key});
|
|
||||||
wasOpened = (wasOpened ? wasOpened : {});
|
|
||||||
const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
|
|
||||||
const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
|
|
||||||
|
|
||||||
let book = null;
|
try {
|
||||||
|
progress.show();
|
||||||
|
progress.setState({state: 'parse'});
|
||||||
|
|
||||||
if (!opts.force) {
|
// есть ли среди недавних
|
||||||
// пытаемся загрузить и распарсить книгу в менеджере из локального кэша
|
const key = bookManager.keyFromUrl(url);
|
||||||
const bookParsed = await bookManager.getBook({url}, (prog) => {
|
let wasOpened = await bookManager.getRecentBook({key});
|
||||||
progress.setState({progress: prog});
|
wasOpened = (wasOpened ? wasOpened : {});
|
||||||
});
|
const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
|
||||||
|
const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
|
||||||
|
|
||||||
// если есть в локальном кэше
|
let book = null;
|
||||||
if (bookParsed) {
|
|
||||||
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
|
|
||||||
this.mostRecentBook();
|
|
||||||
this.addAction(bookPos);
|
|
||||||
this.loaderActive = false;
|
|
||||||
progress.hide(); this.progressActive = false;
|
|
||||||
this.blinkCachedLoadMessage();
|
|
||||||
|
|
||||||
await this.activateClickMapPage();
|
if (!opts.force) {
|
||||||
return;
|
// пытаемся загрузить и распарсить книгу в менеджере из локального кэша
|
||||||
}
|
const bookParsed = await bookManager.getBook({url}, (prog) => {
|
||||||
|
|
||||||
// иначе идем на сервер
|
|
||||||
// пытаемся загрузить готовый файл с сервера
|
|
||||||
if (wasOpened.path) {
|
|
||||||
try {
|
|
||||||
const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
|
|
||||||
progress.setState(state);
|
|
||||||
});
|
|
||||||
book = Object.assign({}, wasOpened, {data: resp.data});
|
|
||||||
} catch (e) {
|
|
||||||
//молчим
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
progress.setState({totalSteps: 5});
|
|
||||||
|
|
||||||
// не удалось, скачиваем книгу полностью с конвертацией
|
|
||||||
let loadCached = true;
|
|
||||||
if (!book) {
|
|
||||||
book = await readerApi.loadBook(url, (state) => {
|
|
||||||
progress.setState(state);
|
|
||||||
});
|
|
||||||
loadCached = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// добавляем в bookManager
|
|
||||||
progress.setState({state: 'parse', step: 5});
|
|
||||||
const addedBook = await bookManager.addBook(book, (prog) => {
|
|
||||||
progress.setState({progress: prog});
|
progress.setState({progress: prog});
|
||||||
});
|
});
|
||||||
|
|
||||||
// добавляем в историю
|
// если есть в локальном кэше
|
||||||
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, addedBook));
|
if (bookParsed) {
|
||||||
this.mostRecentBook();
|
await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
|
||||||
this.addAction(bookPos);
|
this.mostRecentBook();
|
||||||
this.updateRoute(true);
|
this.addAction(bookPos);
|
||||||
|
this.loaderActive = false;
|
||||||
this.loaderActive = false;
|
progress.hide(); this.progressActive = false;
|
||||||
progress.hide(); this.progressActive = false;
|
|
||||||
if (loadCached) {
|
|
||||||
this.blinkCachedLoadMessage();
|
this.blinkCachedLoadMessage();
|
||||||
} else
|
|
||||||
this.stopBlink = true;
|
|
||||||
|
|
||||||
await this.activateClickMapPage();
|
await this.activateClickMapPage();
|
||||||
} catch (e) {
|
return;
|
||||||
progress.hide(); this.progressActive = false;
|
}
|
||||||
this.loaderActive = true;
|
|
||||||
this.$alert(e.message, 'Ошибка', {type: 'error'});
|
// иначе идем на сервер
|
||||||
|
// пытаемся загрузить готовый файл с сервера
|
||||||
|
if (wasOpened.path) {
|
||||||
|
try {
|
||||||
|
const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
|
||||||
|
progress.setState(state);
|
||||||
|
});
|
||||||
|
book = Object.assign({}, wasOpened, {data: resp.data});
|
||||||
|
} catch (e) {
|
||||||
|
//молчим
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
loadFile(opts) {
|
progress.setState({totalSteps: 5});
|
||||||
this.progressActive = true;
|
|
||||||
this.$nextTick(async() => {
|
|
||||||
const progress = this.$refs.page;
|
|
||||||
try {
|
|
||||||
progress.show();
|
|
||||||
progress.setState({state: 'upload'});
|
|
||||||
|
|
||||||
const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
|
// не удалось, скачиваем книгу полностью с конвертацией
|
||||||
|
let loadCached = true;
|
||||||
|
if (!book) {
|
||||||
|
book = await readerApi.loadBook(url, (state) => {
|
||||||
progress.setState(state);
|
progress.setState(state);
|
||||||
});
|
});
|
||||||
|
loadCached = false;
|
||||||
progress.hide(); this.progressActive = false;
|
|
||||||
|
|
||||||
this.loadBook({url});
|
|
||||||
} catch (e) {
|
|
||||||
progress.hide(); this.progressActive = false;
|
|
||||||
this.loaderActive = true;
|
|
||||||
this.$alert(e.message, 'Ошибка', {type: 'error'});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
// добавляем в 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}, addedBook));
|
||||||
|
this.mostRecentBook();
|
||||||
|
this.addAction(bookPos);
|
||||||
|
this.updateRoute(true);
|
||||||
|
|
||||||
|
this.loaderActive = false;
|
||||||
|
progress.hide(); this.progressActive = false;
|
||||||
|
if (loadCached) {
|
||||||
|
this.blinkCachedLoadMessage();
|
||||||
|
} else
|
||||||
|
this.stopBlink = true;
|
||||||
|
|
||||||
|
await this.activateClickMapPage();
|
||||||
|
} catch (e) {
|
||||||
|
progress.hide(); this.progressActive = false;
|
||||||
|
this.loaderActive = true;
|
||||||
|
this.$alert(e.message, 'Ошибка', {type: 'error'});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadFile(opts) {
|
||||||
|
this.progressActive = true;
|
||||||
|
|
||||||
|
await this.$nextTick();
|
||||||
|
|
||||||
|
const progress = this.$refs.page;
|
||||||
|
try {
|
||||||
|
progress.show();
|
||||||
|
progress.setState({state: 'upload'});
|
||||||
|
|
||||||
|
const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
|
||||||
|
progress.setState(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
progress.hide(); this.progressActive = false;
|
||||||
|
|
||||||
|
await this.loadBook({url});
|
||||||
|
} catch (e) {
|
||||||
|
progress.hide(); this.progressActive = false;
|
||||||
|
this.loaderActive = true;
|
||||||
|
this.$alert(e.message, 'Ошибка', {type: 'error'});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
blinkCachedLoadMessage() {
|
blinkCachedLoadMessage() {
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ class BookManager {
|
|||||||
}
|
}
|
||||||
await bmCacheStore.setItem('books', this.booksCached);
|
await bmCacheStore.setItem('books', this.booksCached);
|
||||||
await bmCacheStore.setItem('recent', this.recent);
|
await bmCacheStore.setItem('recent', this.recent);
|
||||||
|
this.emit('load-meta-finish');
|
||||||
}
|
}
|
||||||
|
|
||||||
async cleanBooks() {
|
async cleanBooks() {
|
||||||
|
|||||||
Reference in New Issue
Block a user