1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

[api-major] Remove the PDFJS.disableWorker option

Despite this patch removing the `disableWorker` option itself, please note that we'll still fallback to loading the worker file(s) on the main-thread when running in environments without proper Web Worker support.

Furthermore it's still possible, even with this patch, to force the use of fake workers by manually loading the necessary file using a `<script>` tag on the main-thread.[1]
That way, the functionality of the now removed `SINGLE_FILE` build target and the resulting `build/pdf.combined.js` file can still be achieved simply by adding e.g. `<script src="build/pdf.worker.js"></script>` to the HTML (obviously with the path adjusted as needed).

Finally note that the `disableWorker` option is a performance footgun, and unfortunately many existing third-party examples actually use it without providing any sort of warning/justification.

---

[1] This approach is used in the default viewer, since certain kind of debugging may be easier if the code is running directly on the main-thread.
This commit is contained in:
Jonas Jenwald 2018-01-19 18:16:17 +01:00
parent a5aaf62754
commit 56a8c934dd
11 changed files with 64 additions and 57 deletions

View file

@ -1216,6 +1216,19 @@ var PDFWorker = (function PDFWorkerClosure() {
throw new Error('No PDFJS.workerSrc specified');
}
function getMainThreadWorkerMessageHandler() {
if (typeof window === 'undefined') {
return null;
}
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
return (window.pdfjsNonProductionPdfWorker &&
window.pdfjsNonProductionPdfWorker.WorkerMessageHandler);
}
// PRODUCTION
return (window.pdfjsDistBuildPdfWorker &&
window.pdfjsDistBuildPdfWorker.WorkerMessageHandler);
}
let fakeWorkerFilesLoadedCapability;
// Loads worker code into main thread.
@ -1225,6 +1238,13 @@ var PDFWorker = (function PDFWorkerClosure() {
return fakeWorkerFilesLoadedCapability.promise;
}
fakeWorkerFilesLoadedCapability = createPromiseCapability();
let mainWorkerMessageHandler = getMainThreadWorkerMessageHandler();
if (mainWorkerMessageHandler) {
// The worker was already loaded using a `<script>` tag.
fakeWorkerFilesLoadedCapability.resolve(mainWorkerMessageHandler);
return fakeWorkerFilesLoadedCapability.promise;
}
// In the developer build load worker_loader which in turn loads all the
// other files and resolves the promise. In production only the
// pdf.worker.js file is needed.
@ -1316,7 +1336,7 @@ var PDFWorker = (function PDFWorkerClosure() {
// Right now, the requirement is, that an Uint8Array is still an
// Uint8Array as it arrives on the worker. (Chrome added this with v.15.)
if (typeof Worker !== 'undefined' && !isWorkerDisabled &&
!getDefaultSetting('disableWorker')) {
!getMainThreadWorkerMessageHandler()) {
var workerSrc = getWorkerSrc();
try {
@ -1427,7 +1447,7 @@ var PDFWorker = (function PDFWorkerClosure() {
},
_setupFakeWorker: function PDFWorker_setupFakeWorker() {
if (!isWorkerDisabled && !getDefaultSetting('disableWorker')) {
if (!isWorkerDisabled) {
warn('Setting up fake worker.');
isWorkerDisabled = true;
}

View file

@ -361,8 +361,6 @@ function getDefaultSetting(id) {
return globalSettings ? globalSettings.workerPort : null;
case 'workerSrc':
return globalSettings ? globalSettings.workerSrc : null;
case 'disableWorker':
return globalSettings ? globalSettings.disableWorker : false;
case 'maxImageSize':
return globalSettings ? globalSettings.maxImageSize : -1;
case 'imageResourcesPath':

View file

@ -128,15 +128,6 @@ PDFJS.disableFontFace = (PDFJS.disableFontFace === undefined ?
PDFJS.imageResourcesPath = (PDFJS.imageResourcesPath === undefined ?
'' : PDFJS.imageResourcesPath);
/**
* Disable the web worker and run all code on the main thread. This will
* happen automatically if the browser doesn't support workers or sending
* typed arrays to workers.
* @var {boolean}
*/
PDFJS.disableWorker = (PDFJS.disableWorker === undefined ?
false : PDFJS.disableWorker);
/**
* Path and filename of the worker file. Required when the worker is enabled
* in development mode. If unspecified in the production build, the worker
@ -148,8 +139,7 @@ PDFJS.disableWorker = (PDFJS.disableWorker === undefined ?
PDFJS.workerSrc = (PDFJS.workerSrc === undefined ? null : PDFJS.workerSrc);
/**
* Defines global port for worker process. Overrides workerSrc and
* disableWorker setting.
* Defines global port for worker process. Overrides `workerSrc` setting.
*/
PDFJS.workerPort = (PDFJS.workerPort === undefined ? null : PDFJS.workerPort);