From 591e754831a14ee23f7fb52def761b3ce041700e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 19 Dec 2019 17:36:36 +0100 Subject: [PATCH] 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. --- src/display/api.js | 65 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index e26265e72..e47ecb064 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -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;