Add task management for PDF rendering operations

Introduced `currentRenderTask` to track and manage rendering tasks, ensuring only one task is active at a time. Added logic to cancel ongoing tasks before starting new ones, preventing conflicts and redundant rendering.

Enhanced error handling to gracefully manage `RenderingCancelledException` and allow rendering of pending pages. Improved logging for better debugging and insights into the rendering process. Ensured clean state management by resetting `currentRenderTask` after task completion or cancellation.
This commit is contained in:
2026-06-05 10:40:09 +02:00
parent f4b78bce36
commit e3bc439444

View File

@@ -1,13 +1,14 @@
// PDF.js Viewer for Blazor WASM
window.pdfViewer = {
pdfDoc: null,
pageNum: 1,
pageRendering: false,
pageNumPending: null,
scale: 1.5,
canvas: null,
ctx: null,
totalPages: 0,
pdfDoc: null,
pageNum: 1,
pageRendering: false,
pageNumPending: null,
scale: 1.5,
canvas: null,
ctx: null,
totalPages: 0,
currentRenderTask: null,
async initialize(canvasId, pdfDataUrl) {
try {
@@ -90,10 +91,17 @@ window.pdfViewer = {
viewport: viewport
};
await page.render(renderContext).promise;
if (this.currentRenderTask) {
console.log('Cancelling previous render task');
this.currentRenderTask.cancel();
}
this.currentRenderTask = page.render(renderContext);
await this.currentRenderTask.promise;
console.log('Page rendered successfully');
this.currentRenderTask = null;
this.pageRendering = false;
if (this.pageNumPending !== null) {
@@ -101,7 +109,12 @@ window.pdfViewer = {
this.pageNumPending = null;
}
} catch (error) {
console.error('Error rendering page:', error);
if (error.name === 'RenderingCancelledException') {
console.log('Rendering cancelled, will render pending page');
} else {
console.error('Error rendering page:', error);
}
this.currentRenderTask = null;
this.pageRendering = false;
}
},