Skip to content

Commit

Permalink
pdfjs rendering race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Dec 20, 2024
1 parent be383f2 commit 8adcb7d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/shared/EmbeddedFileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export class EmbeddedFilesLoader {
// Get page
const page = await pdfDoc.getPage(num);
// Set scale
const viewport = await page.getViewport({ scale });
const viewport = page.getViewport({ scale });
height = canvas.height = viewport.height;
width = canvas.width = viewport.width;

Expand All @@ -810,7 +810,22 @@ export class EmbeddedFilesLoader {
viewport
};

await page.render(renderCtx).promise;
//when obsidian loads there seems to be an occasional race condition where the rendering is cancelled
//this is a workaround for that
const maxRetries = 3;
for (let i = 0; i < maxRetries; i++) {
try {
await page.render(renderCtx).promise;
break;
} catch (e) {
if (i === maxRetries - 1) throw e; // Throw on last retry
if (e.name === 'RenderingCancelledException') {
await new Promise(resolve => setTimeout(resolve, 50 * (i + 1))); // Incremental backoff
continue;
}
throw e; // Throw other errors immediately
}
}
if(validRect) {
const [left, bottom, _, top] = page.view;

Expand Down

0 comments on commit 8adcb7d

Please sign in to comment.