]> git.parisson.com Git - pdf.js.git/commitdiff
Fixing web worker feature detection for Safari
authorTim de Koning <github@king-management.nl>
Tue, 26 Jun 2012 21:07:37 +0000 (23:07 +0200)
committerTim de Koning <github@king-management.nl>
Tue, 26 Jun 2012 21:07:37 +0000 (23:07 +0200)
src/api.js

index 7d65f96b4e2d3698e95769144d434b5290c71263..88cc6e288569d25bf2e99f82dd73c57af85bddef 100644 (file)
@@ -18,7 +18,7 @@
  * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
  */
 PDFJS.getDocument = function getDocument(source) {
-  var url, data, headers, password, parameters = {};
+  var url, data, headers, password, parameters = {}, workerInitializedPromise, workerReadyPromise, transport;
   if (typeof source === 'string') {
     url = source;
   } else if (isArrayBuffer(source)) {
@@ -37,8 +37,9 @@ PDFJS.getDocument = function getDocument(source) {
           'string or a parameter object');
   }
 
-  var promise = new PDFJS.Promise();
-  var transport = new WorkerTransport(promise);
+  workerInitializedPromise = new PDFJS.Promise();
+  workerReadyPromise = new PDFJS.Promise();
+  transport = new WorkerTransport(workerInitializedPromise, workerReadyPromise);
   if (data) {
     // assuming the data is array, instantiating directly from it
     transport.sendData(data, parameters);
@@ -49,23 +50,26 @@ PDFJS.getDocument = function getDocument(source) {
         url: url,
         progress: function getPDFProgress(evt) {
           if (evt.lengthComputable)
-            promise.progress({
+                         workerReadyPromise.progress({
               loaded: evt.loaded,
               total: evt.total
             });
         },
         error: function getPDFError(e) {
-          promise.reject('Unexpected server response of ' +
+                       workerReadyPromise.reject('Unexpected server response of ' +
             e.target.status + '.');
         },
         headers: headers
       },
       function getPDFLoad(data) {
-        transport.sendData(data, parameters);
+               //we have to wait for the WorkerTransport to finalize worker-support detection! This may take a while...
+               workerInitializedPromise.then(function () {
+               transport.sendData(data, parameters);
+               });
       });
   }
 
-  return promise;
+  return workerReadyPromise;
 };
 
 /**
@@ -414,8 +418,8 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
  * For internal use only.
  */
 var WorkerTransport = (function WorkerTransportClosure() {
-  function WorkerTransport(promise) {
-    this.workerReadyPromise = promise;
+  function WorkerTransport(workerInitializedPromise, workerReadyPromise) {
+    this.workerReadyPromise = workerReadyPromise;
     this.objs = new PDFObjects();
 
     this.pageCache = [];
@@ -459,6 +463,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
             globalScope.PDFJS.disableWorker = true;
             this.setupFakeWorker();
           }
+          workerInitializedPromise.resolve();
         }.bind(this));
 
         var testObj = new Uint8Array(1);
@@ -474,6 +479,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
     // Thus, we fallback to a faked worker.
     globalScope.PDFJS.disableWorker = true;
     this.setupFakeWorker();
+       workerInitializedPromise.resolve();
   }
   WorkerTransport.prototype = {
     destroy: function WorkerTransport_destroy() {