From 140164a0279e68a6fc166b7bb6dba635dd16edc8 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Wed, 2 Mar 2016 19:16:38 -0600 Subject: [PATCH] Makes WorkerMessageHandler non-global. --- src/core/worker.js | 5 ----- src/display/api.js | 35 +++++++++++++++++++++-------------- src/frameworks.js | 6 +++--- src/pdf.js | 2 ++ 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index cdf804b3e93fd2..0f22e85ffbdd70 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -989,11 +989,6 @@ function initializeWorker() { handler.send('ready', null); } -if (globalScope.PDFJS) { - // TODO properly handle WorkerMessageHandler as a module export at api.js. - globalScope.PDFJS.WorkerMessageHandler = WorkerMessageHandler; -} - // Worker thread (and not node.js)? if (typeof window === 'undefined' && !(typeof module !== 'undefined' && module.require)) { diff --git a/src/display/api.js b/src/display/api.js index 97da03ad7d3a16..2f102c738be870 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1214,38 +1214,45 @@ var PDFWorker = (function PDFWorkerClosure() { error('No PDFJS.workerSrc specified'); } + var fakeWorkerFilesLoadedCapability; + // Loads worker code into main thread. function setupFakeWorkerGlobal() { - if (!PDFJS.fakeWorkerFilesLoadedCapability) { - PDFJS.fakeWorkerFilesLoadedCapability = createPromiseCapability(); + var WorkerMessageHandler; + if (!fakeWorkerFilesLoadedCapability) { + fakeWorkerFilesLoadedCapability = createPromiseCapability(); // 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. //#if !PRODUCTION if (typeof amdRequire === 'function') { - amdRequire(['pdfjs/core/network', 'pdfjs/core/worker'], function () { - PDFJS.fakeWorkerFilesLoadedCapability.resolve(); + amdRequire(['pdfjs/core/network', 'pdfjs/core/worker'], + function (network, worker) { + WorkerMessageHandler = worker.WorkerMessageHandler; + fakeWorkerFilesLoadedCapability.resolve(WorkerMessageHandler); }); } else if (typeof require === 'function') { - require('../core/worker.js'); - PDFJS.fakeWorkerFilesLoadedCapability.resolve(); + var worker = require('../core/worker.js'); + WorkerMessageHandler = worker.WorkerMessageHandler; + fakeWorkerFilesLoadedCapability.resolve(WorkerMessageHandler); } else { throw new Error('AMD or CommonJS must be used to load fake worker.'); } //#endif //#if PRODUCTION && SINGLE_FILE -// PDFJS.fakeWorkerFilesLoadedCapability.resolve(); +// WorkerMessageHandler = pdfjsLibs.pdfjsCoreWorker.WorkerMessageHandler; +// fakeWorkerFilesLoadedCapability.resolve(WorkerMessageHandler); //#endif //#if PRODUCTION && !SINGLE_FILE // var loader = fakeWorkerFilesLoader || function (callback) { -// Util.loadScript(getWorkerSrc(), callback); +// Util.loadScript(getWorkerSrc(), function () { +// callback(window.pdfjsDistBuildPdfWorker.WorkerMessageHandler); +// }); // }; -// loader(function () { -// PDFJS.fakeWorkerFilesLoadedCapability.resolve(); -// }); +// loader(fakeWorkerFilesLoadedCapability.resolve); //#endif } - return PDFJS.fakeWorkerFilesLoadedCapability.promise; + return fakeWorkerFilesLoadedCapability.promise; } function createCDNWrapper(url) { @@ -1384,7 +1391,7 @@ var PDFWorker = (function PDFWorkerClosure() { globalScope.PDFJS.disableWorker = true; } - setupFakeWorkerGlobal().then(function () { + setupFakeWorkerGlobal().then(function (WorkerMessageHandler) { if (this.destroyed) { this._readyCapability.reject(new Error('Worker was destroyed')); return; @@ -1416,7 +1423,7 @@ var PDFWorker = (function PDFWorkerClosure() { // If the main thread is our worker, setup the handling for the // messages -- the main thread sends to it self. var workerHandler = new MessageHandler(id + '_worker', id, port); - PDFJS.WorkerMessageHandler.setup(workerHandler, port); + WorkerMessageHandler.setup(workerHandler, port); var messageHandler = new MessageHandler(id, id + '_worker', port); this._messageHandler = messageHandler; diff --git a/src/frameworks.js b/src/frameworks.js index 95b4d36dacde6b..01493218f7fa72 100644 --- a/src/frameworks.js +++ b/src/frameworks.js @@ -37,11 +37,11 @@ if (typeof requirejs !== 'undefined' && requirejs.toUrl) { } var fakeWorkerFilesLoader = useRequireEnsure ? (function (callback) { require.ensure([], function () { - require('./pdf.worker.js'); - callback(); + var worker = require('./pdf.worker.js'); + callback(worker.WorkerMessageHandler); }); }) : (typeof requirejs !== 'undefined') ? (function (callback) { requirejs(['pdfjs-dist/build/pdf.worker'], function (worker) { - callback(); + callback(worker.WorkerMessageHandler); }); }) : null; diff --git a/src/pdf.js b/src/pdf.js index 49da559b0738d9..6a5c880c99e438 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -57,5 +57,7 @@ exports.MissingPDFException = pdfjsLibs.pdfjsSharedUtil.MissingPDFException; exports.UnexpectedResponseException = pdfjsLibs.pdfjsSharedUtil.UnexpectedResponseException; +//#else + exports.WorkerMessageHandler = pdfjsLibs.pdfjsCoreWorker.WorkerMessageHandler; //#endif }));