Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition: Render last requested page #380

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"trailingComma": "es5"
},
"devDependencies": {
"eslint": "^8.33.0",
"prettier": "3.2.2",
"turbo": "1.11.3",
"typescript": "5.3.3"
Expand Down
6 changes: 5 additions & 1 deletion packages/react-pdf-js/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const usePdf = ({
const [pdfDocument, setPdfDocument] = useState<PDFDocumentProxy>();
const [pdfPage, setPdfPage] = useState<PDFPageProxy>();
const renderTask = useRef<PDFRenderTask | null>(null);
const lastPageRequestedRenderRef = useRef<PDFPageProxy | null>(null);
const onDocumentLoadSuccessRef = useRef(onDocumentLoadSuccess);
const onDocumentLoadFailRef = useRef(onDocumentLoadFail);
const onPageLoadSuccessRef = useRef(onPageLoadSuccess);
Expand Down Expand Up @@ -135,6 +136,7 @@ export const usePdf = ({

// if previous render isn't done yet, we cancel it
if (renderTask.current) {
lastPageRequestedRenderRef.current = page;
renderTask.current.cancel();
return;
}
Expand All @@ -156,7 +158,9 @@ export const usePdf = ({
renderTask.current = null;

if (reason && reason.name === 'RenderingCancelledException') {
drawPDF(page);
const lastPageRequestedRender = lastPageRequestedRenderRef.current ?? page;
lastPageRequestedRenderRef.current = null;
drawPDF(lastPageRequestedRender);
} else if (isFunction(onPageRenderFailRef.current)) {
onPageRenderFailRef.current();
}
Expand Down