perf: optimize book list API with ETag, delta sync, and IndexedDB caching (#2753)

This commit is contained in:
ACX
2026-02-14 17:47:08 -07:00
committed by GitHub
parent 44002c2c3c
commit 6f77223686
13 changed files with 1143 additions and 1220 deletions
@@ -0,0 +1,76 @@
import {Injectable} from '@angular/core';
import {openDB, IDBPDatabase} from 'idb';
import {Book} from '../model/book.model';
const DB_NAME = 'booklore-books';
const DB_VERSION = 1;
const BOOKS_STORE = 'books';
const META_STORE = 'meta';
@Injectable({
providedIn: 'root',
})
export class BookCacheService {
private dbPromise: Promise<IDBPDatabase> = openDB(DB_NAME, DB_VERSION, {
upgrade(db) {
if (!db.objectStoreNames.contains(BOOKS_STORE)) {
db.createObjectStore(BOOKS_STORE, {keyPath: 'id'});
}
if (!db.objectStoreNames.contains(META_STORE)) {
db.createObjectStore(META_STORE);
}
},
});
async getAll(): Promise<Book[]> {
const db = await this.dbPromise;
return db.getAll(BOOKS_STORE);
}
async putAll(books: Book[]): Promise<void> {
const db = await this.dbPromise;
const tx = db.transaction(BOOKS_STORE, 'readwrite');
for (const book of books) {
tx.store.put(book);
}
await tx.done;
}
async put(book: Book): Promise<void> {
const db = await this.dbPromise;
await db.put(BOOKS_STORE, book);
}
async delete(id: number): Promise<void> {
const db = await this.dbPromise;
await db.delete(BOOKS_STORE, id);
}
async deleteMany(ids: number[]): Promise<void> {
const db = await this.dbPromise;
const tx = db.transaction(BOOKS_STORE, 'readwrite');
for (const id of ids) {
tx.store.delete(id);
}
await tx.done;
}
async getSyncTimestamp(): Promise<string | null> {
const db = await this.dbPromise;
return (await db.get(META_STORE, 'syncTimestamp')) ?? null;
}
async setSyncTimestamp(ts: string): Promise<void> {
const db = await this.dbPromise;
await db.put(META_STORE, ts, 'syncTimestamp');
}
async clear(): Promise<void> {
const db = await this.dbPromise;
const tx = db.transaction([BOOKS_STORE, META_STORE], 'readwrite');
tx.objectStore(BOOKS_STORE).clear();
tx.objectStore(META_STORE).clear();
await tx.done;
}
}
@@ -1,5 +1,6 @@
import {inject, Injectable} from '@angular/core';
import {BookStateService} from './book-state.service';
import {BookCacheService} from './book-cache.service';
import {Book, BookMetadata} from '../model/book.model';
@Injectable({
@@ -7,6 +8,7 @@ import {Book, BookMetadata} from '../model/book.model';
})
export class BookSocketService {
private bookStateService = inject(BookStateService);
private bookCacheService = inject(BookCacheService);
handleNewlyCreatedBook(book: Book): void {
const currentState = this.bookStateService.getCurrentBookState();
@@ -18,12 +20,14 @@ export class BookSocketService {
updatedBooks.push(book);
}
this.bookStateService.updateBookState({...currentState, books: updatedBooks});
this.bookCacheService.put(book);
}
handleRemovedBookIds(removedBookIds: number[]): void {
const currentState = this.bookStateService.getCurrentBookState();
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.includes(book.id));
this.bookStateService.updateBookState({...currentState, books: filteredBooks});
this.bookCacheService.deleteMany(removedBookIds);
}
handleBookUpdate(updatedBook: Book): void {
@@ -32,6 +36,7 @@ export class BookSocketService {
book.id === updatedBook.id ? updatedBook : book
);
this.bookStateService.updateBookState({...currentState, books: updatedBooks});
this.bookCacheService.put(updatedBook);
}
handleMultipleBookUpdates(updatedBooks: Book[]): void {
@@ -45,6 +50,7 @@ export class BookSocketService {
);
this.bookStateService.updateBookState({...currentState, books: mergedBooks});
this.bookCacheService.putAll(updatedBooks);
}
handleBookMetadataUpdate(bookId: number, updatedMetadata: BookMetadata): void {
@@ -53,19 +59,30 @@ export class BookSocketService {
return book.id === bookId ? {...book, metadata: updatedMetadata} : book;
});
this.bookStateService.updateBookState({...currentState, books: updatedBooks});
// Mirror metadata update to cache
const updatedBook = updatedBooks.find(b => b.id === bookId);
if (updatedBook) {
this.bookCacheService.put(updatedBook);
}
}
handleMultipleBookCoverPatches(patches: { id: number; coverUpdatedOn: string }[]): void {
if (!patches || patches.length === 0) return;
const currentState = this.bookStateService.getCurrentBookState();
const books = currentState.books || [];
const patchedBooks: Book[] = [];
patches.forEach(p => {
const index = books.findIndex(b => b.id === p.id);
if (index !== -1 && books[index].metadata) {
books[index].metadata.coverUpdatedOn = p.coverUpdatedOn;
patchedBooks.push(books[index]);
}
});
this.bookStateService.updateBookState({...currentState, books});
if (patchedBooks.length > 0) {
this.bookCacheService.putAll(patchedBooks);
}
}
}
@@ -1,8 +1,8 @@
import {inject, Injectable} from '@angular/core';
import {first, Observable, of, throwError} from 'rxjs';
import {first, from, Observable, of, throwError} from 'rxjs';
import {HttpClient, HttpParams} from '@angular/common/http';
import {catchError, distinctUntilChanged, filter, finalize, map, shareReplay, tap} from 'rxjs/operators';
import {AdditionalFile, AdditionalFileType, Book, BookDeletionResponse, BookMetadata, BookRecommendation, BookSetting, BookType, BulkMetadataUpdateRequest, CreatePhysicalBookRequest, MetadataUpdateWrapper, ReadStatus} from '../model/book.model';
import {catchError, distinctUntilChanged, filter, finalize, map, shareReplay, switchMap, tap} from 'rxjs/operators';
import {AdditionalFile, AdditionalFileType, Book, BookDeletionResponse, BookMetadata, BookRecommendation, BookSetting, BookSyncResponse, BookType, BulkMetadataUpdateRequest, CreatePhysicalBookRequest, MetadataUpdateWrapper, ReadStatus} from '../model/book.model';
import {BookState} from '../model/state/book-state.model';
import {API_CONFIG} from '../../../core/config/api-config';
import {MessageService} from 'primeng/api';
@@ -13,6 +13,7 @@ import {Router} from '@angular/router';
import {BookStateService} from './book-state.service';
import {BookSocketService} from './book-socket.service';
import {BookPatchService} from './book-patch.service';
import {BookCacheService} from './book-cache.service';
import {TranslocoService} from '@jsverse/transloco';
export interface BookStatusUpdateResponse {
@@ -42,6 +43,7 @@ export class BookService {
private bookStateService = inject(BookStateService);
private bookSocketService = inject(BookSocketService);
private bookPatchService = inject(BookPatchService);
private bookCacheService = inject(BookCacheService);
private readonly t = inject(TranslocoService);
private loading$: Observable<Book[]> | null = null;
@@ -53,6 +55,7 @@ export class BookService {
if (token === null) {
this.bookStateService.resetBookState();
this.loading$ = null;
this.bookCacheService.clear();
} else {
const current = this.bookStateService.getCurrentBookState();
if (current.loaded && !current.books) {
@@ -72,7 +75,7 @@ export class BookService {
bookState$ = this.bookStateService.bookState$.pipe(
tap(state => {
if (!state.loaded && !state.error && !this.loading$) {
this.loading$ = this.fetchBooks().pipe(
this.loading$ = this.fetchBooksWithCache().pipe(
shareReplay(1),
finalize(() => (this.loading$ = null))
);
@@ -85,14 +88,57 @@ export class BookService {
return this.bookStateService.getCurrentBookState();
}
private fetchBooks(): Observable<Book[]> {
return this.http.get<Book[]>(this.url).pipe(
tap(books => {
private fetchBooksWithCache(): Observable<Book[]> {
return from(this.bookCacheService.getAll()).pipe(
switchMap(cachedBooks => from(this.bookCacheService.getSyncTimestamp()).pipe(
map(syncTs => ({cachedBooks, syncTs}))
)),
switchMap(({cachedBooks, syncTs}) => {
if (cachedBooks.length > 0 && syncTs) {
// Emit cached books immediately for instant render
this.bookStateService.updateBookState({
books: cachedBooks,
loaded: true,
error: null,
});
// Background delta sync
this.deltaSync(syncTs);
return of(cachedBooks);
}
// No cache — full fetch
return this.fetchBooksFullAndCache();
}),
catchError(error => {
// IndexedDB failed — fall back to full fetch
return this.fetchBooksFullAndCache().pipe(
catchError(fetchError => {
const curr = this.bookStateService.getCurrentBookState();
this.bookStateService.updateBookState({
books: curr.books,
loaded: true,
error: fetchError.message,
});
throw fetchError;
})
);
})
);
}
private fetchBooksFullAndCache(): Observable<Book[]> {
return this.http.get<Book[]>(this.url, {observe: 'response'}).pipe(
map(response => {
const bookList = response.body || [];
this.bookStateService.updateBookState({
books: books || [],
books: bookList,
loaded: true,
error: null,
});
const serverDate = response.headers.get('Date');
const syncTs = serverDate ? new Date(serverDate).toISOString() : new Date().toISOString();
this.writeBooksToCache(bookList, syncTs);
return bookList;
}),
catchError(error => {
const curr = this.bookStateService.getCurrentBookState();
@@ -106,14 +152,77 @@ export class BookService {
);
}
refreshBooks(): void {
this.http.get<Book[]>(this.url).pipe(
tap(books => {
private deltaSync(syncTs: string): void {
const params = new HttpParams().set('since', syncTs);
this.http.get<BookSyncResponse>(`${this.url}/delta`, {params}).pipe(
tap(delta => {
const currentState = this.bookStateService.getCurrentBookState();
let books = [...(currentState.books || [])];
// Remove deleted books
if (delta.deletedIds?.length) {
const deletedSet = new Set(delta.deletedIds);
books = books.filter(b => !deletedSet.has(b.id));
this.bookCacheService.deleteMany(delta.deletedIds);
}
// Upsert changed/new books
if (delta.books?.length) {
const updatedMap = new Map(delta.books.map(b => [b.id, b]));
books = books.map(b => updatedMap.get(b.id) ?? b);
// Add new books that weren't in the existing list
const existingIds = new Set(books.map(b => b.id));
for (const book of delta.books) {
if (!existingIds.has(book.id)) {
books.push(book);
}
}
this.bookCacheService.putAll(delta.books);
}
// If total count doesn't match, do a full refresh
if (delta.totalBookCount !== books.length) {
this.refreshBooks();
return;
}
this.bookStateService.updateBookState({
books: books || [],
books,
loaded: true,
error: null,
});
if (delta.syncTimestamp) {
this.bookCacheService.setSyncTimestamp(delta.syncTimestamp);
}
}),
catchError(() => {
// Delta sync failed — fall back to full refresh
this.refreshBooks();
return of(null);
})
).subscribe();
}
private writeBooksToCache(books: Book[], syncTimestamp: string): void {
this.bookCacheService.clear().then(() => {
this.bookCacheService.putAll(books);
this.bookCacheService.setSyncTimestamp(syncTimestamp);
});
}
refreshBooks(): void {
this.http.get<Book[]>(this.url, {observe: 'response'}).pipe(
tap(response => {
const bookList = response.body || [];
this.bookStateService.updateBookState({
books: bookList,
loaded: true,
error: null,
});
const serverDate = response.headers.get('Date');
const syncTs = serverDate ? new Date(serverDate).toISOString() : new Date().toISOString();
this.writeBooksToCache(bookList, syncTs);
}),
catchError(error => {
this.bookStateService.updateBookState({