From cac86818c5ea0480fe82635f1180bc4b940a2875 Mon Sep 17 00:00:00 2001 From: Alex <25013571+alexhb1@users.noreply.github.com> Date: Sun, 10 May 2026 17:49:47 +0100 Subject: [PATCH] refactor(ui): restore deferred render state service (#1255) --- .../book-browser/book-browser.component.html | 4 +- .../book-browser.component.spec.ts | 22 ++++++ .../book-browser/book-browser.component.ts | 73 +++++++++++++------ .../book-browser/deferred-render-state.ts | 46 ++++++++++++ 4 files changed, 120 insertions(+), 25 deletions(-) create mode 100644 frontend/src/app/features/book/components/book-browser/deferred-render-state.ts diff --git a/frontend/src/app/features/book/components/book-browser/book-browser.component.html b/frontend/src/app/features/book/components/book-browser/book-browser.component.html index 48384710c..c5af3486d 100644 --- a/frontend/src/app/features/book/components/book-browser/book-browser.component.html +++ b/frontend/src/app/features/book/components/book-browser/book-browser.component.html @@ -21,7 +21,7 @@ {{ entityType }}: {{ entity()?.name }}{{ (isFilterActive() || hasSearchTerm()) ? ' ' + t('labels.filteredSuffix') : '' }} }
- @if (!isBooksLoading() && seriesCollapseFilter.seriesCollapsed() && books(); as books) { + @if (!showBooksLoadingPlaceholder() && seriesCollapseFilter.seriesCollapsed() && books(); as books) {{{ t('labels.seriesCollapsedInfo', { count: books.length, itemWord: books.length === 1 ? t('labels.item') : t('labels.items') }) }}
@@ -252,7 +252,7 @@ } @else {
{{ t('labels.noBooks') }}
diff --git a/frontend/src/app/features/book/components/book-browser/book-browser.component.spec.ts b/frontend/src/app/features/book/components/book-browser/book-browser.component.spec.ts
index 18d3e04f5..f5e9cd316 100644
--- a/frontend/src/app/features/book/components/book-browser/book-browser.component.spec.ts
+++ b/frontend/src/app/features/book/components/book-browser/book-browser.component.spec.ts
@@ -388,6 +388,7 @@ describe('BookBrowserComponent', () => {
const {component} = createHarness();
TestBed.flushEffects();
+ vi.runOnlyPendingTimers();
expect(component.books().map(book => book.id)).toEqual([2, 1]);
@@ -395,6 +396,7 @@ describe('BookBrowserComponent', () => {
{label: 'Title', field: 'title', direction: SortDirection.ASCENDING},
]);
TestBed.flushEffects();
+ vi.runOnlyPendingTimers();
expect(component.books().map(book => book.id)).toEqual([1, 2]);
});
@@ -403,6 +405,7 @@ describe('BookBrowserComponent', () => {
const {component, paramMap$, routeSnapshot} = createHarness();
TestBed.flushEffects();
+ vi.runOnlyPendingTimers();
expect(component.books().map(book => book.id)).toEqual([2, 1]);
@@ -410,6 +413,7 @@ describe('BookBrowserComponent', () => {
routeSnapshot.params = {libraryId: '2'};
paramMap$.next(routeSnapshot.paramMap);
TestBed.flushEffects();
+ vi.runOnlyPendingTimers();
expect(component.books().map(book => book.id)).toEqual([3]);
});
@@ -424,6 +428,24 @@ describe('BookBrowserComponent', () => {
expect(component.showTableLoadingPlaceholder()).toBe(false);
});
+ it('keeps rendered books visible when loading starts after the first render', () => {
+ const renderedBooks = Array.from({length: 30}, (_, index) =>
+ makeBook(index + 1, 1, `Book ${index + 1}`, `2024-01-${String(index + 1).padStart(2, '0')}T00:00:00Z`)
+ );
+ const {component, isBooksLoading} = createHarness({books: renderedBooks});
+
+ TestBed.flushEffects();
+ vi.runOnlyPendingTimers();
+
+ expect(component.books()).toHaveLength(30);
+ expect(component.virtualRowCount()).toBe(30);
+
+ isBooksLoading.set(true);
+
+ expect(component.showBooksLoadingPlaceholder()).toBe(false);
+ expect(component.virtualRowCount()).toBe(30);
+ });
+
it('sizes grid loading placeholders to fill the viewport', () => {
const {component} = createHarness({books: [], isBooksLoading: true});
const loadingGrid = component as unknown as {
diff --git a/frontend/src/app/features/book/components/book-browser/book-browser.component.ts b/frontend/src/app/features/book/components/book-browser/book-browser.component.ts
index 28f465904..a2d6dde70 100644
--- a/frontend/src/app/features/book/components/book-browser/book-browser.component.ts
+++ b/frontend/src/app/features/book/components/book-browser/book-browser.component.ts
@@ -1,4 +1,4 @@
-import {AfterViewInit, ChangeDetectionStrategy, Component, DestroyRef, ElementRef, HostListener, computed, effect, inject, signal, viewChild} from '@angular/core';
+import {AfterViewInit, ChangeDetectionStrategy, Component, DestroyRef, ElementRef, HostListener, computed, effect, inject, signal, untracked, viewChild} from '@angular/core';
import {takeUntilDestroyed, toObservable, toSignal} from '@angular/core/rxjs-interop';
import {ActivatedRoute} from '@angular/router';
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
@@ -58,6 +58,7 @@ import {filterBooksBySearchTerm} from './filters/HeaderFilter';
import {filterBooksByFilters} from './filters/sidebar-filter';
import {LayoutService} from '../../../../shared/layout/layout.service';
import {createGridDensity} from '../../../../shared/util/grid-density.util';
+import {DeferredRenderState} from './deferred-render-state';
export enum EntityType {
LIBRARY = 'Library',
@@ -200,29 +201,51 @@ export class BookBrowserComponent implements AfterViewInit {
return actions;
});
- // --- Layered pipeline: each computed only recomputes when its direct inputs change ---
- private readonly entityBooks = computed(() => {
- const {entityId, entityType} = this.entityInfo();
- return this.entityService.getBooksByEntity(this.bookService.books(), entityId, entityType);
- });
- private readonly searchedBooks = computed(() =>
- filterBooksBySearchTerm(this.entityBooks(), this.debouncedSearchTerm())
- );
- private readonly filteredBooks = computed(() =>
- filterBooksByFilters(this.searchedBooks(), this.selectedFilter(), this.selectedFilterMode())
- );
+ // Deferred pipeline: heavy filter/sort runs in a setTimeout so the page chrome
+ // and skeletons paint first, then real books replace them on the next task.
private readonly forceExpandSeries = computed(() =>
this.queryParamsService.shouldForceExpandSeries(this.queryParamMap())
);
- private readonly collapsedBooks = computed(() =>
- this.seriesCollapseFilter.collapseBooks(
- this.filteredBooks(), this.forceExpandSeries(), this.seriesCollapsed()
- )
- );
+ private readonly booksContextKey = computed(() => {
+ const {entityId, entityType} = this.entityInfo();
+ return Number.isNaN(entityId) ? entityType : `${entityType}:${entityId}`;
+ });
+ private lastBooksContextKey: string | null = null;
+ private readonly booksRenderState = new DeferredRenderState