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

Conversation

djsamseng
Copy link
Contributor

Test Plan: Copy into local project and quickly change pages. Ensure that the correct page gets rendered. Run the demo app with the following changes to observe the issue and test the fix

  useEffect(() => {
    for (let i = 0; i < 8; i++) {
      const pgNum = (i % 2) + 8;

      setTimeout(() => {
        setPage(pgNum);
      }, i * 100);
    }
  }, []);

  const { pdfDocument } = usePdf({
    file: 'plan.pdf',
    page,
    canvasRef,
    scale: 1.5,
    onPageRenderSuccess: page => {
      console.log("Render success:", page.pageNumber);
    }
  });
  console.log("Set page:", page);

Without these changes:

Set page: 8
Set page: 9
Set page: 8
Set page: 9
Set page: 8
Set page: 9
Set page: 8
Set page: 9
Render success: 8

With these changes:

Set page: 8
Set page: 9
Set page: 8
Set page: 9
Set page: 8
Set page: 9
Set page: 8
Set page: 9
Render success: 9

Note that I did have to use a pdf with pages that required a lot of rendering as well as a high scale in order to reproduce the issue

@vercel
Copy link

vercel bot commented Jan 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-pdf-js ❌ Failed (Inspect) Jan 16, 2024 9:39pm

Test Plan: Copy into local project and quickly change
pages. Ensure that the correct page gets rendered.
Copy link
Owner

@mikecousins mikecousins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@mikecousins mikecousins merged commit 7220de3 into mikecousins:main Jan 16, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants