Uses rAF to interrupt the operator list execution

This commit is contained in:
Yury Delendik 2014-05-09 07:00:47 -05:00
parent abc924b5af
commit c5eb058b09
4 changed files with 28 additions and 18 deletions

View File

@ -1271,6 +1271,10 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
this.graphicsReady = false; this.graphicsReady = false;
this.cancelled = false; this.cancelled = false;
this.capability = createPromiseCapability(); this.capability = createPromiseCapability();
// caching this-bound methods
this._continueBound = this._continue.bind(this);
this._scheduleNextBound = this._scheduleNext.bind(this);
this._nextBound = this._next.bind(this);
} }
InternalRenderTask.prototype = { InternalRenderTask.prototype = {
@ -1309,7 +1313,7 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
operatorListChanged: function InternalRenderTask_operatorListChanged() { operatorListChanged: function InternalRenderTask_operatorListChanged() {
if (!this.graphicsReady) { if (!this.graphicsReady) {
if (!this.graphicsReadyCallback) { if (!this.graphicsReadyCallback) {
this.graphicsReadyCallback = this._continue.bind(this); this.graphicsReadyCallback = this._continueBound;
} }
return; return;
} }
@ -1330,19 +1334,23 @@ var InternalRenderTask = (function InternalRenderTaskClosure() {
return; return;
} }
if (this.params.continueCallback) { if (this.params.continueCallback) {
this.params.continueCallback(this._next.bind(this)); this.params.continueCallback(this._scheduleNextBound);
} else { } else {
this._next(); this._scheduleNext();
} }
}, },
_scheduleNext: function InternalRenderTask__scheduleNext() {
window.requestAnimationFrame(this._nextBound);
},
_next: function InternalRenderTask__next() { _next: function InternalRenderTask__next() {
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._continue.bind(this), 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;

View File

@ -746,7 +746,6 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var commonObjs = this.commonObjs; var commonObjs = this.commonObjs;
var objs = this.objs; var objs = this.objs;
var fnId; var fnId;
var deferred = Promise.resolve();
while (true) { while (true) {
if (stepper && i === stepper.nextBreakPoint) { if (stepper && i === stepper.nextBreakPoint) {
@ -784,11 +783,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
return i; return i;
} }
// If the execution took longer then a certain amount of time, schedule // If the execution took longer then a certain amount of time and
// to continue exeution after a short delay. // `continueCallback` is specified, interrupt the execution.
// However, this is only possible if a 'continueCallback' is passed in.
if (continueCallback && Date.now() > endTime) { if (continueCallback && Date.now() > endTime) {
deferred.then(continueCallback); continueCallback();
return i; return i;
} }

View File

@ -564,3 +564,15 @@ if (typeof PDFJS === 'undefined') {
console.log('Unable to create polyfill for localStorage'); console.log('Unable to create polyfill for localStorage');
} }
})(); })();
(function checkRequestAnimationFrame() {
if ('requestAnimationFrame' in window) {
return;
}
window.requestAnimationFrame =
window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame ||
window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
(function fakeRequestAnimationFrame(callback) {
window.setTimeout(callback, 20);
});
})();

View File

@ -2410,16 +2410,8 @@ window.addEventListener('afterprint', function afterPrint(evt) {
(function animationStartedClosure() { (function animationStartedClosure() {
// The offsetParent is not set until the pdf.js iframe or object is visible. // The offsetParent is not set until the pdf.js iframe or object is visible.
// Waiting for first animation. // Waiting for first animation.
var requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function startAtOnce(callback) { callback(); };
PDFView.animationStartedPromise = new Promise(function (resolve) { PDFView.animationStartedPromise = new Promise(function (resolve) {
requestAnimationFrame(function onAnimationFrame() { window.requestAnimationFrame(resolve);
resolve();
});
}); });
})(); })();