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:
@@ -1,13 +1,14 @@
|
|||||||
// PDF.js Viewer for Blazor WASM
|
// PDF.js Viewer for Blazor WASM
|
||||||
window.pdfViewer = {
|
window.pdfViewer = {
|
||||||
pdfDoc: null,
|
pdfDoc: null,
|
||||||
pageNum: 1,
|
pageNum: 1,
|
||||||
pageRendering: false,
|
pageRendering: false,
|
||||||
pageNumPending: null,
|
pageNumPending: null,
|
||||||
scale: 1.5,
|
scale: 1.5,
|
||||||
canvas: null,
|
canvas: null,
|
||||||
ctx: null,
|
ctx: null,
|
||||||
totalPages: 0,
|
totalPages: 0,
|
||||||
|
currentRenderTask: null,
|
||||||
|
|
||||||
async initialize(canvasId, pdfDataUrl) {
|
async initialize(canvasId, pdfDataUrl) {
|
||||||
try {
|
try {
|
||||||
@@ -90,10 +91,17 @@ window.pdfViewer = {
|
|||||||
viewport: viewport
|
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');
|
console.log('Page rendered successfully');
|
||||||
|
|
||||||
|
this.currentRenderTask = null;
|
||||||
this.pageRendering = false;
|
this.pageRendering = false;
|
||||||
|
|
||||||
if (this.pageNumPending !== null) {
|
if (this.pageNumPending !== null) {
|
||||||
@@ -101,7 +109,12 @@ window.pdfViewer = {
|
|||||||
this.pageNumPending = null;
|
this.pageNumPending = null;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} 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;
|
this.pageRendering = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user