fix(reader): fix kobo progress race + apply in-chapter progress (#859)

This commit is contained in:
Alex
2026-04-25 14:31:33 +01:00
committed by GitHub
parent cb3e4af5a2
commit bdd54a4565
@@ -36,6 +36,12 @@ import {RelocateProgressData} from './state/progress.service';
import {WakeLockService} from '../../../shared/service/wake-lock.service'; import {WakeLockService} from '../../../shared/service/wake-lock.service';
import {ViewEvent} from './core/view-manager.service'; import {ViewEvent} from './core/view-manager.service';
interface PendingInitialChapterRestore {
href: string;
contentSourceProgressPercent: number;
attempts: number;
}
@Component({ @Component({
selector: 'app-ebook-reader', selector: 'app-ebook-reader',
standalone: true, standalone: true,
@@ -75,6 +81,8 @@ import {ViewEvent} from './core/view-manager.service';
export class EbookReaderComponent implements OnInit { export class EbookReaderComponent implements OnInit {
private readonly destroyRef = inject(DestroyRef); private readonly destroyRef = inject(DestroyRef);
private static readonly MAX_CHAPTER_PROGRESS_PERCENT = 99.9; private static readonly MAX_CHAPTER_PROGRESS_PERCENT = 99.9;
private static readonly INITIAL_CHAPTER_RESTORE_RETRY_MS = 100;
private static readonly INITIAL_CHAPTER_RESTORE_MAX_ATTEMPTS = 15;
private loaderService = inject(ReaderLoaderService); private loaderService = inject(ReaderLoaderService);
private styleService = inject(ReaderStyleService); private styleService = inject(ReaderStyleService);
private bookService = inject(BookService); private bookService = inject(BookService);
@@ -102,7 +110,8 @@ export class EbookReaderComponent implements OnInit {
private visibilityManager!: ReaderHeaderFooterVisibilityManager; private visibilityManager!: ReaderHeaderFooterVisibilityManager;
private relocateTimeout?: ReturnType<typeof setTimeout>; private relocateTimeout?: ReturnType<typeof setTimeout>;
private sectionFractionsTimeout?: ReturnType<typeof setTimeout>; private sectionFractionsTimeout?: ReturnType<typeof setTimeout>;
private pendingInitialChapterRestore: { href: string; contentSourceProgressPercent: number } | null = null; private pendingInitialChapterRestore: PendingInitialChapterRestore | null = null;
private pendingInitialChapterRestoreTimeout?: ReturnType<typeof setTimeout>;
isLoading = signal(true); isLoading = signal(true);
showQuickSettings = signal(false); showQuickSettings = signal(false);
@@ -140,6 +149,7 @@ export class EbookReaderComponent implements OnInit {
if (this.immersiveAutoHideTimer) clearTimeout(this.immersiveAutoHideTimer); if (this.immersiveAutoHideTimer) clearTimeout(this.immersiveAutoHideTimer);
if (this.relocateTimeout) clearTimeout(this.relocateTimeout); if (this.relocateTimeout) clearTimeout(this.relocateTimeout);
if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout); if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout);
if (this.pendingInitialChapterRestoreTimeout) clearTimeout(this.pendingInitialChapterRestoreTimeout);
if (this._fileUrl) { if (this._fileUrl) {
URL.revokeObjectURL(this._fileUrl); URL.revokeObjectURL(this._fileUrl);
@@ -251,15 +261,7 @@ export class EbookReaderComponent implements OnInit {
switchMap(() => { switchMap(() => {
if (!this.hasLoadedOnce) { if (!this.hasLoadedOnce) {
this.hasLoadedOnce = true; this.hasLoadedOnce = true;
if (book.epubProgress?.cfi) { return this.restoreSavedPosition(book);
return this.viewManager.goTo(book.epubProgress.cfi);
} else if (book.epubProgress?.href) {
return this.viewManager.goTo(book.epubProgress.href);
} else if (book.epubProgress?.percentage && book.epubProgress.percentage > 0) {
return this.viewManager.goToFraction(book.epubProgress.percentage / 100);
} else {
return this.viewManager.goTo(0);
}
} }
return of(undefined); return of(undefined);
}) })
@@ -313,18 +315,12 @@ export class EbookReaderComponent implements OnInit {
break; break;
case 'relocate': case 'relocate':
if (this.handlePendingInitialChapterRestore(event.detail)) { if (this.handlePendingInitialChapterRestore(event.detail)) {
if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout);
this.sectionFractionsTimeout = setTimeout(() => {
this.updateSectionFractions();
}, 500);
break; break;
} }
if (this.relocateTimeout) clearTimeout(this.relocateTimeout); if (this.relocateTimeout) clearTimeout(this.relocateTimeout);
this.relocateTimeout = setTimeout(() => { this.relocateTimeout = setTimeout(() => {
this.progressService.handleRelocateEvent(event.detail); this.handleRelocateProgress(event.detail);
this.progressData.set(this.progressService.currentProgressData);
this.updateBookmarkIndicator();
}, 100); }, 100);
if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout); if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout);
@@ -394,6 +390,8 @@ export class EbookReaderComponent implements OnInit {
} }
private restoreSavedPosition(book: Book): Observable<void> { private restoreSavedPosition(book: Book): Observable<void> {
this.pendingInitialChapterRestore = null;
const progress = book.epubProgress; const progress = book.epubProgress;
if (progress?.cfi) { if (progress?.cfi) {
@@ -405,16 +403,13 @@ export class EbookReaderComponent implements OnInit {
if (typeof chapterProgress === 'number' && Number.isFinite(chapterProgress) && chapterProgress > 0) { if (typeof chapterProgress === 'number' && Number.isFinite(chapterProgress) && chapterProgress > 0) {
this.pendingInitialChapterRestore = { this.pendingInitialChapterRestore = {
href: this.normalizeHref(progress.href), href: this.normalizeHref(progress.href),
contentSourceProgressPercent: chapterProgress contentSourceProgressPercent: chapterProgress,
attempts: 0
}; };
} else {
this.pendingInitialChapterRestore = null;
} }
return this.viewManager.goTo(progress.href); return this.viewManager.goTo(progress.href);
} }
this.pendingInitialChapterRestore = null;
if (progress?.percentage && progress.percentage > 0) { if (progress?.percentage && progress.percentage > 0) {
return this.viewManager.goToFraction(progress.percentage / 100); return this.viewManager.goToFraction(progress.percentage / 100);
} }
@@ -423,22 +418,26 @@ export class EbookReaderComponent implements OnInit {
} }
private handlePendingInitialChapterRestore(detail: RelocateProgressData): boolean { private handlePendingInitialChapterRestore(detail: RelocateProgressData): boolean {
if (!this.pendingInitialChapterRestore) { const pendingRestore = this.pendingInitialChapterRestore;
if (!pendingRestore) {
return false; return false;
} }
const currentHref = this.normalizeHref(detail.pageItem?.href ?? detail.tocItem?.href ?? null); const currentHref = this.normalizeHref(detail.pageItem?.href ?? detail.tocItem?.href ?? null);
if (!currentHref || !this.hrefsMatch(currentHref, this.pendingInitialChapterRestore.href)) { if (!currentHref) {
return false; return this.retryInitialRestore(detail);
}
if (!this.hrefsMatch(currentHref, pendingRestore.href)) {
return this.retryInitialRestore(detail);
} }
const targetFraction = this.resolveChapterFraction( const targetFraction = this.resolveChapterFraction(
detail.section?.current, detail.section?.current,
this.pendingInitialChapterRestore.contentSourceProgressPercent pendingRestore.contentSourceProgressPercent
); );
if (targetFraction === null) { if (targetFraction === null) {
return false; return this.retryInitialRestore(detail);
} }
if (typeof detail.fraction === 'number' && Math.abs(detail.fraction - targetFraction) < 0.0001) { if (typeof detail.fraction === 'number' && Math.abs(detail.fraction - targetFraction) < 0.0001) {
@@ -447,13 +446,43 @@ export class EbookReaderComponent implements OnInit {
} }
this.pendingInitialChapterRestore = null; this.pendingInitialChapterRestore = null;
if (this.pendingInitialChapterRestoreTimeout) {
clearTimeout(this.pendingInitialChapterRestoreTimeout);
this.pendingInitialChapterRestoreTimeout = undefined;
}
this.viewManager.goToFraction(targetFraction) this.viewManager.goToFraction(targetFraction)
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(); .subscribe();
return true; return true;
} }
private retryInitialRestore(detail: RelocateProgressData): boolean {
const pendingRestore = this.pendingInitialChapterRestore;
if (!pendingRestore) {
return false;
}
if (pendingRestore.attempts >= EbookReaderComponent.INITIAL_CHAPTER_RESTORE_MAX_ATTEMPTS) {
this.pendingInitialChapterRestore = null;
this.handleRelocateProgress(detail);
return true;
}
pendingRestore.attempts += 1;
if (this.pendingInitialChapterRestoreTimeout) clearTimeout(this.pendingInitialChapterRestoreTimeout);
this.pendingInitialChapterRestoreTimeout = setTimeout(() => {
this.updateSectionFractions();
this.handlePendingInitialChapterRestore(detail);
}, EbookReaderComponent.INITIAL_CHAPTER_RESTORE_RETRY_MS);
return true;
}
private handleRelocateProgress(detail: RelocateProgressData): void {
this.progressService.handleRelocateEvent(detail);
this.progressData.set(this.progressService.currentProgressData);
this.updateBookmarkIndicator();
}
private resolveChapterFraction(sectionIndex: number | undefined, chapterProgressPercent: number): number | null { private resolveChapterFraction(sectionIndex: number | undefined, chapterProgressPercent: number): number | null {
if (sectionIndex === undefined) { if (sectionIndex === undefined) {
return null; return null;