Skip to content

Commit 6548c9f

Browse files
committed
Inline the PDFThumbnailView.prototype.#finishRenderTask helper method
Given that the `draw` method is already asynchronous we can easily inline this old helper method, which shortens the code and improves consistency in the code-base (note the `BasePDFPageView`-implementation).
1 parent 7ee061b commit 6548c9f

1 file changed

Lines changed: 32 additions & 35 deletions

File tree

web/pdf_thumbnail_view.js

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -261,31 +261,12 @@ class PDFThumbnailView {
261261
zeroCanvas(reducedCanvas);
262262
}
263263

264-
async #finishRenderTask(renderTask, canvas, error = null) {
265-
// The renderTask may have been replaced by a new one, so only remove
266-
// the reference to the renderTask if it matches the one that is
267-
// triggering this callback.
268-
if (renderTask === this.renderTask) {
269-
this.renderTask = null;
270-
}
271-
272-
if (error instanceof RenderingCancelledException) {
273-
return;
274-
}
275-
this.renderingState = RenderingStates.FINISHED;
276-
this.#convertCanvasToImage(canvas);
277-
278-
if (error) {
279-
throw error;
280-
}
281-
}
282-
283264
async draw() {
284265
if (this.renderingState !== RenderingStates.INITIAL) {
285266
console.error("Must be in new state before drawing");
286-
return undefined;
267+
return;
287268
}
288-
const { pdfPage } = this;
269+
const { pageColors, pdfPage } = this;
289270

290271
if (!pdfPage) {
291272
this.renderingState = RenderingStates.FINISHED;
@@ -321,26 +302,42 @@ class PDFThumbnailView {
321302
transform,
322303
viewport: drawViewport,
323304
optionalContentConfigPromise: this._optionalContentConfigPromise,
324-
pageColors: this.pageColors,
305+
pageColors,
325306
};
326307
const renderTask = (this.renderTask = pdfPage.render(renderContext));
327308
renderTask.onContinue = renderContinueCallback;
328309

329-
const resultPromise = renderTask.promise.then(
330-
() => this.#finishRenderTask(renderTask, canvas),
331-
error => this.#finishRenderTask(renderTask, canvas, error)
332-
);
333-
resultPromise.finally(() => {
334-
zeroCanvas(canvas);
335-
336-
this.eventBus.dispatch("thumbnailrendered", {
337-
source: this,
338-
pageNumber: this.id,
339-
pdfPage: this.pdfPage,
340-
});
310+
let error = null;
311+
try {
312+
await renderTask.promise;
313+
} catch (e) {
314+
if (e instanceof RenderingCancelledException) {
315+
zeroCanvas(canvas);
316+
return;
317+
}
318+
error = e;
319+
} finally {
320+
// The renderTask may have been replaced by a new one, so only remove
321+
// the reference to the renderTask if it matches the one that is
322+
// triggering this callback.
323+
if (renderTask === this.renderTask) {
324+
this.renderTask = null;
325+
}
326+
}
327+
this.renderingState = RenderingStates.FINISHED;
328+
329+
this.#convertCanvasToImage(canvas);
330+
zeroCanvas(canvas);
331+
332+
this.eventBus.dispatch("thumbnailrendered", {
333+
source: this,
334+
pageNumber: this.id,
335+
pdfPage,
341336
});
342337

343-
return resultPromise;
338+
if (error) {
339+
throw error;
340+
}
344341
}
345342

346343
setImage(pageView) {

0 commit comments

Comments
 (0)