Preload previous/next chapter in reader

This commit is contained in:
schroda
2025-02-10 01:05:35 +01:00
parent 16c1edaa52
commit 174d850093
14 changed files with 185 additions and 44 deletions

View File

@@ -18,6 +18,7 @@ import {
ReaderExitMode,
ReaderOverlayMode,
ReaderResumeMode,
ReaderStateChapters,
ReadingDirection,
ReadingMode,
} from '@/modules/reader/types/Reader.types.ts';
@@ -44,6 +45,8 @@ import {
getChapterIdsForDownloadAhead,
getChapterIdsToDeleteForChapterUpdate,
getReaderChapterFromCache,
isInDownloadAheadRange,
updateReaderStateVisibleChapters,
} from '@/modules/reader/utils/Reader.utils.ts';
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
import { Queue } from '@/lib/Queue.ts';
@@ -118,6 +121,42 @@ export class ReaderService {
});
}
static preloadChapter(
pageIndex: number,
pageCount: number,
chapter: TChapterReader | undefined | null,
lastLeadingChapterSourceOrder: number,
lastTrailingChapterSourceOrder: number,
setReaderStateChapters: ReaderStateChapters['setReaderStateChapters'],
direction: DirectionOffset,
): void {
if (!chapter) {
return;
}
const isPreviousChapter = direction === DirectionOffset.PREVIOUS;
const isNextChapter = direction === DirectionOffset.NEXT;
const isAlreadyPreloaded =
(isPreviousChapter && lastLeadingChapterSourceOrder <= chapter.sourceOrder) ||
(isNextChapter && lastTrailingChapterSourceOrder >= chapter.sourceOrder);
const shouldPreload = !isAlreadyPreloaded && isInDownloadAheadRange(pageIndex, pageCount, direction);
if (!shouldPreload) {
return;
}
setReaderStateChapters((state) =>
updateReaderStateVisibleChapters(
isPreviousChapter,
state,
chapter.sourceOrder,
false,
isPreviousChapter ? true : undefined,
isNextChapter ? true : undefined,
),
);
}
static useUpdateChapter(): (patch: UpdateChapterPatchInput) => void {
const { manga } = useReaderStateMangaContext();
const { chapterForDuplicatesHandling, currentChapter, mangaChapters } = useReaderStateChaptersContext();