Move the fake worker loader code into the PDFWorkerClosure

Given that this code isn't needed "globally" in the file, it seems reasonable to move it to where it's actually used instead.
This commit is contained in:
Jonas Jenwald 2019-12-19 17:36:36 +01:00
parent aab0f91740
commit 591e754831

View File

@ -43,38 +43,6 @@ import { WebGLContext } from './webgl';
const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536
const RENDERING_CANCELLED_TIMEOUT = 100; // ms
let isWorkerDisabled = false;
let fallbackWorkerSrc;
let fakeWorkerFilesLoader = null;
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
if (isNodeJS && typeof __non_webpack_require__ === 'function') {
// Workers aren't supported in Node.js, force-disabling them there.
isWorkerDisabled = true;
fakeWorkerFilesLoader = function() {
return new Promise(function(resolve, reject) {
try {
let worker;
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) {
worker = __non_webpack_require__('../pdf.worker.js');
} else {
worker = __non_webpack_require__('./pdf.worker.js');
}
resolve(worker.WorkerMessageHandler);
} catch (ex) {
reject(ex);
}
});
};
} else if (typeof document === 'object' && 'currentScript' in document) {
const pdfjsFilePath = document.currentScript && document.currentScript.src;
if (pdfjsFilePath) {
fallbackWorkerSrc =
pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2');
}
}
}
/**
* @typedef {function} IPDFStreamFactory
@ -1498,9 +1466,42 @@ class LoopbackPort {
const PDFWorker = (function PDFWorkerClosure() {
const pdfWorkerPorts = new WeakMap();
let isWorkerDisabled = false;
let fallbackWorkerSrc;
let nextFakeWorkerId = 0;
let fakeWorkerFilesLoadedCapability;
let fakeWorkerFilesLoader = null;
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
if (isNodeJS && typeof __non_webpack_require__ === 'function') {
// Workers aren't supported in Node.js, force-disabling them there.
isWorkerDisabled = true;
fakeWorkerFilesLoader = function() {
return new Promise(function(resolve, reject) {
try {
let worker;
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) {
worker = __non_webpack_require__('../pdf.worker.js');
} else {
worker = __non_webpack_require__('./pdf.worker.js');
}
resolve(worker.WorkerMessageHandler);
} catch (ex) {
reject(ex);
}
});
};
} else if (typeof document === 'object' && 'currentScript' in document) {
const pdfjsFilePath = document.currentScript &&
document.currentScript.src;
if (pdfjsFilePath) {
fallbackWorkerSrc =
pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2');
}
}
}
function getWorkerSrc() {
if (GlobalWorkerOptions.workerSrc) {
return GlobalWorkerOptions.workerSrc;