fix(book-browser): prevent memory leaks by unsubscribing from observables (#80)

This commit is contained in:
Alex Fair
2026-04-22 17:26:29 -04:00
committed by Zach Yale
parent d0ba4aec9c
commit 5f1d34d716
@@ -296,13 +296,14 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
ngOnInit(): void { ngOnInit(): void {
this.pageTitle.setPageTitle(''); this.pageTitle.setPageTitle('');
this.coverScalePreferenceService.scaleChange$.pipe(debounceTime(1000)).subscribe(); this.coverScalePreferenceService.scaleChange$.pipe(debounceTime(1000), takeUntil(this.destroy$)).subscribe();
this.loadMobileColumnsPreference(); this.loadMobileColumnsPreference();
this.initializeEntityRouting(); this.initializeEntityRouting();
this.setupRouteChangeHandlers(); this.setupRouteChangeHandlers();
this.setupUserStateSubscription(); this.setupUserStateSubscription();
this.setupQueryParamSubscription(); this.setupQueryParamSubscription();
this.setupFilterToggleSubscription();
this.setupSearchTermSubscription(); this.setupSearchTermSubscription();
this.setupScrollPositionTracking(); this.setupScrollPositionTracking();
this.setupSelectionSubscription(); this.setupSelectionSubscription();
@@ -364,7 +365,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
this.entity$ = routeEntityInfo$.pipe( this.entity$ = routeEntityInfo$.pipe(
switchMap(({entityId, entityType}) => this.entityService.fetchEntity(entityId, entityType)) switchMap(({entityId, entityType}) => this.entityService.fetchEntity(entityId, entityType))
); );
this.entity$.subscribe(entity => this.handleEntityLoaded(entity)); this.entity$.pipe(takeUntil(this.destroy$)).subscribe(entity => this.handleEntityLoaded(entity));
} }
} }
@@ -384,7 +385,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
} }
private setupRouteChangeHandlers(): void { private setupRouteChangeHandlers(): void {
this.activatedRoute.paramMap.subscribe(() => { this.activatedRoute.paramMap.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.searchTerm$.next(''); this.searchTerm$.next('');
this.bookTitle = ''; this.bookTitle = '';
this.bookSelectionService.deselectAll(); this.bookSelectionService.deselectAll();
@@ -393,8 +394,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
} }
private setupUserStateSubscription(): void { private setupUserStateSubscription(): void {
this.userService.userState$.pipe(filter(u => !!u?.user && u.loaded)) this.userService.userState$.pipe(
.subscribe(userState => { filter(u => !!u?.user && u.loaded),
takeUntil(this.destroy$)
).subscribe(userState => {
this.metadataMenuItems = this.bookMenuService.getMetadataMenuItems( this.metadataMenuItems = this.bookMenuService.getMetadataMenuItems(
() => this.autoFetchMetadata(), () => this.autoFetchMetadata(),
() => this.fetchMetadata(), () => this.fetchMetadata(),
@@ -414,7 +417,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
this.entityRouteInfo$, this.entityRouteInfo$,
this.activatedRoute.queryParamMap, this.activatedRoute.queryParamMap,
this.userService.userState$.pipe(filter(u => !!u?.user && u.loaded)) this.userService.userState$.pipe(filter(u => !!u?.user && u.loaded))
]).subscribe(([entityInfo, queryParamMap, user]) => { ]).pipe(takeUntil(this.destroy$)).subscribe(([entityInfo, queryParamMap, user]) => {
const parseResult = this.queryParamsService.parseQueryParams( const parseResult = this.queryParamsService.parseQueryParams(
queryParamMap, queryParamMap,
user.user?.userSettings?.entityViewPreferences, user.user?.userSettings?.entityViewPreferences,
@@ -432,11 +435,6 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
} }
} }
this.sidebarFilterTogglePrefService.showFilter$.subscribe(value => {
this.showFilter = value;
});
this.currentFilterLabel = this.t.translate('book.browser.labels.allBooks'); this.currentFilterLabel = this.t.translate('book.browser.labels.allBooks');
const filterParams = queryParamMap.get('filter'); const filterParams = queryParamMap.get('filter');
@@ -493,11 +491,19 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
} }
private setupSearchTermSubscription(): void { private setupSearchTermSubscription(): void {
this.searchTerm$.subscribe(term => { this.searchTerm$.pipe(takeUntil(this.destroy$)).subscribe(term => {
this.hasSearchTerm = !!term && term.trim().length > 0; this.hasSearchTerm = !!term && term.trim().length > 0;
}); });
} }
private setupFilterToggleSubscription(): void {
this.sidebarFilterTogglePrefService.showFilter$
.pipe(takeUntil(this.destroy$))
.subscribe(value => {
this.showFilter = value;
});
}
private setupSelectionSubscription(): void { private setupSelectionSubscription(): void {
this.bookSelectionService.selectedBooks$ this.bookSelectionService.selectedBooks$
.pipe(takeUntil(this.destroy$)) .pipe(takeUntil(this.destroy$))