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.
|
* @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
|
||||||
*/
|
*/
|
||||||
PDFJS.getDocument = function getDocument(source) {
|
PDFJS.getDocument = function getDocument(source) {
|
||||||
var url, data, headers, password, parameters = {};
|
var url, data, headers, password, parameters = {}, workerInitializedPromise, workerReadyPromise, transport;
|
||||||
if (typeof source === 'string') {
|
if (typeof source === 'string') {
|
||||||
url = source;
|
url = source;
|
||||||
} else if (isArrayBuffer(source)) {
|
} else if (isArrayBuffer(source)) {
|
||||||
@ -37,8 +37,9 @@ PDFJS.getDocument = function getDocument(source) {
|
|||||||
'string or a parameter object');
|
'string or a parameter object');
|
||||||
}
|
}
|
||||||
|
|
||||||
var promise = new PDFJS.Promise();
|
workerInitializedPromise = new PDFJS.Promise();
|
||||||
var transport = new WorkerTransport(promise);
|
workerReadyPromise = new PDFJS.Promise();
|
||||||
|
transport = new WorkerTransport(workerInitializedPromise, workerReadyPromise);
|
||||||
if (data) {
|
if (data) {
|
||||||
// assuming the data is array, instantiating directly from it
|
// assuming the data is array, instantiating directly from it
|
||||||
transport.sendData(data, parameters);
|
transport.sendData(data, parameters);
|
||||||
@ -49,23 +50,26 @@ PDFJS.getDocument = function getDocument(source) {
|
|||||||
url: url,
|
url: url,
|
||||||
progress: function getPDFProgress(evt) {
|
progress: function getPDFProgress(evt) {
|
||||||
if (evt.lengthComputable)
|
if (evt.lengthComputable)
|
||||||
promise.progress({
|
workerReadyPromise.progress({
|
||||||
loaded: evt.loaded,
|
loaded: evt.loaded,
|
||||||
total: evt.total
|
total: evt.total
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
error: function getPDFError(e) {
|
error: function getPDFError(e) {
|
||||||
promise.reject('Unexpected server response of ' +
|
workerReadyPromise.reject('Unexpected server response of ' +
|
||||||
e.target.status + '.');
|
e.target.status + '.');
|
||||||
},
|
},
|
||||||
headers: headers
|
headers: headers
|
||||||
},
|
},
|
||||||
function getPDFLoad(data) {
|
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.
|
* For internal use only.
|
||||||
*/
|
*/
|
||||||
var WorkerTransport = (function WorkerTransportClosure() {
|
var WorkerTransport = (function WorkerTransportClosure() {
|
||||||
function WorkerTransport(promise) {
|
function WorkerTransport(workerInitializedPromise, workerReadyPromise) {
|
||||||
this.workerReadyPromise = promise;
|
this.workerReadyPromise = workerReadyPromise;
|
||||||
this.objs = new PDFObjects();
|
this.objs = new PDFObjects();
|
||||||
|
|
||||||
this.pageCache = [];
|
this.pageCache = [];
|
||||||
@ -459,6 +463,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||||||
globalScope.PDFJS.disableWorker = true;
|
globalScope.PDFJS.disableWorker = true;
|
||||||
this.setupFakeWorker();
|
this.setupFakeWorker();
|
||||||
}
|
}
|
||||||
|
workerInitializedPromise.resolve();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
var testObj = new Uint8Array(1);
|
var testObj = new Uint8Array(1);
|
||||||
@ -474,6 +479,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
|
|||||||
// Thus, we fallback to a faked worker.
|
// Thus, we fallback to a faked worker.
|
||||||
globalScope.PDFJS.disableWorker = true;
|
globalScope.PDFJS.disableWorker = true;
|
||||||
this.setupFakeWorker();
|
this.setupFakeWorker();
|
||||||
|
workerInitializedPromise.resolve();
|
||||||
}
|
}
|
||||||
WorkerTransport.prototype = {
|
WorkerTransport.prototype = {
|
||||||
destroy: function WorkerTransport_destroy() {
|
destroy: function WorkerTransport_destroy() {
|
||||||
|
Loading…
Reference in New Issue
Block a user