1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-21 23:58:07 +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

@ -22,8 +22,8 @@ import {
} from './ui_utils';
import {
build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException,
MissingPDFException, OPS, PDFJS, shadow, UnexpectedResponseException,
UNSUPPORTED_FEATURES, version
MissingPDFException, OPS, PDFJS, PDFWorker, shadow,
UnexpectedResponseException, UNSUPPORTED_FEATURES, version
} from 'pdfjs-lib';
import { CursorTool, PDFCursorTools } from './pdf_cursor_tools';
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
@ -285,8 +285,9 @@ let PDFViewerApplication = {
let hash = document.location.hash.substring(1);
let hashParams = parseQueryString(hash);
if ('disableworker' in hashParams) {
PDFJS.disableWorker = (hashParams['disableworker'] === 'true');
if ('disableworker' in hashParams &&
hashParams['disableworker'] === 'true') {
waitOn.push(loadFakeWorker());
}
if ('disablerange' in hashParams) {
PDFJS.disableRange = (hashParams['disablerange'] === 'true');
@ -1512,6 +1513,35 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
};
}
function loadFakeWorker() {
return new Promise(function(resolve, reject) {
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
if (typeof SystemJS === 'object') {
SystemJS.import('pdfjs/core/worker').then((worker) => {
window.pdfjsNonProductionPdfWorker = worker;
resolve();
});
} else if (typeof require === 'function') {
window.pdfjsNonProductionPdfWorker = require('../src/core/worker.js');
resolve();
} else {
reject(new Error(
'SystemJS or CommonJS must be used to load fake worker.'));
}
} else {
let script = document.createElement('script');
script.src = PDFWorker.getWorkerSrc();
script.onload = function() {
resolve();
};
script.onerror = function() {
reject(new Error(`Cannot load fake worker at: ${script.src}`));
};
(document.head || document.documentElement).appendChild(script);
}
});
}
function loadAndEnablePDFBug(enabledTabs) {
return new Promise(function (resolve, reject) {
let appConfig = PDFViewerApplication.appConfig;