Skip to content

Commit

Permalink
Makes WorkerMessageHandler non-global.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Mar 3, 2016
1 parent f111142 commit 140164a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
5 changes: 0 additions & 5 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
35 changes: 21 additions & 14 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/frameworks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 2 additions & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@
exports.MissingPDFException = pdfjsLibs.pdfjsSharedUtil.MissingPDFException;
exports.UnexpectedResponseException =
pdfjsLibs.pdfjsSharedUtil.UnexpectedResponseException;
//#else
exports.WorkerMessageHandler = pdfjsLibs.pdfjsCoreWorker.WorkerMessageHandler;
//#endif
}));

0 comments on commit 140164a

Please sign in to comment.