Remove useWorker
variable and turn worker support whenever this is possible.
This commit is contained in:
parent
43dbc3a84d
commit
493c25dcaa
65
src/core.js
65
src/core.js
@ -7,7 +7,6 @@ var globalScope = (typeof window === 'undefined') ? this : window;
|
|||||||
|
|
||||||
var ERRORS = 0, WARNINGS = 1, TODOS = 5;
|
var ERRORS = 0, WARNINGS = 1, TODOS = 5;
|
||||||
var verbosity = WARNINGS;
|
var verbosity = WARNINGS;
|
||||||
var useWorker = false;
|
|
||||||
|
|
||||||
// The global PDFJS object exposes the API
|
// The global PDFJS object exposes the API
|
||||||
// In production, it will be declared outside a global wrapper
|
// In production, it will be declared outside a global wrapper
|
||||||
@ -470,8 +469,14 @@ var PDFDoc = (function pdfDoc() {
|
|||||||
this.objs = new PDFObjects();
|
this.objs = new PDFObjects();
|
||||||
|
|
||||||
this.pageCache = [];
|
this.pageCache = [];
|
||||||
|
this.workerReadyPromise = new Promise('workerReady');
|
||||||
|
|
||||||
if (useWorker) {
|
// If worker support isn't disabled explicit and the browser has worker
|
||||||
|
// support, create a new web worker and test if it/the browser fullfills
|
||||||
|
// all requirements to run parts of pdf.js in a web worker.
|
||||||
|
// Right now, the requirement is, that an Uint8Array is still an Uint8Array
|
||||||
|
// as it arrives on the worker. Chrome added this with version 15.
|
||||||
|
if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
|
||||||
var workerSrc = PDFJS.workerSrc;
|
var workerSrc = PDFJS.workerSrc;
|
||||||
if (typeof workerSrc === 'undefined') {
|
if (typeof workerSrc === 'undefined') {
|
||||||
throw 'No PDFJS.workerSrc specified';
|
throw 'No PDFJS.workerSrc specified';
|
||||||
@ -479,26 +484,50 @@ var PDFDoc = (function pdfDoc() {
|
|||||||
|
|
||||||
var worker = new Worker(workerSrc);
|
var worker = new Worker(workerSrc);
|
||||||
|
|
||||||
|
var processorHandler = new MessageHandler('main', worker);
|
||||||
|
|
||||||
// Tell the worker the file it was created from.
|
// Tell the worker the file it was created from.
|
||||||
worker.postMessage({
|
processorHandler.send('workerSrc', workerSrc);
|
||||||
action: 'workerSrc',
|
|
||||||
data: workerSrc
|
processorHandler.on('test', function pdfDocTest(supportTypedArray) {
|
||||||
});
|
if (supportTypedArray) {
|
||||||
|
this.worker = worker;
|
||||||
|
this.setupProcessorHandler(processorHandler);
|
||||||
} else {
|
} else {
|
||||||
|
this.setupFakeWorker();
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
var testObj = new Uint8Array(1);
|
||||||
|
processorHandler.send('test', testObj);
|
||||||
|
} else {
|
||||||
|
this.setupFakeWorker();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.fontsLoading = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor.prototype = {
|
||||||
|
setupFakeWorker: function() {
|
||||||
// If we don't use a worker, just post/sendMessage to the main thread.
|
// If we don't use a worker, just post/sendMessage to the main thread.
|
||||||
var worker = {
|
var fakeWorker = {
|
||||||
postMessage: function pdfDocPostMessage(obj) {
|
postMessage: function pdfDocPostMessage(obj) {
|
||||||
worker.onmessage({data: obj});
|
fakeWorker.onmessage({data: obj});
|
||||||
},
|
},
|
||||||
terminate: function pdfDocTerminate() {}
|
terminate: function pdfDocTerminate() {}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
this.worker = worker;
|
|
||||||
|
|
||||||
this.fontsLoading = {};
|
var processorHandler = new MessageHandler('main', fakeWorker);
|
||||||
|
this.setupProcessorHandler(processorHandler);
|
||||||
|
|
||||||
var processorHandler = this.processorHandler =
|
// If the main thread is our worker, setup the handling for the messages
|
||||||
new MessageHandler('main', worker);
|
// the main thread sends to it self.
|
||||||
|
WorkerProcessorHandler.setup(processorHandler);
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
setupProcessorHandler: function(processorHandler) {
|
||||||
|
this.processorHandler = processorHandler;
|
||||||
|
|
||||||
processorHandler.on('page', function pdfDocPage(data) {
|
processorHandler.on('page', function pdfDocPage(data) {
|
||||||
var pageNum = data.pageNum;
|
var pageNum = data.pageNum;
|
||||||
@ -564,20 +593,12 @@ var PDFDoc = (function pdfDoc() {
|
|||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
if (!useWorker) {
|
|
||||||
// If the main thread is our worker, setup the handling for the messages
|
|
||||||
// the main thread sends to it self.
|
|
||||||
WorkerProcessorHandler.setup(processorHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.workerReadyPromise = new Promise('workerReady');
|
|
||||||
setTimeout(function pdfDocFontReadySetTimeout() {
|
setTimeout(function pdfDocFontReadySetTimeout() {
|
||||||
processorHandler.send('doc', this.data);
|
processorHandler.send('doc', this.data);
|
||||||
this.workerReadyPromise.resolve(true);
|
this.workerReadyPromise.resolve(true);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
},
|
||||||
|
|
||||||
constructor.prototype = {
|
|
||||||
get numPages() {
|
get numPages() {
|
||||||
return this.pdf.numPages;
|
return this.pdf.numPages;
|
||||||
},
|
},
|
||||||
|
@ -47,6 +47,10 @@ var WorkerProcessorHandler = {
|
|||||||
setup: function wphSetup(handler) {
|
setup: function wphSetup(handler) {
|
||||||
var pdfDoc = null;
|
var pdfDoc = null;
|
||||||
|
|
||||||
|
handler.on('test', function wphSetupTest(data) {
|
||||||
|
handler.send('test', data instanceof Uint8Array);
|
||||||
|
});
|
||||||
|
|
||||||
handler.on('workerSrc', function wphSetupWorkerSrc(data) {
|
handler.on('workerSrc', function wphSetupWorkerSrc(data) {
|
||||||
// In development, the `workerSrc` message is handled in the
|
// In development, the `workerSrc` message is handled in the
|
||||||
// `worker_loader.js` file. In production the workerProcessHandler is
|
// `worker_loader.js` file. In production the workerProcessHandler is
|
||||||
|
Loading…
x
Reference in New Issue
Block a user