Merge branch 'master' of github.com:kingsquare/pdf.js
This commit is contained in:
commit
4845bb586e
24
src/api.js
24
src/api.js
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user