refactor(ebook-reader): optimize destruction handling and improve reactive subscriptions (#478)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, effect, HostListener, inject, OnInit, signal} from '@angular/core';
|
||||
import {from, Observable, of, Subject, throwError} from 'rxjs';
|
||||
import {catchError, map, switchMap, takeUntil, tap} from 'rxjs/operators';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {forkJoin, from, Observable, of, throwError} from 'rxjs';
|
||||
import {catchError, map, switchMap, tap} from 'rxjs/operators';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {ReaderLoaderService} from './core/loader.service';
|
||||
import {ReaderViewManagerService} from './core/view-manager.service';
|
||||
@@ -17,7 +18,7 @@ import {ReaderNoteService} from './features/notes/note.service';
|
||||
import {BookService} from '../../book/service/book.service';
|
||||
import {BookFileService} from '../../book/service/book-file.service';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import {Book, BookType} from '../../book/model/book.model';
|
||||
import {AdditionalFile, Book, BookType} from '../../book/model/book.model';
|
||||
import {ReaderHeaderComponent} from './layout/header/header.component';
|
||||
import {ReaderSidebarComponent} from './layout/sidebar/sidebar.component';
|
||||
import {ReaderLeftSidebarComponent} from './layout/panel/panel.component';
|
||||
@@ -33,6 +34,7 @@ import {EbookShortcutsHelpComponent} from './dialogs/shortcuts-help.component';
|
||||
import {TranslocoPipe} from '@jsverse/transloco';
|
||||
import {RelocateProgressData} from './state/progress.service';
|
||||
import {WakeLockService} from '../../../shared/service/wake-lock.service';
|
||||
import {ViewEvent} from './core/view-manager.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-ebook-reader',
|
||||
@@ -71,8 +73,7 @@ import {WakeLockService} from '../../../shared/service/wake-lock.service';
|
||||
changeDetection: ChangeDetectionStrategy.OnPush
|
||||
})
|
||||
export class EbookReaderComponent implements OnInit {
|
||||
private destroy$ = new Subject<void>();
|
||||
private destroyRef = inject(DestroyRef);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private loaderService = inject(ReaderLoaderService);
|
||||
private styleService = inject(ReaderStyleService);
|
||||
private bookService = inject(BookService);
|
||||
@@ -85,6 +86,7 @@ export class EbookReaderComponent implements OnInit {
|
||||
private headerService = inject(ReaderHeaderService);
|
||||
private noteService = inject(ReaderNoteService);
|
||||
private wakeLockService = inject(WakeLockService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
public sidebarService = inject(ReaderSidebarService);
|
||||
public leftSidebarService = inject(ReaderLeftSidebarService);
|
||||
@@ -121,9 +123,6 @@ export class EbookReaderComponent implements OnInit {
|
||||
|
||||
constructor() {
|
||||
this.destroyRef.onDestroy(() => {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
|
||||
this.wakeLockService.disable();
|
||||
this.viewManager.destroy();
|
||||
this.annotationService.reset();
|
||||
@@ -169,43 +168,107 @@ export class EbookReaderComponent implements OnInit {
|
||||
});
|
||||
|
||||
this.sidebarService.showMetadata$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.showMetadata.set(true));
|
||||
|
||||
this.headerService.showControls$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.showQuickSettings.set(true));
|
||||
|
||||
this.headerService.showMetadata$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.showMetadata.set(true));
|
||||
|
||||
this.headerService.toggleFullscreen$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.toggleFullscreen());
|
||||
|
||||
this.headerService.showShortcutsHelp$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.showShortcutsHelp.set(true));
|
||||
|
||||
// Enable wake lock after a short delay
|
||||
setTimeout(() => this.wakeLockService.enable(), 1000);
|
||||
|
||||
this.isLoading.set(true);
|
||||
this.initializeFoliate().pipe(
|
||||
switchMap(() => this.epubCustomFontService.loadAndCacheFonts()),
|
||||
tap(() => this.stateService.refreshCustomFonts()),
|
||||
switchMap(() => this.setupView()),
|
||||
tap(() => {
|
||||
this.subscribeToViewEvents();
|
||||
|
||||
this.bookId = +this.route.snapshot.paramMap.get('bookId')!;
|
||||
this.altBookType = this.route.snapshot.queryParamMap.get('bookType') ?? undefined;
|
||||
|
||||
// Parallelize Foliate script loading and initial book detail fetch
|
||||
forkJoin([
|
||||
this.initializeFoliate(),
|
||||
from(this.bookService.fetchFreshBookDetail(this.bookId, false))
|
||||
]).pipe(
|
||||
switchMap(([, book]) => {
|
||||
this.book.set(book);
|
||||
const bookType = (this.altBookType as BookType | undefined) ?? book.primaryFile?.bookType;
|
||||
if (!bookType) {
|
||||
return throwError(() => new Error('Book type not found'));
|
||||
}
|
||||
|
||||
let bookFileId: number | undefined;
|
||||
if (this.altBookType) {
|
||||
const altFile = book.alternativeFormats?.find((f: AdditionalFile) => f.bookType === this.altBookType);
|
||||
bookFileId = altFile?.id;
|
||||
} else {
|
||||
bookFileId = book.primaryFile?.id;
|
||||
}
|
||||
|
||||
// Parallelize font loading/view setup and state initialization
|
||||
return forkJoin([
|
||||
this.epubCustomFontService.loadAndCacheFonts().pipe(
|
||||
tap(() => this.stateService.refreshCustomFonts())
|
||||
),
|
||||
this.setupView().pipe(
|
||||
tap(() => this.subscribeToViewEvents())
|
||||
),
|
||||
this.stateService.initializeState(this.bookId, bookFileId!).pipe(
|
||||
map(() => ({book, bookType, bookFileId}))
|
||||
)
|
||||
]);
|
||||
}),
|
||||
switchMap(([, , {book, bookType, bookFileId}]) => {
|
||||
this.progressService.initialize(this.bookId, bookType, bookFileId);
|
||||
this.selectionService.initialize(this.bookId);
|
||||
this.headerService.initialize(this.bookId, book.metadata?.title || '');
|
||||
|
||||
const useStreaming = this.route.snapshot.queryParamMap.get('streaming') === 'true';
|
||||
const loadBook$ = bookType === 'EPUB' && useStreaming
|
||||
? this.viewManager.loadEpubStreaming(this.bookId, this.altBookType)
|
||||
: this.loadBookBlob();
|
||||
|
||||
return loadBook$.pipe(
|
||||
tap(() => {
|
||||
this.applyStyles();
|
||||
this.sidebarService.initialize(this.bookId, book);
|
||||
this.leftSidebarService.initialize(this.bookId);
|
||||
this.noteService.initialize(this.bookId);
|
||||
}),
|
||||
switchMap(() => this.viewManager.getMetadata()),
|
||||
switchMap(() => {
|
||||
if (!this.hasLoadedOnce) {
|
||||
this.hasLoadedOnce = true;
|
||||
if (book.epubProgress?.cfi) {
|
||||
return this.viewManager.goTo(book.epubProgress.cfi);
|
||||
} 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);
|
||||
})
|
||||
);
|
||||
}),
|
||||
switchMap(() => this.loadBookFromAPI()),
|
||||
tap(() => this.isLoading.set(false)),
|
||||
catchError(() => {
|
||||
catchError((err) => {
|
||||
console.error('Failed to load ebook', err);
|
||||
this.isLoading.set(false);
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to load book'});
|
||||
return of(null);
|
||||
}),
|
||||
takeUntil(this.destroy$)
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
@@ -225,73 +288,6 @@ export class EbookReaderComponent implements OnInit {
|
||||
return of(undefined);
|
||||
}
|
||||
|
||||
private loadBookFromAPI(): Observable<void> {
|
||||
this.bookId = +this.route.snapshot.paramMap.get('bookId')!;
|
||||
this.altBookType = this.route.snapshot.queryParamMap.get('bookType') ?? undefined;
|
||||
|
||||
return from(this.bookService.fetchFreshBookDetail(this.bookId, false)).pipe(
|
||||
switchMap((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;
|
||||
if (!bookType) {
|
||||
return throwError(() => new Error('Book type not found'));
|
||||
}
|
||||
|
||||
// Determine which file ID to use for progress tracking
|
||||
let bookFileId: number | undefined;
|
||||
if (this.altBookType) {
|
||||
// Look for the alternative format file with matching type
|
||||
const altFile = book.alternativeFormats?.find(f => f.bookType === this.altBookType);
|
||||
bookFileId = altFile?.id;
|
||||
} else {
|
||||
// Use the primary file
|
||||
bookFileId = book.primaryFile?.id;
|
||||
}
|
||||
|
||||
return this.stateService.initializeState(this.bookId, bookFileId!).pipe(
|
||||
map(() => ({book, bookType, bookFileId}))
|
||||
);
|
||||
}),
|
||||
switchMap(({book, bookType, bookFileId}) => {
|
||||
this.progressService.initialize(this.bookId, bookType, bookFileId);
|
||||
this.selectionService.initialize(this.bookId, this.destroy$);
|
||||
this.headerService.initialize(this.bookId, book.metadata?.title || '');
|
||||
|
||||
// Use streaming for EPUB if query param is set, blob loading otherwise (default)
|
||||
const useStreaming = this.route.snapshot.queryParamMap.get('streaming') === 'true';
|
||||
const loadBook$ = bookType === 'EPUB' && useStreaming
|
||||
? this.viewManager.loadEpubStreaming(this.bookId, this.altBookType)
|
||||
: this.loadBookBlob();
|
||||
|
||||
return loadBook$.pipe(
|
||||
tap(() => {
|
||||
this.applyStyles();
|
||||
this.sidebarService.initialize(this.bookId, book, this.destroy$);
|
||||
this.leftSidebarService.initialize(this.bookId, this.destroy$);
|
||||
this.noteService.initialize(this.bookId, this.destroy$);
|
||||
}),
|
||||
switchMap(() => this.viewManager.getMetadata()),
|
||||
switchMap(() => {
|
||||
if (!this.hasLoadedOnce) {
|
||||
this.hasLoadedOnce = true;
|
||||
// Navigate to saved position if progress exists, otherwise go to first page
|
||||
if (book.epubProgress?.cfi) {
|
||||
return this.viewManager.goTo(book.epubProgress.cfi);
|
||||
} 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);
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private loadBookBlob(): Observable<void> {
|
||||
return this.bookFileService.getFileContent(this.bookId, this.altBookType).pipe(
|
||||
switchMap(fileBlob => {
|
||||
@@ -304,8 +300,8 @@ export class EbookReaderComponent implements OnInit {
|
||||
|
||||
private subscribeToViewEvents(): void {
|
||||
this.viewManager.events$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(event => {
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe((event: ViewEvent) => {
|
||||
switch (event.type) {
|
||||
case 'load':
|
||||
this.applyStyles();
|
||||
@@ -430,7 +426,7 @@ export class EbookReaderComponent implements OnInit {
|
||||
onProgressChange(fraction: number): void {
|
||||
this.viewManager.goToFraction(fraction)
|
||||
.pipe(
|
||||
takeUntil(this.destroy$),
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
catchError(() => of(undefined))
|
||||
)
|
||||
.subscribe();
|
||||
@@ -486,7 +482,7 @@ export class EbookReaderComponent implements OnInit {
|
||||
if (linkUrl) {
|
||||
this.viewManager.goTo(linkUrl)
|
||||
.pipe(
|
||||
takeUntil(this.destroy$),
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
catchError(() => of(undefined))
|
||||
)
|
||||
.subscribe();
|
||||
|
||||
@@ -53,10 +53,8 @@ describe('ReaderNoteService', () => {
|
||||
};
|
||||
|
||||
let service: ReaderNoteService;
|
||||
let destroy$: Subject<void>;
|
||||
|
||||
beforeEach(() => {
|
||||
destroy$ = new Subject<void>();
|
||||
bookNoteV2Service.createNote.mockReset();
|
||||
bookNoteV2Service.updateNote.mockReset();
|
||||
messageService.add.mockReset();
|
||||
@@ -80,12 +78,10 @@ describe('ReaderNoteService', () => {
|
||||
});
|
||||
|
||||
service = TestBed.inject(ReaderNoteService);
|
||||
service.initialize(14, destroy$);
|
||||
service.initialize(14);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
destroy$.next();
|
||||
destroy$.complete();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {inject, Injectable, signal} from '@angular/core';
|
||||
import {Subject} from 'rxjs';
|
||||
import {takeUntil} from 'rxjs/operators';
|
||||
import {DestroyRef, inject, Injectable, signal} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {BookNoteV2Service, CreateBookNoteV2Request, UpdateBookNoteV2Request} from '../../../../../shared/service/book-note-v2.service';
|
||||
@@ -25,8 +24,8 @@ export class ReaderNoteService {
|
||||
private leftSidebarService = inject(ReaderLeftSidebarService);
|
||||
private viewManager = inject(ReaderViewManagerService);
|
||||
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private bookId!: number;
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
private readonly _dialogState = signal<NoteDialogState>({
|
||||
visible: false,
|
||||
@@ -34,12 +33,11 @@ export class ReaderNoteService {
|
||||
});
|
||||
readonly dialogState = this._dialogState.asReadonly();
|
||||
|
||||
initialize(bookId: number, destroy$: Subject<void>): void {
|
||||
initialize(bookId: number): void {
|
||||
this.bookId = bookId;
|
||||
this.destroy$ = destroy$;
|
||||
|
||||
this.leftSidebarService.editNote$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(note => {
|
||||
this.openEditDialog({
|
||||
cfi: note.cfi,
|
||||
@@ -96,7 +94,7 @@ export class ReaderNoteService {
|
||||
};
|
||||
|
||||
this.bookNoteV2Service.createNote(createRequest)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.closeDialog();
|
||||
@@ -125,7 +123,7 @@ export class ReaderNoteService {
|
||||
};
|
||||
|
||||
this.bookNoteV2Service.updateNote(noteId, updateRequest)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.closeDialog();
|
||||
|
||||
+2
-3
@@ -1,5 +1,5 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {of, Subject} from 'rxjs';
|
||||
import {of} from 'rxjs';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
import {Annotation} from '../../../../../shared/service/annotation.service';
|
||||
@@ -9,7 +9,6 @@ import {ReaderViewManagerService} from '../../core/view-manager.service';
|
||||
import {ReaderSelectionService} from './selection.service';
|
||||
|
||||
describe('ReaderSelectionService', () => {
|
||||
const destroy$ = new Subject<void>();
|
||||
|
||||
const viewManager = {
|
||||
addAnnotation: vi.fn(() => of({index: 1, label: 'Annotation 1'})),
|
||||
@@ -49,7 +48,7 @@ describe('ReaderSelectionService', () => {
|
||||
});
|
||||
|
||||
service = TestBed.inject(ReaderSelectionService);
|
||||
service.initialize(41, destroy$);
|
||||
service.initialize(41);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
+11
-11
@@ -1,6 +1,7 @@
|
||||
import {inject, Injectable, signal} from '@angular/core';
|
||||
import {DestroyRef, inject, Injectable, signal} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {Subject} from 'rxjs';
|
||||
import {switchMap, takeUntil, tap} from 'rxjs/operators';
|
||||
import {switchMap, tap} from 'rxjs/operators';
|
||||
import {of} from 'rxjs';
|
||||
import {Annotation} from '../../../../../shared/service/annotation.service';
|
||||
import {ReaderViewManagerService} from '../../core/view-manager.service';
|
||||
@@ -31,10 +32,10 @@ export class ReaderSelectionService {
|
||||
private annotationService = inject(ReaderAnnotationHttpService);
|
||||
private leftSidebarService = inject(ReaderLeftSidebarService);
|
||||
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private bookId!: number;
|
||||
private annotations: Annotation[] = [];
|
||||
private currentSelection: SelectionDetail | null = null;
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
private _visible = false;
|
||||
private _position = { x: 0, y: 0 };
|
||||
@@ -60,9 +61,8 @@ export class ReaderSelectionService {
|
||||
private annotationsChangedSubject = new Subject<Annotation[]>();
|
||||
public annotationsChanged$ = this.annotationsChangedSubject.asObservable();
|
||||
|
||||
initialize(bookId: number, destroy$: Subject<void>): void {
|
||||
initialize(bookId: number): void {
|
||||
this.bookId = bookId;
|
||||
this.destroy$ = destroy$;
|
||||
}
|
||||
|
||||
setAnnotations(annotations: Annotation[]): void {
|
||||
@@ -137,7 +137,7 @@ export class ReaderSelectionService {
|
||||
private updatePreview(cfi: string, color: string, style: AnnotationStyle): void {
|
||||
if (this.previewCfi) {
|
||||
this.viewManager.deleteAnnotation(this.previewCfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
@@ -149,13 +149,13 @@ export class ReaderSelectionService {
|
||||
value: cfi,
|
||||
color: color,
|
||||
style: style
|
||||
}).pipe(takeUntil(this.destroy$)).subscribe();
|
||||
}).pipe(takeUntilDestroyed(this.destroyRef)).subscribe();
|
||||
}
|
||||
|
||||
private clearPreview(): void {
|
||||
if (this.previewCfi) {
|
||||
this.viewManager.deleteAnnotation(this.previewCfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe();
|
||||
this.previewCfi = null;
|
||||
this.previewColor = null;
|
||||
@@ -185,7 +185,7 @@ export class ReaderSelectionService {
|
||||
tap(() => this.viewManager.clearSelection())
|
||||
);
|
||||
}),
|
||||
takeUntil(this.destroy$)
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
@@ -194,11 +194,11 @@ export class ReaderSelectionService {
|
||||
if (!annotation) return;
|
||||
|
||||
this.annotationService.deleteAnnotation(annotationId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(success => {
|
||||
if (success) {
|
||||
this.viewManager.deleteAnnotation(annotation.cfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe();
|
||||
this.annotations = this.annotations.filter(a => a.id !== annotationId);
|
||||
this.annotationsChangedSubject.next(this.annotations);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {of, Subject} from 'rxjs';
|
||||
import {of} from 'rxjs';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
import {BookNoteV2, BookNoteV2Service} from '../../../../../shared/service/book-note-v2.service';
|
||||
@@ -8,7 +8,6 @@ import {ReaderLeftSidebarService} from './panel.service';
|
||||
|
||||
describe('ReaderLeftSidebarService', () => {
|
||||
let service: ReaderLeftSidebarService;
|
||||
let destroy$: Subject<void>;
|
||||
|
||||
const notes = [
|
||||
{id: 1, bookId: 9, cfi: 'epubcfi(/6/2)', noteContent: 'Alpha', selectedText: 'Quote', chapterTitle: 'Intro', createdAt: '2026-03-26T00:00:00Z'},
|
||||
@@ -41,7 +40,6 @@ describe('ReaderLeftSidebarService', () => {
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
destroy$ = new Subject<void>();
|
||||
bookNoteV2Service.getNotesForBook.mockReset();
|
||||
bookNoteV2Service.getNotesForBook.mockReturnValue(of(notes));
|
||||
bookNoteV2Service.deleteNote.mockReset();
|
||||
@@ -61,13 +59,11 @@ describe('ReaderLeftSidebarService', () => {
|
||||
});
|
||||
|
||||
service = TestBed.inject(ReaderLeftSidebarService);
|
||||
service.initialize(9, destroy$);
|
||||
service.initialize(9);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
destroy$.next();
|
||||
destroy$.complete();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
|
||||
@@ -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 {BookNoteV2, BookNoteV2Service} from '../../../../../shared/service/book-note-v2.service';
|
||||
import {ReaderViewManagerService} from '../../core/view-manager.service';
|
||||
import {SearchResult, SearchState} from '../sidebar/sidebar.service';
|
||||
@@ -17,7 +17,7 @@ export class ReaderLeftSidebarService {
|
||||
private viewManager = inject(ReaderViewManagerService);
|
||||
private bookNoteV2Service = inject(BookNoteV2Service);
|
||||
|
||||
private destroy$ = new Subject<void>();
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private bookId!: number;
|
||||
|
||||
private readonly _isOpen = signal(false);
|
||||
@@ -58,16 +58,15 @@ export class ReaderLeftSidebarService {
|
||||
private _editNote = new Subject<BookNoteV2>();
|
||||
editNote$ = this._editNote.asObservable();
|
||||
|
||||
initialize(bookId: number, destroy$: Subject<void>): void {
|
||||
initialize(bookId: number): void {
|
||||
this.bookId = bookId;
|
||||
this.destroy$ = destroy$;
|
||||
|
||||
this.loadNotes();
|
||||
}
|
||||
|
||||
private loadNotes(): void {
|
||||
this.bookNoteV2Service.getNotesForBook(this.bookId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(notes => this._notes.set(notes));
|
||||
}
|
||||
|
||||
@@ -106,13 +105,13 @@ export class ReaderLeftSidebarService {
|
||||
|
||||
navigateToNote(cfi: string): void {
|
||||
this.viewManager.goTo(cfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.close());
|
||||
}
|
||||
|
||||
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));
|
||||
});
|
||||
@@ -204,7 +203,7 @@ export class ReaderLeftSidebarService {
|
||||
|
||||
navigateToSearchResult(cfi: string): void {
|
||||
this.viewManager.goTo(cfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.close());
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -14,7 +14,6 @@ import {Book} from '../../../../book/model/book.model';
|
||||
import {ReaderSidebarService} from './sidebar.service';
|
||||
|
||||
describe('ReaderSidebarService', () => {
|
||||
const destroy$ = new Subject<void>();
|
||||
|
||||
const selectionAnnotations$ = new Subject<{id: number; cfi: string}[]>();
|
||||
const selectionService = {
|
||||
@@ -133,7 +132,7 @@ describe('ReaderSidebarService', () => {
|
||||
});
|
||||
|
||||
service = TestBed.inject(ReaderSidebarService);
|
||||
service.initialize(7, book, destroy$);
|
||||
service.initialize(7, book);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {inject, Injectable, Injector, signal} from '@angular/core';
|
||||
import {DestroyRef, inject, Injectable, Injector, signal} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {Subject} from 'rxjs';
|
||||
import {takeUntil} from 'rxjs/operators';
|
||||
import {TocItem} from 'epubjs';
|
||||
import {BookMark, BookMarkService} from '../../../../../shared/service/book-mark.service';
|
||||
import {Annotation} from '../../../../../shared/service/annotation.service';
|
||||
@@ -48,7 +48,7 @@ export class ReaderSidebarService {
|
||||
private bookMarkHttpService = inject(BookMarkService);
|
||||
private annotationService = inject(ReaderAnnotationHttpService);
|
||||
|
||||
private destroy$ = new Subject<void>();
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private bookId!: number;
|
||||
private selectionService: ReaderSelectionService | null = null;
|
||||
|
||||
@@ -84,9 +84,8 @@ export class ReaderSidebarService {
|
||||
return this.progressService.currentChapterHref;
|
||||
}
|
||||
|
||||
initialize(bookId: number, book: Book, destroy$: Subject<void>): void {
|
||||
initialize(bookId: number, book: Book): void {
|
||||
this.bookId = bookId;
|
||||
this.destroy$ = destroy$;
|
||||
|
||||
this._bookInfo.set({
|
||||
id: book.id,
|
||||
@@ -110,7 +109,7 @@ export class ReaderSidebarService {
|
||||
}
|
||||
|
||||
this.selectionService.annotationsChanged$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(annotations => {
|
||||
this._annotations.set(annotations);
|
||||
});
|
||||
@@ -118,7 +117,7 @@ export class ReaderSidebarService {
|
||||
|
||||
private loadAnnotations(): void {
|
||||
this.annotationService.getAnnotations(this.bookId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(annotations => {
|
||||
this._annotations.set(annotations);
|
||||
if (!this.selectionService) {
|
||||
@@ -166,7 +165,7 @@ export class ReaderSidebarService {
|
||||
|
||||
navigateToChapter(href: string): void {
|
||||
this.viewManager.goTo(href)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.close());
|
||||
}
|
||||
|
||||
@@ -223,19 +222,19 @@ export class ReaderSidebarService {
|
||||
|
||||
private loadBookmarks(): void {
|
||||
this.bookMarkHttpService.getBookmarksForBook(this.bookId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(bookmarks => this._bookmarks.set(bookmarks));
|
||||
}
|
||||
|
||||
navigateToBookmark(cfi: string): void {
|
||||
this.viewManager.goTo(cfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.close());
|
||||
}
|
||||
|
||||
createBookmark(): void {
|
||||
this.bookmarkService.createBookmarkAtCurrentPosition(this.bookId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(success => {
|
||||
if (success) {
|
||||
this.loadBookmarks();
|
||||
@@ -257,13 +256,13 @@ export class ReaderSidebarService {
|
||||
|
||||
deleteBookmark(bookmarkId: number): void {
|
||||
this.bookMarkHttpService.deleteBookmark(bookmarkId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.loadBookmarks());
|
||||
}
|
||||
|
||||
navigateToAnnotation(cfi: string): void {
|
||||
this.viewManager.goTo(cfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(() => this.close());
|
||||
}
|
||||
|
||||
@@ -273,11 +272,11 @@ export class ReaderSidebarService {
|
||||
if (!annotation) return;
|
||||
|
||||
this.annotationService.deleteAnnotation(annotationId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(success => {
|
||||
if (success) {
|
||||
this.viewManager.deleteAnnotation(annotation.cfi)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe();
|
||||
const updatedAnnotations = annotations.filter(a => a.id !== annotationId);
|
||||
this._annotations.set(updatedAnnotations);
|
||||
|
||||
Reference in New Issue
Block a user