]> git.parisson.com Git - pdf.js.git/commitdiff
Add displayReadyPromise to the Page object to not request the IRQueue all the time...
authorJulian Viereck <julian.viereck@gmail.com>
Thu, 22 Dec 2011 18:04:53 +0000 (19:04 +0100)
committerJulian Viereck <julian.viereck@gmail.com>
Mon, 2 Jan 2012 19:02:20 +0000 (20:02 +0100)
src/core.js

index 6a0c82aa6b95d4b9d1a5a503220ed193d0f289d1..8b5fbea753a9d7104a58de89a3d4178e79fd7c35 100644 (file)
@@ -70,8 +70,7 @@ var Page = (function PageClosure() {
     this.xref = xref;
     this.ref = ref;
 
-    this.ctx = null;
-    this.callback = null;
+    this.displayReadyPromise = null;
   }
 
   Page.prototype = {
@@ -167,20 +166,12 @@ var Page = (function PageClosure() {
                                                 IRQueue, fonts) {
       var self = this;
       this.IRQueue = IRQueue;
-      var gfx = new CanvasGraphics(this.ctx, this.objs, this.textLayer);
 
       var displayContinuation = function pageDisplayContinuation() {
         // Always defer call to display() to work around bug in
         // Firefox error reporting from XHR callbacks.
         setTimeout(function pageSetTimeout() {
-          try {
-            self.display(gfx, self.callback);
-          } catch (e) {
-            if (self.callback)
-              self.callback(e);
-            else
-              throw e;
-          }
+          self.displayReadyPromise.resolve();
         });
       };
 
@@ -397,12 +388,27 @@ var Page = (function PageClosure() {
       return items;
     },
     startRendering: function pageStartRendering(ctx, callback, textLayer)  {
-      this.ctx = ctx;
-      this.callback = callback;
-      this.textLayer = textLayer;
-
       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));
     }
   };