Remove the closure used with the InternalRenderTask class

This patch utilizes the same approach as used in lots of other parts of the code-base, which thus *slightly* reduces the size of this code.
This commit is contained in:
Jonas Jenwald 2021-07-28 11:58:08 +02:00
parent b18620ac0f
commit 4c679d80ac

View File

@ -3063,181 +3063,179 @@ class RenderTask {
* For internal use only. * For internal use only.
* @ignore * @ignore
*/ */
const InternalRenderTask = (function InternalRenderTaskClosure() { class InternalRenderTask {
const canvasInRendering = new WeakSet(); static get canvasInUse() {
return shadow(this, "canvasInUse", new WeakSet());
}
// eslint-disable-next-line no-shadow constructor({
class InternalRenderTask { callback,
constructor({ params,
callback, objs,
params, commonObjs,
objs, operatorList,
commonObjs, pageIndex,
operatorList, canvasFactory,
pageIndex, useRequestAnimationFrame = false,
canvasFactory, pdfBug = false,
useRequestAnimationFrame = false, }) {
pdfBug = false, this.callback = callback;
}) { this.params = params;
this.callback = callback; this.objs = objs;
this.params = params; this.commonObjs = commonObjs;
this.objs = objs; this.operatorListIdx = null;
this.commonObjs = commonObjs; this.operatorList = operatorList;
this.operatorListIdx = null; this._pageIndex = pageIndex;
this.operatorList = operatorList; this.canvasFactory = canvasFactory;
this._pageIndex = pageIndex; this._pdfBug = pdfBug;
this.canvasFactory = canvasFactory;
this._pdfBug = pdfBug;
this.running = false; this.running = false;
this.graphicsReadyCallback = null; this.graphicsReadyCallback = null;
this.graphicsReady = false; this.graphicsReady = false;
this._useRequestAnimationFrame = this._useRequestAnimationFrame =
useRequestAnimationFrame === true && typeof window !== "undefined"; useRequestAnimationFrame === true && typeof window !== "undefined";
this.cancelled = false; this.cancelled = false;
this.capability = createPromiseCapability(); this.capability = createPromiseCapability();
this.task = new RenderTask(this); this.task = new RenderTask(this);
// caching this-bound methods // caching this-bound methods
this._cancelBound = this.cancel.bind(this); this._cancelBound = this.cancel.bind(this);
this._continueBound = this._continue.bind(this); this._continueBound = this._continue.bind(this);
this._scheduleNextBound = this._scheduleNext.bind(this); this._scheduleNextBound = this._scheduleNext.bind(this);
this._nextBound = this._next.bind(this); this._nextBound = this._next.bind(this);
this._canvas = params.canvasContext.canvas; 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, optionalContentConfig }) {
if (this.cancelled) {
return;
}
if (this._canvas) {
if (InternalRenderTask.canvasInUse.has(this._canvas)) {
throw new Error(
"Cannot use the same canvas during multiple render() operations. " +
"Use different canvas or ensure previous operations were " +
"cancelled or completed."
);
}
InternalRenderTask.canvasInUse.add(this._canvas);
} }
get completed() { if (this._pdfBug && globalThis.StepperManager?.enabled) {
return this.capability.promise.catch(function () { this.stepper = globalThis.StepperManager.create(this._pageIndex);
// Ignoring errors, since we only want to know when rendering is this.stepper.init(this.operatorList);
// no longer pending. this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
}
const { canvasContext, viewport, transform, imageLayer, background } =
this.params;
this.gfx = new CanvasGraphics(
canvasContext,
this.commonObjs,
this.objs,
this.canvasFactory,
imageLayer,
optionalContentConfig
);
this.gfx.beginDrawing({
transform,
viewport,
transparency,
background,
});
this.operatorListIdx = 0;
this.graphicsReady = true;
if (this.graphicsReadyCallback) {
this.graphicsReadyCallback();
}
}
cancel(error = null) {
this.running = false;
this.cancelled = true;
if (this.gfx) {
this.gfx.endDrawing();
}
if (this._canvas) {
InternalRenderTask.canvasInUse.delete(this._canvas);
}
this.callback(
error ||
new RenderingCancelledException(
`Rendering cancelled, page ${this._pageIndex + 1}`,
"canvas"
)
);
}
operatorListChanged() {
if (!this.graphicsReady) {
if (!this.graphicsReadyCallback) {
this.graphicsReadyCallback = this._continueBound;
}
return;
}
if (this.stepper) {
this.stepper.updateOperatorList(this.operatorList);
}
if (this.running) {
return;
}
this._continue();
}
_continue() {
this.running = true;
if (this.cancelled) {
return;
}
if (this.task.onContinue) {
this.task.onContinue(this._scheduleNextBound);
} else {
this._scheduleNext();
}
}
_scheduleNext() {
if (this._useRequestAnimationFrame) {
window.requestAnimationFrame(() => {
this._nextBound().catch(this._cancelBound);
}); });
} else {
Promise.resolve().then(this._nextBound).catch(this._cancelBound);
} }
}
initializeGraphics({ transparency = false, optionalContentConfig }) { async _next() {
if (this.cancelled) { if (this.cancelled) {
return; return;
}
if (this._canvas) {
if (canvasInRendering.has(this._canvas)) {
throw new Error(
"Cannot use the same canvas during multiple render() operations. " +
"Use different canvas or ensure previous operations were " +
"cancelled or completed."
);
}
canvasInRendering.add(this._canvas);
}
if (this._pdfBug && globalThis.StepperManager?.enabled) {
this.stepper = globalThis.StepperManager.create(this._pageIndex);
this.stepper.init(this.operatorList);
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
}
const { canvasContext, viewport, transform, imageLayer, background } =
this.params;
this.gfx = new CanvasGraphics(
canvasContext,
this.commonObjs,
this.objs,
this.canvasFactory,
imageLayer,
optionalContentConfig
);
this.gfx.beginDrawing({
transform,
viewport,
transparency,
background,
});
this.operatorListIdx = 0;
this.graphicsReady = true;
if (this.graphicsReadyCallback) {
this.graphicsReadyCallback();
}
} }
this.operatorListIdx = this.gfx.executeOperatorList(
cancel(error = null) { this.operatorList,
this.operatorListIdx,
this._continueBound,
this.stepper
);
if (this.operatorListIdx === this.operatorList.argsArray.length) {
this.running = false; this.running = false;
this.cancelled = true; if (this.operatorList.lastChunk) {
if (this.gfx) {
this.gfx.endDrawing(); this.gfx.endDrawing();
} if (this._canvas) {
if (this._canvas) { InternalRenderTask.canvasInUse.delete(this._canvas);
canvasInRendering.delete(this._canvas);
}
this.callback(
error ||
new RenderingCancelledException(
`Rendering cancelled, page ${this._pageIndex + 1}`,
"canvas"
)
);
}
operatorListChanged() {
if (!this.graphicsReady) {
if (!this.graphicsReadyCallback) {
this.graphicsReadyCallback = this._continueBound;
}
return;
}
if (this.stepper) {
this.stepper.updateOperatorList(this.operatorList);
}
if (this.running) {
return;
}
this._continue();
}
_continue() {
this.running = true;
if (this.cancelled) {
return;
}
if (this.task.onContinue) {
this.task.onContinue(this._scheduleNextBound);
} else {
this._scheduleNext();
}
}
_scheduleNext() {
if (this._useRequestAnimationFrame) {
window.requestAnimationFrame(() => {
this._nextBound().catch(this._cancelBound);
});
} else {
Promise.resolve().then(this._nextBound).catch(this._cancelBound);
}
}
async _next() {
if (this.cancelled) {
return;
}
this.operatorListIdx = this.gfx.executeOperatorList(
this.operatorList,
this.operatorListIdx,
this._continueBound,
this.stepper
);
if (this.operatorListIdx === this.operatorList.argsArray.length) {
this.running = false;
if (this.operatorList.lastChunk) {
this.gfx.endDrawing();
if (this._canvas) {
canvasInRendering.delete(this._canvas);
}
this.callback();
} }
this.callback();
} }
} }
} }
return InternalRenderTask; }
})();
/** @type {string} */ /** @type {string} */
const version = const version =