Skip to content

Commit

Permalink
Reduce chapter updates in the reader
Browse files Browse the repository at this point in the history
Debounce updating chapter, unless it's the last page, when switching/scrolling pages really fast.
Reduces amount of requests send to the server.
  • Loading branch information
schroda committed Nov 11, 2023
1 parent a0a52b1 commit b080286
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/screens/Reader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { VerticalPager } from '@/components/reader/pager/VerticalPager';
import { ReaderNavBar } from '@/components/navbar/ReaderNavBar';
import { makeToast } from '@/components/util/Toast';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
import { useDebounce } from '@/components/manga/hooks.ts';

const isDupChapter = async (chapterIndex: number, currentChapter: TChapter) => {
const nextChapter = await requestManager.getChapter(currentChapter.manga.id, chapterIndex).response;
Expand Down Expand Up @@ -158,6 +159,8 @@ export function Reader() {
const isLoading = isChapterLoading || !arePagesUpdated;
const [wasLastPageReadSet, setWasLastPageReadSet] = useState(false);
const [curPage, setCurPage] = useState<number>(0);
const isLastPage = curPage === chapter.pageCount - 1;
const curPageDebounced = useDebounce(curPage, isLastPage ? 0 : 1000);
const [pageToScrollTo, setPageToScrollTo] = useState<number | undefined>(undefined);
const { setOverride, setTitle } = useContext(NavBarContext);
const [retrievingNextChapter, setRetrievingNextChapter] = useState(false);
Expand Down Expand Up @@ -248,17 +251,17 @@ export function Reader() {
}

// do not mutate the chapter, this will cause the page to jump around due to always scrolling to the last read page
const updateLastPageRead = curPage !== -1;
const updateIsRead = curPage === chapter.pageCount - 1;
const updateLastPageRead = curPageDebounced !== -1;
const updateIsRead = curPageDebounced === chapter.pageCount - 1;
const updateChapter = updateLastPageRead || updateIsRead;

if (updateChapter) {
requestManager.updateChapter(chapter.id, {
lastPageRead: updateLastPageRead ? curPage : undefined,
lastPageRead: updateLastPageRead ? curPageDebounced : undefined,
isRead: updateIsRead ? true : undefined,
});
}
}, [curPage]);
}, [curPageDebounced]);

const nextChapter = useCallback(() => {
if (chapter.sourceOrder < manga.chapters.totalCount) {
Expand Down

0 comments on commit b080286

Please sign in to comment.