]> git.parisson.com Git - pdf.js.git/commitdiff
Stop the execution if it takes longer then a certain amount of time and reshedule it
authorJulian Viereck <julian.viereck@gmail.com>
Fri, 9 Sep 2011 16:58:44 +0000 (09:58 -0700)
committerJulian Viereck <julian.viereck@gmail.com>
Thu, 15 Sep 2011 20:16:03 +0000 (13:16 -0700)
pdf.js

diff --git a/pdf.js b/pdf.js
index 44873a744844792370789ae936b277cf1b56e8bc..9c8ea2fcc00310f6d019a823146943c538dd4cc7 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -4849,6 +4849,11 @@ function ScratchCanvas(width, height) {
 }
 
 var CanvasGraphics = (function() {
+  var kScalePrecision = 50;
+  var kRasterizerMin = 14;
+  var kExecutionTime = 50;
+  var kExecutionTimeCheck = 500;
+
   function constructor(canvasCtx, imageCanvas) {
     this.ctx = canvasCtx;
     this.current = new CanvasExtraState();
@@ -4885,34 +4890,54 @@ var CanvasGraphics = (function() {
       this.ctx.scale(cw / mediaBox.width, ch / mediaBox.height);
     },
 
-    executeIRQueue: function(codeIR, startIdx, continueCallback) {
+    executeIRQueue: function(codeIR, executionStartIdx, continueCallback) {
       var argsArray = codeIR.argsArray;
       var fnArray =   codeIR.fnArray;
-      var i = startIdx || 0;
-      var length = argsArray.length;
-      for (i; i < length; i++) {
-        if (fnArray[i] !== "dependency") {
-          this[fnArray[i]].apply(this, argsArray[i]);
-        } else {
-          var deps = argsArray[i];
-          for (var n = 0; n < deps.length; n++) {
-            var depObjId = deps[n];
-            var promise;
-            if (!Objects[depObjId]) {
-              promise = Objects[depObjId] = new Promise();
-            } else {
-              promise = Objects[depObjId];
-            }
-            // If the promise isn't resolved yet, add the continueCallback
-            // to the promise and bail out.
-            if (!promise.isResolved) {
-              promise.then(continueCallback);
-              return i;
+      var i = executionStartIdx || 0;
+      var argsArrayLen = argsArray.length;
+      
+      var executionEndIdx;
+      var startTime = Date.now();
+
+      do {
+        executionEndIdx = Math.min(argsArrayLen, i + kExecutionTimeCheck);
+        
+        for (i; i < executionEndIdx; i++) {
+          if (fnArray[i] !== "dependency") {
+            this[fnArray[i]].apply(this, argsArray[i]);
+          } else {
+            var deps = argsArray[i];
+            for (var n = 0; n < deps.length; n++) {
+              var depObjId = deps[n];
+              var promise;
+              if (!Objects[depObjId]) {
+                promise = Objects[depObjId] = new Promise();
+              } else {
+                promise = Objects[depObjId];
+              }
+              // If the promise isn't resolved yet, add the continueCallback
+              // to the promise and bail out.
+              if (!promise.isResolved) {
+                promise.then(continueCallback);
+                return i;
+              }
             }
           }
         }
-      }
-      return i; 
+
+        // If the entire IRQueue was executed, stop as were done.
+        if (i == argsArrayLen) {
+          return i;
+        } 
+        // If the execution took longer then a certain amount of time, shedule
+        // to continue exeution after a short delay.
+        else if ((Date.now() - startTime) > kExecutionTime) {
+          setTimeout(continueCallback, 0);
+          return i;
+        }
+        // If the IRQueue isn't executed completly yet OR the execution time
+        // was short enough, do another execution round.
+      } while (true);
     },
 
     endDrawing: function() {