Enable cancelling of thumbnail drawing

This is useful if thumbnails are being rendered when the document is rotated, since it let us abort the current rendering.
This commit is contained in:
Jonas Jenwald 2015-01-25 16:59:57 +01:00
parent 878aa76c15
commit b7041f81f4

View File

@ -130,6 +130,9 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
}, },
reset: function PDFThumbnailView_reset() { reset: function PDFThumbnailView_reset() {
if (this.renderTask) {
this.renderTask.cancel();
}
this.hasImage = false; this.hasImage = false;
this.resume = null; this.resume = null;
this.renderingState = RenderingStates.INITIAL; this.renderingState = RenderingStates.INITIAL;
@ -213,31 +216,53 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
}); });
var self = this; var self = this;
function thumbnailDrawCallback(error) {
// The renderTask may have been replaced by a new one, so only remove
// the reference to the renderTask if it matches the one that is
// triggering this callback.
if (renderTask === self.renderTask) {
self.renderTask = null;
}
if (error === 'cancelled') {
rejectRenderPromise(error);
return;
}
self.renderingState = RenderingStates.FINISHED;
if (!error) {
resolveRenderPromise(undefined);
} else {
rejectRenderPromise(error);
}
}
var ctx = this._getPageDrawContext(); var ctx = this._getPageDrawContext();
var drawViewport = this.viewport.clone({ scale: this.scale }); var drawViewport = this.viewport.clone({ scale: this.scale });
var renderContinueCallback = function renderContinueCallback(cont) {
if (!self.renderingQueue.isHighestPriority(self)) {
self.renderingState = RenderingStates.PAUSED;
self.resume = function resumeCallback() {
self.renderingState = RenderingStates.RUNNING;
cont();
};
return;
}
cont();
};
var renderContext = { var renderContext = {
canvasContext: ctx, canvasContext: ctx,
viewport: drawViewport, viewport: drawViewport,
continueCallback: function renderContinueCallback(cont) { continueCallback: renderContinueCallback
if (!self.renderingQueue.isHighestPriority(self)) {
self.renderingState = RenderingStates.PAUSED;
self.resume = function() {
self.renderingState = RenderingStates.RUNNING;
cont();
};
return;
}
cont();
}
}; };
this.pdfPage.render(renderContext).promise.then( var renderTask = this.renderTask = this.pdfPage.render(renderContext);
renderTask.promise.then(
function pdfPageRenderCallback() { function pdfPageRenderCallback() {
self.renderingState = RenderingStates.FINISHED; thumbnailDrawCallback(null);
resolveRenderPromise(undefined);
}, },
function pdfPageRenderError(error) { function pdfPageRenderError(error) {
self.renderingState = RenderingStates.FINISHED; thumbnailDrawCallback(error);
rejectRenderPromise(error);
} }
); );
return promise; return promise;