fix(reader): implement signal-based state management and cleanup in EbookReaderComponent (#435)
This commit is contained in:
@@ -100,6 +100,7 @@ export class ReaderEventService {
|
||||
private longHoldTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
private keydownHandler?: (event: KeyboardEvent) => void;
|
||||
private clickedDocs = new WeakSet<Document>();
|
||||
private iframeCleanupFns: (() => void)[] = [];
|
||||
|
||||
private touchStartX = 0;
|
||||
private touchStartY = 0;
|
||||
@@ -120,12 +121,26 @@ export class ReaderEventService {
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
window.removeEventListener('message', this.windowMessageHandler);
|
||||
for (const fn of this.iframeCleanupFns) fn();
|
||||
this.iframeCleanupFns = [];
|
||||
if (this.keydownHandler) {
|
||||
document.removeEventListener('keydown', this.keydownHandler);
|
||||
this.keydownHandler = undefined;
|
||||
}
|
||||
if (this.longHoldTimeout) {
|
||||
clearTimeout(this.longHoldTimeout);
|
||||
this.longHoldTimeout = null;
|
||||
}
|
||||
if (this.selectionChangeTimeout) {
|
||||
clearTimeout(this.selectionChangeTimeout);
|
||||
this.selectionChangeTimeout = null;
|
||||
}
|
||||
this.view = null;
|
||||
this.viewCallbacks = null;
|
||||
this.isNavigating = false;
|
||||
this.lastClickTime = 0;
|
||||
this.lastClickZone = null;
|
||||
this.clickedDocs = new WeakSet<Document>();
|
||||
}
|
||||
|
||||
@@ -141,7 +156,9 @@ export class ReaderEventService {
|
||||
this.eventSubject.next({type: 'load', detail: e.detail});
|
||||
if (e.detail?.doc) {
|
||||
if (this.keydownHandler) {
|
||||
e.detail.doc.addEventListener('keydown', this.keydownHandler);
|
||||
const handler = this.keydownHandler;
|
||||
e.detail.doc.addEventListener('keydown', handler);
|
||||
this.iframeCleanupFns.push(() => e.detail?.doc?.removeEventListener('keydown', handler));
|
||||
}
|
||||
this.attachIframeEventHandlers(e.detail.doc);
|
||||
}
|
||||
@@ -234,12 +251,14 @@ export class ReaderEventService {
|
||||
document.addEventListener('keydown', this.keydownHandler);
|
||||
}
|
||||
|
||||
private readonly windowMessageHandler = (event: MessageEvent): void => {
|
||||
if (this.isIframeClickMessage(event.data)) {
|
||||
this.handleIframeClickMessage(event.data);
|
||||
}
|
||||
};
|
||||
|
||||
private attachWindowMessageHandler(): void {
|
||||
window.addEventListener('message', (event) => {
|
||||
if (this.isIframeClickMessage(event.data)) {
|
||||
this.handleIframeClickMessage(event.data);
|
||||
}
|
||||
});
|
||||
window.addEventListener('message', this.windowMessageHandler);
|
||||
}
|
||||
|
||||
private isIframeClickMessage(value: unknown): value is IframeClickMessage {
|
||||
@@ -252,17 +271,22 @@ export class ReaderEventService {
|
||||
}
|
||||
this.clickedDocs.add(doc);
|
||||
|
||||
doc.addEventListener('mousedown', () => {
|
||||
const track = (target: EventTarget, type: string, handler: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void => {
|
||||
target.addEventListener(type, handler, options);
|
||||
this.iframeCleanupFns.push(() => target.removeEventListener(type, handler, options));
|
||||
};
|
||||
|
||||
track(doc, 'mousedown', () => {
|
||||
this.longHoldTimeout = setTimeout(() => {
|
||||
this.longHoldTimeout = null;
|
||||
}, this.LONG_HOLD_THRESHOLD_MS);
|
||||
}, true);
|
||||
}, {capture: true});
|
||||
|
||||
doc.addEventListener('mouseup', () => {
|
||||
track(doc, 'mouseup', () => {
|
||||
this.handleSelectionEnd(doc);
|
||||
});
|
||||
|
||||
doc.addEventListener('click', (event: MouseEvent) => {
|
||||
track(doc, 'click', ((event: MouseEvent) => {
|
||||
// Ignore synthesized mouse events that follow touch events
|
||||
if (Date.now() - this.lastTouchTime < 500) {
|
||||
return;
|
||||
@@ -284,21 +308,21 @@ export class ReaderEventService {
|
||||
eventClientX: event.clientX,
|
||||
target: (event.target as HTMLElement)?.tagName
|
||||
}, '*');
|
||||
}, true);
|
||||
}) as EventListener, {capture: true});
|
||||
|
||||
doc.addEventListener('touchstart', (event: TouchEvent) => {
|
||||
track(doc, 'touchstart', ((event: TouchEvent) => {
|
||||
this.handleTouchStart(event, doc);
|
||||
}, {passive: true});
|
||||
}) as EventListener, {passive: true});
|
||||
|
||||
doc.addEventListener('touchmove', (event: TouchEvent) => {
|
||||
track(doc, 'touchmove', ((event: TouchEvent) => {
|
||||
this.handleTouchMove(event, doc);
|
||||
}, {passive: false});
|
||||
}) as EventListener, {passive: false});
|
||||
|
||||
doc.addEventListener('touchend', (event: TouchEvent) => {
|
||||
track(doc, 'touchend', ((event: TouchEvent) => {
|
||||
this.handleTouchEnd(event, doc);
|
||||
}, {passive: false});
|
||||
}) as EventListener, {passive: false});
|
||||
|
||||
doc.addEventListener('selectionchange', () => {
|
||||
track(doc, 'selectionchange', () => {
|
||||
this.handleSelectionChange(doc);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
<div
|
||||
class="reader"
|
||||
[class.immersive]="immersiveMode"
|
||||
[class.immersive]="immersiveMode()"
|
||||
[style.background-color]="readerState().theme.bg ?? ''">
|
||||
|
||||
<div class="header-trigger-zone"
|
||||
[class.inactive]="headerVisible || immersiveMode"
|
||||
[class.inactive]="headerVisible() || immersiveMode()"
|
||||
(mouseenter)="onHeaderTriggerZoneEnter()"></div>
|
||||
<div class="footer-trigger-zone"
|
||||
[class.inactive]="forceNavbarVisible || immersiveMode"
|
||||
[class.inactive]="forceNavbarVisible() || immersiveMode()"
|
||||
(mouseenter)="onFooterTriggerZoneEnter()"></div>
|
||||
|
||||
@if (isLoading) {
|
||||
@if (isLoading()) {
|
||||
<div class="loader-overlay">
|
||||
<div class="loader-content">
|
||||
<div class="spinner"></div>
|
||||
@@ -33,37 +33,37 @@
|
||||
</div>
|
||||
|
||||
<app-reader-navbar
|
||||
[progressData]="currentProgressData"
|
||||
[forceVisible]="forceNavbarVisible"
|
||||
[sectionFractions]="sectionFractions"
|
||||
[progressData]="progressData()"
|
||||
[forceVisible]="forceNavbarVisible()"
|
||||
[sectionFractions]="sectionFractions()"
|
||||
(progressChange)="onProgressChange($event)">
|
||||
</app-reader-navbar>
|
||||
|
||||
<app-reader-sidebar></app-reader-sidebar>
|
||||
<app-reader-left-sidebar></app-reader-left-sidebar>
|
||||
|
||||
@if (showQuickSettings) {
|
||||
@if (showQuickSettings()) {
|
||||
<app-reader-quick-settings
|
||||
[stateService]="stateService"
|
||||
[bookId]="bookId"
|
||||
(closed)="showQuickSettings = false"
|
||||
(openFullSettings)="showControls = true">
|
||||
(closed)="showQuickSettings.set(false)"
|
||||
(openFullSettings)="showControls.set(true)">
|
||||
</app-reader-quick-settings>
|
||||
}
|
||||
|
||||
@if (showControls) {
|
||||
@if (showControls()) {
|
||||
<app-settings-dialog
|
||||
[stateService]="stateService"
|
||||
[viewManager]="viewManager"
|
||||
[bookId]="bookId"
|
||||
(closed)="showControls = false">
|
||||
(closed)="showControls.set(false)">
|
||||
</app-settings-dialog>
|
||||
}
|
||||
|
||||
@if (showMetadata) {
|
||||
@if (showMetadata()) {
|
||||
<app-reader-book-metadata-dialog
|
||||
[book]="book"
|
||||
(closed)="showMetadata = false"
|
||||
[book]="book()"
|
||||
(closed)="showMetadata.set(false)"
|
||||
></app-reader-book-metadata-dialog>
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@
|
||||
></app-reader-note-dialog>
|
||||
}
|
||||
|
||||
@if (showShortcutsHelp) {
|
||||
<app-ebook-shortcuts-help (closed)="showShortcutsHelp = false"></app-ebook-shortcuts-help>
|
||||
@if (showShortcutsHelp()) {
|
||||
<app-ebook-shortcuts-help (closed)="showShortcutsHelp.set(false)"></app-ebook-shortcuts-help>
|
||||
}
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Component, CUSTOM_ELEMENTS_SCHEMA, effect, HostListener, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, effect, HostListener, inject, OnInit, signal} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {from, Observable, of, Subject, throwError} from 'rxjs';
|
||||
import {catchError, map, switchMap, takeUntil, tap} from 'rxjs/operators';
|
||||
@@ -69,10 +69,12 @@ import {WakeLockService} from '../../../shared/service/wake-lock.service';
|
||||
ReaderNoteService
|
||||
],
|
||||
templateUrl: './ebook-reader.component.html',
|
||||
styleUrls: ['./ebook-reader.component.scss']
|
||||
styleUrls: ['./ebook-reader.component.scss'],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush
|
||||
})
|
||||
export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
export class EbookReaderComponent implements OnInit {
|
||||
private destroy$ = new Subject<void>();
|
||||
private destroyRef = inject(DestroyRef);
|
||||
private loaderService = inject(ReaderLoaderService);
|
||||
private styleService = inject(ReaderStyleService);
|
||||
private bookService = inject(BookService);
|
||||
@@ -100,18 +102,19 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
private relocateTimeout?: ReturnType<typeof setTimeout>;
|
||||
private sectionFractionsTimeout?: ReturnType<typeof setTimeout>;
|
||||
|
||||
isLoading = true;
|
||||
showQuickSettings = false;
|
||||
showControls = false;
|
||||
showMetadata = false;
|
||||
forceNavbarVisible = false;
|
||||
headerVisible = false;
|
||||
book: Book | null = null;
|
||||
sectionFractions: number[] = [];
|
||||
isFullscreen = false;
|
||||
showShortcutsHelp = false;
|
||||
immersiveMode = false;
|
||||
isLoading = signal(true);
|
||||
showQuickSettings = signal(false);
|
||||
showControls = signal(false);
|
||||
showMetadata = signal(false);
|
||||
forceNavbarVisible = signal(false);
|
||||
headerVisible = signal(false);
|
||||
book = signal<Book | null>(null);
|
||||
sectionFractions = signal<number[]>([]);
|
||||
isFullscreen = signal(false);
|
||||
showShortcutsHelp = signal(false);
|
||||
immersiveMode = signal(false);
|
||||
private immersiveAutoHideTimer?: ReturnType<typeof setTimeout>;
|
||||
protected progressData = signal<RelocateProgressData | null>(null);
|
||||
|
||||
readonly readerState = this.stateService.state;
|
||||
readonly selectionState = this.selectionService.state;
|
||||
@@ -119,6 +122,32 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
readonly isCurrentCfiBookmarked = this.headerService.isCurrentCfiBookmarked;
|
||||
|
||||
constructor() {
|
||||
this.destroyRef.onDestroy(() => {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
|
||||
this.wakeLockService.disable();
|
||||
this.viewManager.destroy();
|
||||
this.annotationService.reset();
|
||||
this.progressService.endSession();
|
||||
this.progressService.reset();
|
||||
this.selectionService.reset();
|
||||
this.sidebarService.reset();
|
||||
this.leftSidebarService.reset();
|
||||
this.headerService.reset();
|
||||
this.noteService.reset();
|
||||
this.epubCustomFontService.cleanup();
|
||||
|
||||
if (this.immersiveAutoHideTimer) clearTimeout(this.immersiveAutoHideTimer);
|
||||
if (this.relocateTimeout) clearTimeout(this.relocateTimeout);
|
||||
if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout);
|
||||
|
||||
if (this._fileUrl) {
|
||||
URL.revokeObjectURL(this._fileUrl);
|
||||
this._fileUrl = null;
|
||||
}
|
||||
});
|
||||
|
||||
effect(() => {
|
||||
this.stateService.state();
|
||||
this.applyStyles();
|
||||
@@ -133,29 +162,25 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
);
|
||||
}
|
||||
|
||||
get currentProgressData(): RelocateProgressData | null {
|
||||
return this.progressService.currentProgressData;
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.visibilityManager = new ReaderHeaderFooterVisibilityManager(window.innerHeight);
|
||||
this.visibilityManager.onStateChange((state) => {
|
||||
this.headerVisible = state.headerVisible;
|
||||
this.headerVisible.set(state.headerVisible);
|
||||
this.headerService.setForceVisible(state.headerVisible);
|
||||
this.forceNavbarVisible = state.footerVisible;
|
||||
this.forceNavbarVisible.set(state.footerVisible);
|
||||
});
|
||||
|
||||
this.sidebarService.showMetadata$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(() => this.showMetadata = true);
|
||||
.subscribe(() => this.showMetadata.set(true));
|
||||
|
||||
this.headerService.showControls$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(() => this.showQuickSettings = true);
|
||||
.subscribe(() => this.showQuickSettings.set(true));
|
||||
|
||||
this.headerService.showMetadata$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(() => this.showMetadata = true);
|
||||
.subscribe(() => this.showMetadata.set(true));
|
||||
|
||||
this.headerService.toggleFullscreen$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
@@ -163,12 +188,12 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
|
||||
this.headerService.showShortcutsHelp$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(() => this.showShortcutsHelp = true);
|
||||
.subscribe(() => this.showShortcutsHelp.set(true));
|
||||
|
||||
// Enable wake lock after a short delay
|
||||
setTimeout(() => this.wakeLockService.enable(), 1000);
|
||||
|
||||
this.isLoading = true;
|
||||
this.isLoading.set(true);
|
||||
this.initializeFoliate().pipe(
|
||||
switchMap(() => this.epubCustomFontService.loadAndCacheFonts()),
|
||||
tap(() => this.stateService.refreshCustomFonts()),
|
||||
@@ -177,39 +202,15 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
this.subscribeToViewEvents();
|
||||
}),
|
||||
switchMap(() => this.loadBookFromAPI()),
|
||||
tap(() => this.isLoading = false),
|
||||
tap(() => this.isLoading.set(false)),
|
||||
catchError(() => {
|
||||
this.isLoading = false;
|
||||
this.isLoading.set(false);
|
||||
return of(null);
|
||||
}),
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.wakeLockService.disable();
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
this.viewManager.destroy();
|
||||
this.annotationService.reset();
|
||||
this.progressService.endSession();
|
||||
this.progressService.reset();
|
||||
this.selectionService.reset();
|
||||
this.sidebarService.reset();
|
||||
this.leftSidebarService.reset();
|
||||
this.headerService.reset();
|
||||
this.noteService.reset();
|
||||
this.epubCustomFontService.cleanup();
|
||||
|
||||
if (this.immersiveAutoHideTimer) {
|
||||
clearTimeout(this.immersiveAutoHideTimer);
|
||||
}
|
||||
if (this._fileUrl) {
|
||||
URL.revokeObjectURL(this._fileUrl);
|
||||
this._fileUrl = null;
|
||||
}
|
||||
}
|
||||
|
||||
private initializeFoliate(): Observable<void> {
|
||||
return this.loaderService.loadFoliateScript().pipe(
|
||||
switchMap(() => this.loaderService.waitForCustomElement())
|
||||
@@ -232,7 +233,7 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
|
||||
return from(this.bookService.fetchFreshBookDetail(this.bookId, false)).pipe(
|
||||
switchMap((book) => {
|
||||
this.book = book;
|
||||
this.book.set(book);
|
||||
|
||||
// Use alternative bookType from query param if provided, otherwise use primary
|
||||
const bookType = (this.altBookType as BookType | undefined) ?? book.primaryFile?.bookType;
|
||||
@@ -317,6 +318,7 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
if (this.relocateTimeout) clearTimeout(this.relocateTimeout);
|
||||
this.relocateTimeout = setTimeout(() => {
|
||||
this.progressService.handleRelocateEvent(event.detail);
|
||||
this.progressData.set(this.progressService.currentProgressData);
|
||||
this.updateBookmarkIndicator();
|
||||
}, 100);
|
||||
|
||||
@@ -326,7 +328,7 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
}, 500);
|
||||
break;
|
||||
case 'middle-single-tap':
|
||||
if (this.immersiveMode) {
|
||||
if (this.immersiveMode()) {
|
||||
this.immersiveTemporaryShow();
|
||||
} else {
|
||||
this.toggleHeaderNavbarPinned();
|
||||
@@ -339,7 +341,7 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
this.toggleFullscreen();
|
||||
break;
|
||||
case 'toggle-shortcuts-help':
|
||||
this.showShortcutsHelp = !this.showShortcutsHelp;
|
||||
this.showShortcutsHelp.update(v => !v);
|
||||
break;
|
||||
case 'toggle-immersive':
|
||||
this.toggleImmersiveMode();
|
||||
@@ -364,17 +366,17 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
this.leftSidebarService.toggle('notes');
|
||||
break;
|
||||
case 'escape-pressed':
|
||||
if (this.showShortcutsHelp) {
|
||||
this.showShortcutsHelp = false;
|
||||
if (this.showShortcutsHelp()) {
|
||||
this.showShortcutsHelp.set(false);
|
||||
} else if (this.noteDialogState().visible) {
|
||||
this.noteService.closeDialog();
|
||||
} else if (this.showControls) {
|
||||
this.showControls = false;
|
||||
} else if (this.showQuickSettings) {
|
||||
this.showQuickSettings = false;
|
||||
} else if (this.showMetadata) {
|
||||
this.showMetadata = false;
|
||||
} else if (this.isFullscreen) {
|
||||
} else if (this.showControls()) {
|
||||
this.showControls.set(false);
|
||||
} else if (this.showQuickSettings()) {
|
||||
this.showQuickSettings.set(false);
|
||||
} else if (this.showMetadata()) {
|
||||
this.showMetadata.set(false);
|
||||
} else if (this.isFullscreen()) {
|
||||
this.exitFullscreen();
|
||||
}
|
||||
break;
|
||||
@@ -383,7 +385,7 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
private updateSectionFractions(): void {
|
||||
this.sectionFractions = this.viewManager.getSectionFractions();
|
||||
this.sectionFractions.set(this.viewManager.getSectionFractions());
|
||||
}
|
||||
|
||||
private updateBookmarkIndicator(): void {
|
||||
@@ -407,8 +409,8 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
|
||||
@HostListener('document:fullscreenchange')
|
||||
onFullscreenChange(): void {
|
||||
this.isFullscreen = !!document.fullscreenElement;
|
||||
this.headerService.setFullscreen(this.isFullscreen);
|
||||
this.isFullscreen.set(!!document.fullscreenElement);
|
||||
this.headerService.setFullscreen(this.isFullscreen());
|
||||
}
|
||||
|
||||
toggleFullscreen(): void {
|
||||
@@ -464,16 +466,13 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
toggleImmersiveMode(): void {
|
||||
this.immersiveMode = !this.immersiveMode;
|
||||
if (this.immersiveMode) {
|
||||
this.visibilityManager.setImmersive(true);
|
||||
} else {
|
||||
this.visibilityManager.setImmersive(false);
|
||||
}
|
||||
const newValue = !this.immersiveMode();
|
||||
this.immersiveMode.set(newValue);
|
||||
this.visibilityManager.setImmersive(newValue);
|
||||
}
|
||||
|
||||
private immersiveTemporaryShow(): void {
|
||||
if (!this.immersiveMode) return;
|
||||
if (!this.immersiveMode()) return;
|
||||
this.visibilityManager.temporaryShow();
|
||||
if (this.immersiveAutoHideTimer) clearTimeout(this.immersiveAutoHideTimer);
|
||||
this.immersiveAutoHideTimer = setTimeout(() => {
|
||||
|
||||
Reference in New Issue
Block a user