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 {
this.pageTitle.setPageTitle('');
this.coverScalePreferenceService.scaleChange$.pipe(debounceTime(1000)).subscribe();
this.coverScalePreferenceService.scaleChange$.pipe(debounceTime(1000), takeUntil(this.destroy$)).subscribe();
this.loadMobileColumnsPreference();
this.initializeEntityRouting();
this.setupRouteChangeHandlers();
this.setupUserStateSubscription();
this.setupQueryParamSubscription();
this.setupFilterToggleSubscription();
this.setupSearchTermSubscription();
this.setupScrollPositionTracking();
this.setupSelectionSubscription();
@@ -364,7 +365,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
this.entity$ = routeEntityInfo$.pipe(
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 {
this.activatedRoute.paramMap.subscribe(() => {
this.activatedRoute.paramMap.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.searchTerm$.next('');
this.bookTitle = '';
this.bookSelectionService.deselectAll();
@@ -393,8 +394,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
}
private setupUserStateSubscription(): void {
this.userService.userState$.pipe(filter(u => !!u?.user && u.loaded))
.subscribe(userState => {
this.userService.userState$.pipe(
filter(u => !!u?.user && u.loaded),
takeUntil(this.destroy$)
).subscribe(userState => {
this.metadataMenuItems = this.bookMenuService.getMetadataMenuItems(
() => this.autoFetchMetadata(),
() => this.fetchMetadata(),
@@ -414,7 +417,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
this.entityRouteInfo$,
this.activatedRoute.queryParamMap,
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(
queryParamMap,
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');
const filterParams = queryParamMap.get('filter');
@@ -493,11 +491,19 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
}
private setupSearchTermSubscription(): void {
this.searchTerm$.subscribe(term => {
this.searchTerm$.pipe(takeUntil(this.destroy$)).subscribe(term => {
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 {
this.bookSelectionService.selectedBooks$
.pipe(takeUntil(this.destroy$))