From 1e6d1f99221ff180ee8947dbb198f2119418b17d Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Fri, 28 Oct 2011 14:32:36 +0200 Subject: [PATCH] Make worker support work again after file split. Add PDFJS_WORKER_DIR/PDFJS_WORKER_FILE to specify where to load files if worker support is enabled --- examples/helloworld/hello.js | 2 +- examples/helloworld/index.html | 43 +++++++++++++++------------ src/core.js | 21 ++++++++++++- src/pdf.js | 1 - src/worker.js | 5 ++-- src/worker_loader.js | 54 ++++++++++++++++++++++------------ web/viewer-snippet.html | 5 ++++ web/viewer.html | 6 ++-- 8 files changed, 91 insertions(+), 46 deletions(-) diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index 45e61eb6f..c97b53c66 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -7,7 +7,7 @@ 'use strict'; -PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) { +getPdf('helloworld.pdf', function getPdfHelloWorld(data) { // // Instantiate PDFDoc with PDF data // diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html index b10e9eaeb..0fa711fe4 100644 --- a/examples/helloworld/index.html +++ b/examples/helloworld/index.html @@ -3,27 +3,32 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - + diff --git a/src/core.js b/src/core.js index e7241acfa..8d3d6d434 100644 --- a/src/core.js +++ b/src/core.js @@ -471,7 +471,26 @@ var PDFDoc = (function() { this.pageCache = []; if (useWorker) { - var worker = new Worker('../src/worker_loader.js'); + var worker; + if (typeof PDFJS_WORKER_DIR !== 'undefined') { + // If `PDFJS_WORKER_DIR` is specified, we assume the pdf.js files + // located all in that directory. Create a new worker and tell him + // the directory, such that he can load the scripts from there. + worker = new Worker(PDFJS_WORKER_DIR + 'worker_loader.js'); + console.log('main: post dir'); + + worker.postMessage(PDFJS_WORKER_DIR); + } else if (typeof PDFJS_WORKER_FILE !== 'undefined') { + // If we build the worker using a worker file, then we assume, that + // everything the worker needs is already included in that file. + // Therefore the worker doesn't have to call `importScripts` to load + // all the single files and therefore it's not necessary to tell the + // worker a directory to laod the js files from. + // (Which is different from the PDFJS_WORKER_DIR case above.) + worker = new Worker(PDFJS_WORKER_FILE); + } else { + throw 'No worker file or directory specified.'; + } } else { // If we don't use a worker, just post/sendMessage to the main thread. var worker = { diff --git a/src/pdf.js b/src/pdf.js index 34e163967..2ecd2fddc 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -4,7 +4,6 @@ var PDFJS = {}; (function pdfjsWrapper() { - // Use strict in our context only - users might not want it 'use strict'; diff --git a/src/worker.js b/src/worker.js index a83f31668..5545fc459 100644 --- a/src/worker.js +++ b/src/worker.js @@ -174,10 +174,9 @@ var workerConsole = { // Worker thread? if (typeof window === 'undefined') { - globalScope.console = workerConsole; + this.console = workerConsole; - // Listen for messages from the main thread. - var handler = new MessageHandler('worker_processor', globalScope); + var handler = new MessageHandler('worker_processor', this); WorkerProcessorHandler.setup(handler); } diff --git a/src/worker_loader.js b/src/worker_loader.js index fb37ca9c4..69f7d55ba 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -3,22 +3,40 @@ 'use strict'; -importScripts('../src/core.js'); -importScripts('../src/util.js'); -importScripts('../src/canvas.js'); -importScripts('../src/obj.js'); -importScripts('../src/function.js'); -importScripts('../src/charsets.js'); -importScripts('../src/cidmaps.js'); -importScripts('../src/colorspace.js'); -importScripts('../src/crypto.js'); -importScripts('../src/evaluator.js'); -importScripts('../src/fonts.js'); -importScripts('../src/glyphlist.js'); -importScripts('../src/image.js'); -importScripts('../src/metrics.js'); -importScripts('../src/parser.js'); -importScripts('../src/pattern.js'); -importScripts('../src/stream.js'); -importScripts('../src/worker.js'); +this.onmessage = function(evt) { + // Reset the `onmessage` function as it was only set to call + // this function the first time a message is passed to the worker + // but shouldn't get called anytime afterwards. + delete this.onmessage; + // Directory the include files are contained is send as the + // first message to the worker. + var dir = evt.data; + + // List of files to include; + var files = [ + 'core.js', + 'util.js', + 'canvas.js', + 'obj.js', + 'function.js', + 'charsets.js', + 'cidmaps.js', + 'colorspace.js', + 'crypto.js', + 'evaluator.js', + 'fonts.js', + 'glyphlist.js', + 'image.js', + 'metrics.js', + 'parser.js', + 'pattern.js', + 'stream.js', + 'worker.js' + ]; + + // Load all the files. + for (var i = 0; i < files.length; i++) { + importScripts(dir + files[i]); + } +}.bind(this); diff --git a/web/viewer-snippet.html b/web/viewer-snippet.html index c99897a44..4d2a4f7b7 100644 --- a/web/viewer-snippet.html +++ b/web/viewer-snippet.html @@ -1,2 +1,7 @@ + diff --git a/web/viewer.html b/web/viewer.html index f7a5378ed..4505331d6 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -3,9 +3,9 @@ Simple pdf.js page viewer - + - + @@ -114,7 +114,7 @@ - +
Loading... 0%