refactor(cover): add async cover notifications, for real time updates (#716)
This commit is contained in:
@@ -67,6 +67,8 @@ export class AppBooksApiService {
|
||||
if (a.length !== b.length) return false;
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (a[i].id !== b[i].id) return false;
|
||||
if (a[i].metadata?.coverUpdatedOn !== b[i].metadata?.coverUpdatedOn) return false;
|
||||
if (a[i].metadata?.audiobookCoverUpdatedOn !== b[i].metadata?.audiobookCoverUpdatedOn) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {InfiniteData, QueryClient} from '@tanstack/angular-query-experimental';
|
||||
|
||||
import {Book, BookMetadata} from '../model/book.model';
|
||||
import {AppBookSummary, AppPageResponse} from '../model/app-book.model';
|
||||
import {BOOKS_QUERY_KEY, bookDetailQueryPrefix, bookRecommendationsQueryPrefix} from './book-query-keys';
|
||||
|
||||
const APP_BOOKS_QUERY_PREFIX = ['app-books'] as const;
|
||||
@@ -98,3 +99,33 @@ export function patchBookFieldsInCache(queryClient: QueryClient, updates: {bookI
|
||||
invalidateBookDetailQueries(queryClient, updates.map(u => u.bookId));
|
||||
invalidateAppBooksQueries(queryClient);
|
||||
}
|
||||
|
||||
export function patchAppBooksCoverInCache(
|
||||
queryClient: QueryClient,
|
||||
patches: {id: number; coverUpdatedOn?: string | null; audiobookCoverUpdatedOn?: string | null}[]
|
||||
): void {
|
||||
if (patches.length === 0) return;
|
||||
const patchMap = new Map(patches.map(p => [p.id, p]));
|
||||
|
||||
queryClient.setQueriesData<InfiniteData<AppPageResponse<AppBookSummary>>>(
|
||||
{queryKey: APP_BOOKS_QUERY_PREFIX},
|
||||
(current) => {
|
||||
if (!current) return current;
|
||||
return {
|
||||
...current,
|
||||
pages: current.pages.map(page => ({
|
||||
...page,
|
||||
content: page.content.map(summary => {
|
||||
const patch = patchMap.get(summary.id);
|
||||
if (!patch) return summary;
|
||||
return {
|
||||
...summary,
|
||||
...('coverUpdatedOn' in patch ? {coverUpdatedOn: patch.coverUpdatedOn ?? null} : {}),
|
||||
...('audiobookCoverUpdatedOn' in patch ? {audiobookCoverUpdatedOn: patch.audiobookCoverUpdatedOn ?? null} : {}),
|
||||
};
|
||||
})
|
||||
}))
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {BOOKS_QUERY_KEY} from './book-query-keys';
|
||||
import {
|
||||
addBookToCache,
|
||||
invalidateAppBooksQueries,
|
||||
invalidateBookDetailQueries,
|
||||
invalidateBooksQuery,
|
||||
patchAppBooksCoverInCache,
|
||||
patchBooksInCache,
|
||||
removeBookQueries,
|
||||
} from './book-query-cache';
|
||||
@@ -50,7 +50,7 @@ export class BookSocketService {
|
||||
: book;
|
||||
})
|
||||
);
|
||||
patchAppBooksCoverInCache(this.queryClient, patches);
|
||||
invalidateBookDetailQueries(this.queryClient, patches.map(p => p.id));
|
||||
invalidateAppBooksQueries(this.queryClient);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user