From cabc2cc4fcc5c2361c693e4324419f3b49b2479b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jun 2020 15:38:09 +0200 Subject: [PATCH] Add a `InternalRenderTask.completed` getter and use it to simplify `PDFPageProxy._destroy` This patch aims to simplify the `PDFPageProxy._destroy` method, by: - Replacing the unnecessary `forEach` with a "regular" `for`-loop instead. - Use a more appropriate variable name, since `intentState.renderTasks` contain instances of `InternalRenderTask`. - Move the "is rendering completed"-handling to a new `InternalRenderTask.completed` getter, to abstract away some (mostly) internal `InternalRenderTask` state. --- src/display/api.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 8a556c40a..784995825 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1234,13 +1234,10 @@ class PDFPageProxy { // Avoid errors below, since the renderTasks are just stubs. return; } - intentState.renderTasks.forEach(function (renderTask) { - const renderCompleted = renderTask.capability.promise.catch( - function () {} - ); // ignoring failures - waitOn.push(renderCompleted); - renderTask.cancel(); - }); + for (const internalRenderTask of intentState.renderTasks) { + waitOn.push(internalRenderTask.completed); + internalRenderTask.cancel(); + } }); this.objs.clear(); this.annotationsPromise = null; @@ -2650,6 +2647,13 @@ const InternalRenderTask = (function InternalRenderTaskClosure() { this._canvas = params.canvasContext.canvas; } + get completed() { + return this.capability.promise.catch(function () { + // Ignoring errors, since we only want to know when rendering is + // no longer pending. + }); + } + initializeGraphics(transparency = false) { if (this.cancelled) { return;