refactor(ebook-reader): optimize destruction handling and improve reactive subscriptions (#478)

This commit is contained in:
brios
2026-04-22 17:27:38 -04:00
committed by Zach Yale
parent e26df36a33
commit 1f5f99d958
26 changed files with 1112 additions and 1068 deletions
@@ -1,12 +1,12 @@
<div #readerRoot class="comic-reader-container" tabindex="0"
[class.rtl-reading]="readingDirection === CbxReadingDirection.RTL"
[class.fullscreen-mode]="isFullscreen" [class.slideshow-active]="isSlideshowActive">
<app-cbx-header [isCurrentPageBookmarked]="isCurrentPageBookmarked"
[currentPageHasNotes]="currentPageHasNotes"></app-cbx-header>
[class.rtl-reading]="readingDirection() === CbxReadingDirection.RTL"
[class.fullscreen-mode]="isFullscreen()" [class.slideshow-active]="isSlideshowActive()">
<app-cbx-header [isCurrentPageBookmarked]="isCurrentPageBookmarked()"
[currentPageHasNotes]="currentPageHasNotes()"></app-cbx-header>
<app-cbx-sidebar></app-cbx-sidebar>
@if (showWebtoonSuggestion) {
@if (showWebtoonSuggestion()) {
<div class="webtoon-suggestion-banner" role="region" [attr.aria-label]="'readerCbx.webtoonHint.regionLabel' | transloco">
<p class="webtoon-suggestion-text">{{ 'readerCbx.webtoonHint.message' | transloco }}</p>
<div class="webtoon-suggestion-actions">
@@ -23,11 +23,11 @@
</div>
}
@if (showQuickSettings) {
@if (showQuickSettings()) {
<app-cbx-quick-settings></app-cbx-quick-settings>
}
@if (isCurrentPageBookmarked) {
@if (isCurrentPageBookmarked()) {
<div class="bookmark-indicator">
<svg width="48" height="64" viewBox="0 0 48 64">
<polygon points="0,0 48,0 48,64 24,48 0,64" fill="red" />
@@ -35,57 +35,57 @@
</div>
}
@if (showNoteDialog) {
<app-cbx-note-dialog [data]="noteDialogData" (saved)="onNoteSave($event)"
@if (showNoteDialog()) {
<app-cbx-note-dialog [data]="noteDialogData()" (saved)="onNoteSave($event)"
(cancelled)="onNoteCancel()"></app-cbx-note-dialog>
}
@if (showShortcutsHelp) {
@if (showShortcutsHelp()) {
<app-cbx-shortcuts-help (closed)="onShortcutsHelpClose()"></app-cbx-shortcuts-help>
}
@if (isSlideshowActive) {
<div class="slideshow-progress" [class.above-footer]="isFooterVisible"
[style.animation-duration.ms]="slideshowInterval"></div>
@if (isSlideshowActive()) {
<div class="slideshow-progress" [class.above-footer]="isFooterVisible()"
[style.animation-duration.ms]="slideshowInterval()"></div>
}
<div #magnifierLens class="magnifier-lens"></div>
<div #imageScrollHost class="image-container" [class.magnifier-active]="isMagnifierActive"
[class.two-page-view]="isTwoPageView && scrollMode === CbxScrollMode.PAGINATED"
[class.infinite-scroll]="scrollMode === CbxScrollMode.INFINITE"
[class.long-strip]="scrollMode === CbxScrollMode.LONG_STRIP"
[class.fit-actual-size]="fitMode === CbxFitMode.ACTUAL_SIZE" [class.fit-page]="fitMode === CbxFitMode.FIT_PAGE"
[class.fit-width]="fitMode === CbxFitMode.FIT_WIDTH" [class.fit-height]="fitMode === CbxFitMode.FIT_HEIGHT"
[class.fit-auto]="fitMode === CbxFitMode.AUTO" [class.bg-black]="backgroundColor === CbxBackgroundColor.BLACK"
[class.bg-gray]="backgroundColor === CbxBackgroundColor.GRAY"
[class.bg-white]="backgroundColor === CbxBackgroundColor.WHITE" [class.emulate-book]="emulateBook && isTwoPageView"
[style.filter]="brightness < 100 ? 'brightness(' + brightness + '%)' : null" (scroll)="onScroll($event)">
@if (!isLoading) {
@if (pages.length > 0) {
@if (scrollMode === CbxScrollMode.PAGINATED) {
<div #imageScrollHost class="image-container" [class.magnifier-active]="isMagnifierActive()"
[class.two-page-view]="isTwoPageView() && scrollMode() === CbxScrollMode.PAGINATED"
[class.infinite-scroll]="scrollMode() === CbxScrollMode.INFINITE"
[class.long-strip]="scrollMode() === CbxScrollMode.LONG_STRIP"
[class.fit-actual-size]="fitMode() === CbxFitMode.ACTUAL_SIZE" [class.fit-page]="fitMode() === CbxFitMode.FIT_PAGE"
[class.fit-width]="fitMode() === CbxFitMode.FIT_WIDTH" [class.fit-height]="fitMode() === CbxFitMode.FIT_HEIGHT"
[class.fit-auto]="fitMode() === CbxFitMode.AUTO" [class.bg-black]="backgroundColor() === CbxBackgroundColor.BLACK"
[class.bg-gray]="backgroundColor() === CbxBackgroundColor.GRAY"
[class.bg-white]="backgroundColor() === CbxBackgroundColor.WHITE" [class.emulate-book]="emulateBook() && isTwoPageView()"
[style.filter]="brightness() < 100 ? 'brightness(' + brightness() + '%)' : null" (scroll)="onScroll($event)">
@if (!isLoading()) {
@if (pages().length > 0) {
@if (scrollMode() === CbxScrollMode.PAGINATED) {
<div class="pages-wrapper" role="button" tabindex="0" (click)="onImageClick()" (keydown.enter)="onImageClick()"
(dblclick)="onImageDoubleClick()">
@if (isPageTransitioning && previousImageUrls.length > 0) {
@if (isPageTransitioning() && previousImageUrls().length > 0) {
<div class="previous-page-layer">
@for (url of previousImageUrls; track url) {
@for (url of previousImageUrls(); track url) {
<img [src]="url" alt="Previous Page" class="page-image previous-image" />
}
</div>
}
<div class="current-page-layer" [class.fade-in]="isPageTransitioning && imagesLoaded">
<div class="current-page-layer" [class.fade-in]="isPageTransitioning() && imagesLoaded()">
@if (shouldUseCanvasRenderer()) {
<app-canvas-renderer [imageUrl]="currentImageUrls[0]" [splitState]="canvasSplitState"
[splitOption]="pageSplitOption">
<app-canvas-renderer [imageUrl]="currentImageUrls()[0]" [splitState]="canvasSplitState"
[splitOption]="pageSplitOption()">
</app-canvas-renderer>
} @else {
@for (url of currentImageUrls; track url; let i = $index) {
<img [src]="url" alt="Page Image" class="page-image" (load)="onPageImageLoad($event, currentPage + i)" />
@for (url of currentImageUrls(); track url; let i = $index) {
<img [src]="url" alt="Page Image" class="page-image" (load)="onPageImageLoad($event, currentPage() + i)" />
}
}
</div>
</div>
@if (isAtLastPage && nextBookInSeries) {
@if (isAtLastPage() && nextBookInSeries) {
<div class="end-of-comic-action">
<button class="next-book-action-button" (click)="navigateToNextBook()">
<span class="action-icon">📖</span>
@@ -94,21 +94,21 @@
</button>
</div>
}
} @else if (scrollMode === CbxScrollMode.LONG_STRIP) {
} @else if (scrollMode() === CbxScrollMode.LONG_STRIP) {
<div class="long-strip-wrapper" role="button" tabindex="0" (click)="onImageClick()"
(keydown.enter)="onImageClick()">
<div class="strip-width-constrain" [style.width]="cbxQuickSettingsState().stripMaxWidthPercent + '%'">
@for (item of longStripImages; track item.page) {
@for (item of longStripImages(); track item.page) {
<img [src]="item.src" alt="Page {{ item.page + 1 }}" class="long-strip-image" [attr.data-page]="item.page"
draggable="false" [attr.loading]="item.page === currentPage ? 'eager' : 'lazy'" decoding="async" (load)="onLongStripImageLoad($event, item.page)" />
draggable="false" [attr.loading]="item.page === currentPage() ? 'eager' : 'lazy'" decoding="async" (load)="onLongStripImageLoad($event, item.page)" />
}
@if (nextBookInSeries) {
<div class="cbx-continuation-spacer" aria-hidden="true"></div>
<div class="cbx-continuation-hint" [class.visible]="continuationHintVisible" aria-live="polite">
<div class="cbx-continuation-hint" [class.visible]="continuationHintVisible()" aria-live="polite">
{{ 'readerCbx.continuation.scrollForNext' | transloco }}
</div>
}
@if (isAtEndOfLongStrip && nextBookInSeries) {
@if (isAtEndOfLongStrip() && nextBookInSeries) {
<div class="end-of-comic-action">
<button class="next-book-action-button" (click)="navigateToNextBook()">
<span class="action-icon">📖</span>
@@ -122,25 +122,25 @@
} @else {
<div class="infinite-scroll-wrapper">
<div class="strip-width-constrain" [style.width]="cbxQuickSettingsState().stripMaxWidthPercent + '%'">
@for (pageIdx of infiniteScrollPages; track pageIdx) {
@for (pageIdx of infiniteScrollPages(); track pageIdx) {
<img [src]="getPageImageUrl(pageIdx)" alt="Page {{ pageIdx + 1 }}" class="page-image"
[attr.data-page]="pageIdx" role="button" tabindex="0" draggable="false"
[attr.loading]="pageIdx === currentPage ? 'eager' : 'lazy'" decoding="async"
[attr.loading]="pageIdx === currentPage() ? 'eager' : 'lazy'" decoding="async"
(click)="onImageClick()" (keydown.enter)="onImageClick()" (dblclick)="onImageDoubleClick()"
(load)="onPageImageLoad($event, pageIdx)" />
}
@if (isLoadingMore) {
@if (isLoadingMore()) {
<div class="loading-more">
<p-progressSpinner class="small-spinner"></p-progressSpinner>
</div>
}
@if (nextBookInSeries) {
<div class="cbx-continuation-spacer" aria-hidden="true"></div>
<div class="cbx-continuation-hint" [class.visible]="continuationHintVisible" aria-live="polite">
<div class="cbx-continuation-hint" [class.visible]="continuationHintVisible()" aria-live="polite">
{{ 'readerCbx.continuation.scrollForNext' | transloco }}
</div>
}
@if (isAtEndOfInfiniteScroll && nextBookInSeries) {
@if (isAtEndOfInfiniteScroll() && nextBookInSeries) {
<div class="end-of-comic-action">
<button class="next-book-action-button" (click)="navigateToNextBook()">
<span class="action-icon">📖</span>
@@ -153,9 +153,9 @@
</div>
}
@if (clickToPaginate) {
<div class="click-overlay visible" [class.rtl-overlay]="readingDirection === CbxReadingDirection.RTL"
[class.magnifier-disabled]="isMagnifierActive">
@if (clickToPaginate()) {
<div class="click-overlay visible" [class.rtl-overlay]="readingDirection() === CbxReadingDirection.RTL"
[class.magnifier-disabled]="isMagnifierActive()">
<div class="click-zone click-zone-prev" role="button" tabindex="0" (click)="onClickZonePrev($event)"
(keydown.enter)="onClickZonePrev($event)">
<span class="zone-arrow">&#9664;</span>
File diff suppressed because it is too large Load Diff
@@ -1,6 +1,6 @@
import {TestBed} from '@angular/core/testing';
import {TranslocoService} from '@jsverse/transloco';
import {of, Subject} from 'rxjs';
import {of} from 'rxjs';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {CbxPageInfo, CbxReaderService} from '../../../../book/service/cbx-reader.service';
@@ -12,7 +12,6 @@ import {CbxSidebarService} from './cbx-sidebar.service';
describe('CbxSidebarService', () => {
let service: CbxSidebarService;
let destroy$: Subject<void>;
const pageInfo: CbxPageInfo[] = [
{pageNumber: 1, displayName: 'Page 1'},
@@ -76,7 +75,6 @@ describe('CbxSidebarService', () => {
} as Book);
beforeEach(() => {
destroy$ = new Subject<void>();
cbxReaderService.getPageInfo.mockReset();
cbxReaderService.getPageInfo.mockReturnValue(of(pageInfo));
bookMarkService.getBookmarksForBook.mockReset();
@@ -112,12 +110,11 @@ describe('CbxSidebarService', () => {
});
afterEach(() => {
destroy$.complete();
TestBed.resetTestingModule();
});
it('initializes book info and loads pages, bookmarks, and notes', () => {
service.initialize(7, createBook(), destroy$, 'CBX');
service.initialize(7, createBook(), 'CBX');
expect(cbxReaderService.getPageInfo).toHaveBeenCalledWith(7, 'CBX');
expect(bookMarkService.getBookmarksForBook).toHaveBeenCalledWith(7);
@@ -135,7 +132,7 @@ describe('CbxSidebarService', () => {
});
it('falls back to translated untitled book info when metadata is missing', () => {
service.initialize(7, createBook({metadata: undefined, fileName: undefined}), destroy$);
service.initialize(7, createBook({metadata: undefined, fileName: undefined}));
expect(service.bookInfo()).toEqual({
id: 7,
@@ -161,7 +158,7 @@ describe('CbxSidebarService', () => {
});
it('supports bookmark lookups, creation, deletion, and toggle branches', () => {
service.initialize(7, createBook(), destroy$);
service.initialize(7, createBook());
expect(service.isPageBookmarked(2)).toBe(true);
expect(service.isPageBookmarked(9)).toBe(false);
@@ -192,7 +189,7 @@ describe('CbxSidebarService', () => {
});
it('filters notes by content or chapter title and clears the query', () => {
service.initialize(7, createBook(), destroy$);
service.initialize(7, createBook());
service.setNotesSearchQuery('battle');
expect(service.filteredNotes()).toEqual([notes[0]]);
@@ -209,7 +206,7 @@ describe('CbxSidebarService', () => {
const editEvents: BookNoteV2[] = [];
service.navigateToPage$.subscribe(value => pageEvents.push(value));
service.editNote$.subscribe(value => editEvents.push(value));
service.initialize(7, createBook(), destroy$);
service.initialize(7, createBook());
service.createNote(6, 'Remember this', '#fff000');
expect(bookNoteV2Service.createNote).toHaveBeenCalledWith({
@@ -1,6 +1,6 @@
import {computed, inject, Injectable, signal} from '@angular/core';
import {computed, DestroyRef, inject, Injectable, signal} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {TranslocoService} from '@jsverse/transloco';
import {CbxPageInfo, CbxReaderService} from '../../../../book/service/cbx-reader.service';
import {UrlHelperService} from '../../../../../shared/service/url-helper.service';
@@ -25,7 +25,7 @@ export class CbxSidebarService {
private bookNoteV2Service = inject(BookNoteV2Service);
private readonly t = inject(TranslocoService);
private destroy$ = new Subject<void>();
private readonly destroyRef = inject(DestroyRef);
private bookId!: number;
private altBookType?: string;
@@ -76,10 +76,9 @@ export class CbxSidebarService {
navigateToPage$ = this._navigateToPage.asObservable();
editNote$ = this._editNote.asObservable();
initialize(bookId: number, book: Book, destroy$: Subject<void>, altBookType?: string): void {
initialize(bookId: number, book: Book, altBookType?: string): void {
this.bookId = bookId;
this.altBookType = altBookType;
this.destroy$ = destroy$;
this._bookInfo.set({
id: book.id,
@@ -95,19 +94,19 @@ export class CbxSidebarService {
private loadPageInfo(): void {
this.cbxReaderService.getPageInfo(this.bookId, this.altBookType)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(pages => this._pages.set(pages));
}
private loadBookmarks(): void {
this.bookMarkService.getBookmarksForBook(this.bookId)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(bookmarks => this._bookmarks.set(bookmarks));
}
private loadNotes(): void {
this.bookNoteV2Service.getNotesForBook(this.bookId)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(notes => this._notes.set(notes));
}
@@ -153,13 +152,13 @@ export class CbxSidebarService {
};
this.bookMarkService.createBookmark(request)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.loadBookmarks());
}
deleteBookmark(bookmarkId: number): void {
this.bookMarkService.deleteBookmark(bookmarkId)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.loadBookmarks());
}
@@ -195,7 +194,7 @@ export class CbxSidebarService {
};
this.bookNoteV2Service.createNote(request)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.loadNotes());
}
@@ -206,13 +205,13 @@ export class CbxSidebarService {
};
this.bookNoteV2Service.updateNote(noteId, request)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.loadNotes());
}
deleteNote(noteId: number): void {
this.bookNoteV2Service.deleteNote(noteId)
.pipe(takeUntil(this.destroy$))
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this._notes.update(notes => notes.filter(note => note.id !== noteId));
});