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);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -1,5 +1,5 @@
|
||||
import {Subject} from 'rxjs';
|
||||
import {TestBed, ComponentFixture} from '@angular/core/testing';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
||||
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
@@ -12,7 +12,6 @@ describe('LiveNotificationBoxComponent', () => {
|
||||
let notifications$: Subject<LogNotification>;
|
||||
let langChanges$: Subject<string>;
|
||||
let component: LiveNotificationBoxComponent;
|
||||
let fixture: ComponentFixture<LiveNotificationBoxComponent>;
|
||||
|
||||
beforeEach(() => {
|
||||
notifications$ = new Subject<LogNotification>();
|
||||
@@ -35,7 +34,7 @@ describe('LiveNotificationBoxComponent', () => {
|
||||
],
|
||||
});
|
||||
|
||||
fixture = TestBed.createComponent(LiveNotificationBoxComponent);
|
||||
const fixture = TestBed.createComponent(LiveNotificationBoxComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
+4
-1
@@ -1,4 +1,4 @@
|
||||
import {Component, inject, OnDestroy} from '@angular/core';
|
||||
import {ChangeDetectorRef, Component, inject, OnDestroy} from '@angular/core';
|
||||
import {NotificationEventService} from '../../websocket/notification-event.service';
|
||||
import {LogNotification} from '../../websocket/model/log-notification.model';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
@@ -21,6 +21,7 @@ import {TagComponent} from '../tag/tag.component';
|
||||
})
|
||||
export class LiveNotificationBoxComponent implements OnDestroy {
|
||||
private readonly t = inject(TranslocoService);
|
||||
private readonly cdr = inject(ChangeDetectorRef);
|
||||
private readonly destroy$ = new Subject<void>();
|
||||
latestNotification: LogNotification = {message: this.t.translate('shared.liveNotification.defaultMessage')};
|
||||
private hasReceivedNotification = false;
|
||||
@@ -31,11 +32,13 @@ export class LiveNotificationBoxComponent implements OnDestroy {
|
||||
this.notificationService.latestNotification$.pipe(takeUntil(this.destroy$)).subscribe(notification => {
|
||||
this.hasReceivedNotification = true;
|
||||
this.latestNotification = notification;
|
||||
this.cdr.markForCheck();
|
||||
});
|
||||
this.t.langChanges$.pipe(takeUntil(this.destroy$)).subscribe(() => {
|
||||
if (!this.hasReceivedNotification) {
|
||||
this.latestNotification = {message: this.t.translate('shared.liveNotification.defaultMessage')};
|
||||
}
|
||||
this.cdr.markForCheck();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user