Convert InternalRenderTask, in src/display/api.js, to an ES6 class

This changes all occurrences of `var` to `let`/`const` in this code, and updates the signature of the constructor to use object destructuring for better readability (and self documentation).
Also, `useRequestAnimationFrame` is changed to a parameter and the `typeof window` check is now done *once* rather than at every `_scheduleNext` call.
This commit is contained in:
Jonas Jenwald 2018-11-08 14:33:56 +01:00
parent 5a0d64a6de
commit 02e77a39ec
2 changed files with 88 additions and 87 deletions

View File

@ -1002,21 +1002,25 @@ class PDFPageProxy {
stats.timeEnd('Overall'); stats.timeEnd('Overall');
}; };
const internalRenderTask = new InternalRenderTask(complete, { const internalRenderTask = new InternalRenderTask({
canvasContext, callback: complete,
viewport, params: { // Include the required properties, and *not* the entire object.
transform, canvasContext,
imageLayer, viewport,
background, transform,
}, imageLayer,
this.objs, background,
this.commonObjs, },
intentState.operatorList, objs: this.objs,
this.pageNumber, commonObjs: this.commonObjs,
canvasFactoryInstance, operatorList: intentState.operatorList,
webGLContext, pageNumber: this.pageNumber,
this._pdfBug); canvasFactory: canvasFactoryInstance,
internalRenderTask.useRequestAnimationFrame = renderingIntent !== 'print'; webGLContext,
useRequestAnimationFrame: renderingIntent !== 'print',
pdfBug: this._pdfBug,
});
if (!intentState.renderTasks) { if (!intentState.renderTasks) {
intentState.renderTasks = []; intentState.renderTasks = [];
} }
@ -2340,40 +2344,40 @@ class RenderTask {
* For internal use only. * For internal use only.
* @ignore * @ignore
*/ */
var InternalRenderTask = (function InternalRenderTaskClosure() { const InternalRenderTask = (function InternalRenderTaskClosure() {
let canvasInRendering = new WeakSet(); const canvasInRendering = new WeakSet();
function InternalRenderTask(callback, params, objs, commonObjs, operatorList, class InternalRenderTask {
pageNumber, canvasFactory, webGLContext, constructor({ callback, params, objs, commonObjs, operatorList, pageNumber,
pdfBug = false) { canvasFactory, webGLContext, useRequestAnimationFrame = false,
this.callback = callback; pdfBug = false, }) {
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.pageNumber = pageNumber; this.operatorList = operatorList;
this.canvasFactory = canvasFactory; this.pageNumber = pageNumber;
this.webGLContext = webGLContext; this.canvasFactory = canvasFactory;
this._pdfBug = pdfBug; this.webGLContext = webGLContext;
this._pdfBug = pdfBug;
this.running = false; this.running = false;
this.graphicsReadyCallback = null; this.graphicsReadyCallback = null;
this.graphicsReady = false; this.graphicsReady = false;
this.useRequestAnimationFrame = false; this._useRequestAnimationFrame = (useRequestAnimationFrame === true &&
this.cancelled = false; typeof window !== 'undefined');
this.capability = createPromiseCapability(); this.cancelled = false;
this.task = new RenderTask(this); this.capability = createPromiseCapability();
// caching this-bound methods this.task = new RenderTask(this);
this._continueBound = this._continue.bind(this); // caching this-bound methods
this._scheduleNextBound = this._scheduleNext.bind(this); this._continueBound = this._continue.bind(this);
this._nextBound = this._next.bind(this); this._scheduleNextBound = this._scheduleNext.bind(this);
this._canvas = params.canvasContext.canvas; this._nextBound = this._next.bind(this);
} this._canvas = params.canvasContext.canvas;
}
InternalRenderTask.prototype = { initializeGraphics(transparency = false) {
initializeGraphics(transparency) {
if (this.cancelled) { if (this.cancelled) {
return; return;
} }
@ -2393,26 +2397,27 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
this.stepper.init(this.operatorList); this.stepper.init(this.operatorList);
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint(); this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
} }
const {
canvasContext, viewport, transform, imageLayer, background,
} = this.params;
var params = this.params; this.gfx = new CanvasGraphics(canvasContext, this.commonObjs, this.objs,
this.gfx = new CanvasGraphics(params.canvasContext, this.commonObjs, this.canvasFactory, this.webGLContext,
this.objs, this.canvasFactory, imageLayer);
this.webGLContext, params.imageLayer);
this.gfx.beginDrawing({ this.gfx.beginDrawing({
transform: params.transform, transform,
viewport: params.viewport, viewport,
transparency, transparency,
background: params.background, background,
}); });
this.operatorListIdx = 0; this.operatorListIdx = 0;
this.graphicsReady = true; this.graphicsReady = true;
if (this.graphicsReadyCallback) { if (this.graphicsReadyCallback) {
this.graphicsReadyCallback(); this.graphicsReadyCallback();
} }
}, }
cancel: function InternalRenderTask_cancel() { cancel() {
this.running = false; this.running = false;
this.cancelled = true; this.cancelled = true;
if (this.gfx) { if (this.gfx) {
@ -2423,9 +2428,9 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
} }
this.callback(new RenderingCancelledException( this.callback(new RenderingCancelledException(
'Rendering cancelled, page ' + this.pageNumber, 'canvas')); 'Rendering cancelled, page ' + this.pageNumber, 'canvas'));
}, }
operatorListChanged: function InternalRenderTask_operatorListChanged() { operatorListChanged() {
if (!this.graphicsReady) { if (!this.graphicsReady) {
if (!this.graphicsReadyCallback) { if (!this.graphicsReadyCallback) {
this.graphicsReadyCallback = this._continueBound; this.graphicsReadyCallback = this._continueBound;
@ -2441,9 +2446,9 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
return; return;
} }
this._continue(); this._continue();
}, }
_continue: function InternalRenderTask__continue() { _continue() {
this.running = true; this.running = true;
if (this.cancelled) { if (this.cancelled) {
return; return;
@ -2453,42 +2458,38 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
} else { } else {
this._scheduleNext(); this._scheduleNext();
} }
}, }
_scheduleNext: function InternalRenderTask__scheduleNext() { _scheduleNext() {
if (this.useRequestAnimationFrame && typeof window !== 'undefined') { if (this._useRequestAnimationFrame) {
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
this._nextBound().catch(this.callback); this._nextBound().catch(this.callback);
}); });
} else { } else {
Promise.resolve().then(this._nextBound).catch(this.callback); Promise.resolve().then(this._nextBound).catch(this.callback);
} }
}, }
_next: function InternalRenderTask__next() { async _next() {
return new Promise(() => { if (this.cancelled) {
if (this.cancelled) { return;
return; }
} this.operatorListIdx = this.gfx.executeOperatorList(this.operatorList,
this.operatorListIdx = this.gfx.executeOperatorList(this.operatorList, this.operatorListIdx,
this.operatorListIdx, this._continueBound,
this._continueBound, this.stepper);
this.stepper); if (this.operatorListIdx === this.operatorList.argsArray.length) {
if (this.operatorListIdx === this.operatorList.argsArray.length) { this.running = false;
this.running = false; if (this.operatorList.lastChunk) {
if (this.operatorList.lastChunk) { this.gfx.endDrawing();
this.gfx.endDrawing(); if (this._canvas) {
if (this._canvas) { canvasInRendering.delete(this._canvas);
canvasInRendering.delete(this._canvas);
}
this.callback();
} }
this.callback();
} }
}); }
}, }
}
};
return InternalRenderTask; return InternalRenderTask;
})(); })();

View File

@ -726,7 +726,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
CanvasGraphics.prototype = { CanvasGraphics.prototype = {
beginDrawing({ transform, viewport, transparency, beginDrawing({ transform, viewport, transparency = false,
background = null, }) { background = null, }) {
// For pdfs that use blend modes we have to clear the canvas else certain // For pdfs that use blend modes we have to clear the canvas else certain
// blend modes can look wrong since we'd be blending with a white // blend modes can look wrong since we'd be blending with a white