Add displayReadyPromise to the Page object to not request the IRQueue all the time and simplify some stuff

This commit is contained in:
Julian Viereck 2011-12-22 19:04:53 +01:00
parent 0c22e5d653
commit 52aba8648e

View File

@ -70,8 +70,7 @@ var Page = (function PageClosure() {
this.xref = xref; this.xref = xref;
this.ref = ref; this.ref = ref;
this.ctx = null; this.displayReadyPromise = null;
this.callback = null;
} }
Page.prototype = { Page.prototype = {
@ -167,20 +166,12 @@ var Page = (function PageClosure() {
IRQueue, fonts) { IRQueue, fonts) {
var self = this; var self = this;
this.IRQueue = IRQueue; this.IRQueue = IRQueue;
var gfx = new CanvasGraphics(this.ctx, this.objs, this.textLayer);
var displayContinuation = function pageDisplayContinuation() { var displayContinuation = function pageDisplayContinuation() {
// Always defer call to display() to work around bug in // Always defer call to display() to work around bug in
// Firefox error reporting from XHR callbacks. // Firefox error reporting from XHR callbacks.
setTimeout(function pageSetTimeout() { setTimeout(function pageSetTimeout() {
try { self.displayReadyPromise.resolve();
self.display(gfx, self.callback);
} catch (e) {
if (self.callback)
self.callback(e);
else
throw e;
}
}); });
}; };
@ -397,12 +388,27 @@ var Page = (function PageClosure() {
return items; return items;
}, },
startRendering: function pageStartRendering(ctx, callback, textLayer) { startRendering: function pageStartRendering(ctx, callback, textLayer) {
this.ctx = ctx;
this.callback = callback;
this.textLayer = textLayer;
this.startRenderingTime = Date.now(); this.startRenderingTime = Date.now();
this.pdf.startRendering(this);
// If there is no displayReadyPromise yet, then the IRQueue was never
// requested before. Make the request and create the promise.
if (!this.displayReadyPromise) {
this.pdf.startRendering(this);
this.displayReadyPromise = new Promise();
}
// Once the IRQueue and fonts are loaded, perform the actual rendering.
this.displayReadyPromise.then(function pageDisplayReadyPromise() {
var gfx = new CanvasGraphics(ctx, this.objs, textLayer);
try {
this.display(gfx, callback);
} catch (e) {
if (self.callback)
self.callback(e);
else
throw e;
}
}.bind(this));
} }
}; };