fix(reader): add fetchFreshBookDetail method to ensure up-to-date reading position across devices (#358)

* fix(reader): add fetchFreshBookDetail method to ensure up-to-date reading position across devices

* test(audiobook): enhance tests to verify queryClient interactions for progress updates

* fix(audiobook): include audiobookProgress and fileProgress in progress payload
This commit is contained in:
brios
2026-04-22 17:27:06 -04:00
committed by Zach Yale
parent cf7999044f
commit 4527d43e84
9 changed files with 76 additions and 23 deletions
@@ -56,6 +56,9 @@ describe('BookPatchService', () => {
},
});
request.flush(null);
expect(queryClient.setQueryData).toHaveBeenCalledWith(BOOKS_QUERY_KEY, expect.any(Function));
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books', 'detail', 11]});
});
it('deduplicates identical EPUB progress updates before posting', () => {
@@ -80,6 +83,8 @@ describe('BookPatchService', () => {
request.flush(null);
httpTestingController.expectNone(req => req.url.endsWith('/api/v1/books/progress'));
expect(queryClient.setQueryData).toHaveBeenCalledWith(BOOKS_QUERY_KEY, expect.any(Function));
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books', 'detail', 7]});
});
it('patches cached progress fields when resetting kobo progress', () => {
@@ -79,7 +79,14 @@ export class BookPatchService {
progressPercent: payload.percentage
};
}
return this.http.post<void>(`${this.url}/progress`, body);
return this.http.post<void>(`${this.url}/progress`, body).pipe(
tap(() => {
patchBookFieldsInCache(this.queryClient, [{
bookId: payload.bookId,
fields: {epubProgress: {cfi: payload.cfi, percentage: payload.percentage}}
}]);
})
);
}),
share()
);
@@ -127,7 +134,14 @@ export class BookPatchService {
progressPercent: percentage
};
}
return this.http.post<void>(`${this.url}/progress`, body);
return this.http.post<void>(`${this.url}/progress`, body).pipe(
tap(() => {
patchBookFieldsInCache(this.queryClient, [{
bookId,
fields: {pdfProgress: {page, percentage}}
}]);
})
);
}
saveEpubProgress(bookId: number, cfi: string, href: string, percentage: number, bookFileId?: number): void {
@@ -160,7 +174,14 @@ export class BookPatchService {
progressPercent: percentage
};
}
return this.http.post<void>(`${this.url}/progress`, body);
return this.http.post<void>(`${this.url}/progress`, body).pipe(
tap(() => {
patchBookFieldsInCache(this.queryClient, [{
bookId,
fields: {cbxProgress: {page, percentage}}
}]);
})
);
}
saveFileProgress(bookId: number, fileProgress: BookFileProgress): Observable<void> {
@@ -121,6 +121,20 @@ export class BookService {
return this.queryClient.ensureQueryData(this.bookDetailQueryOptions(bookId, withDescription));
}
/**
* Always fetches book detail from the server, bypassing the cache.
*
* Used by reader components on initialization to guarantee the latest reading position.
* While optimistic cache updates keep progress current within a single session,
* this network request is necessary to pick up progress saved on another device or browser.
*/
fetchFreshBookDetail(bookId: number, withDescription: boolean): Promise<Book> {
return this.queryClient.fetchQuery({
...this.bookDetailQueryOptions(bookId, withDescription),
staleTime: 0,
});
}
private getBookRecommendationsQueryOptions(bookId: number, limit: number) {
return queryOptions({
queryKey: bookRecommendationsQueryKey(bookId, limit),