1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 00:28:06 +02:00

Only one worker file, bundled in pdf.js

This commit is contained in:
Artur Adib 2011-10-25 10:16:20 -07:00
parent 8fbb05613e
commit 21753b9e89
8 changed files with 104 additions and 131 deletions

View file

@ -4,9 +4,6 @@
var ERRORS = 0, WARNINGS = 1, TODOS = 5;
var verbosity = WARNINGS;
// Set this to true if you want to use workers.
var useWorker = false;
//
// getPdf()
// Convenience function to perform binary Ajax GET
@ -463,7 +460,7 @@ var PDFDoc = (function() {
this.pageCache = [];
if (useWorker) {
var worker = new Worker('../worker/pdf_worker_loader.js');
var worker = new Worker('../build/pdf.js');
} else {
// If we don't use a worker, just post/sendMessage to the main thread.
var worker = {

View file

@ -1,18 +1,32 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
// // TODO: Global namespace
// var PDF = {};
var PDF = {};
(function(){
(function(globalScope){
// Use strict in our context only - users might not want it
'use strict';
// Set this to true if you want to use workers.
var useWorker = false;
var console;
// Files are inserted below - see Makefile
/* INSERT_POINT */
// Expose API in global object
window.PDFDoc = PDFDoc;
window.getPdf = getPdf;
// Worker-specific
if (typeof window !== 'undefined') {
console = window.console;
} else {
var consoleTimer = {};
console = workerConsole;
// Listen for messages from the main thread.
var handler = new MessageHandler('worker_processor', globalScope);
WorkerProcessorHandler.setup(handler);
}
})();
// Expose API in global object
PDF.PDFDoc = PDFDoc;
PDF.getPdf = getPdf;
})(this);

View file

@ -1,6 +1,49 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
function MessageHandler(name, comObj) {
this.name = name;
this.comObj = comObj;
var ah = this.actionHandler = {};
ah['console_log'] = [function(data) {
console.log.apply(console, data);
}];
ah['console_error'] = [function(data) {
console.error.apply(console, data);
}];
comObj.onmessage = function(event) {
var data = event.data;
if (data.action in ah) {
var action = ah[data.action];
action[0].call(action[1], data.data);
} else {
throw 'Unkown action from worker: ' + data.action;
}
};
}
MessageHandler.prototype = {
on: function(actionName, handler, scope) {
var ah = this.actionHandler;
if (ah[actionName]) {
throw "There is already an actionName called '" + actionName + "'";
}
ah[actionName] = [handler, scope];
},
send: function(actionName, data) {
this.comObj.postMessage({
action: actionName,
data: data
});
}
};
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
var WorkerProcessorHandler = {
setup: function(handler) {
var pdfDoc = null;
@ -97,3 +140,33 @@ var WorkerProcessorHandler = {
});
}
};
var workerConsole = {
log: function log() {
var args = Array.prototype.slice.call(arguments);
postMessage({
action: 'console_log',
data: args
});
},
error: function error() {
var args = Array.prototype.slice.call(arguments);
postMessage({
action: 'console_error',
data: args
});
},
time: function(name) {
consoleTimer[name] = Date.now();
},
timeEnd: function(name) {
var time = consoleTimer[name];
if (time == null) {
throw 'Unkown timer name ' + name;
}
this.log('Timer:', name, Date.now() - time);
}
};

View file

@ -1,34 +0,0 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
var consoleTimer = {};
var console = {
log: function log() {
var args = Array.prototype.slice.call(arguments);
postMessage({
action: 'console_log',
data: args
});
},
error: function error() {
var args = Array.prototype.slice.call(arguments);
postMessage({
action: 'console_error',
data: args
});
},
time: function(name) {
consoleTimer[name] = Date.now();
},
timeEnd: function(name) {
var time = consoleTimer[name];
if (time == null) {
throw 'Unkown timer name ' + name;
}
this.log('Timer:', name, Date.now() - time);
}
};

View file

@ -1,43 +0,0 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
function MessageHandler(name, comObj) {
this.name = name;
this.comObj = comObj;
var ah = this.actionHandler = {};
ah['console_log'] = [function(data) {
console.log.apply(console, data);
}];
ah['console_error'] = [function(data) {
console.error.apply(console, data);
}];
comObj.onmessage = function(event) {
var data = event.data;
if (data.action in ah) {
var action = ah[data.action];
action[0].call(action[1], data.data);
} else {
throw 'Unkown action from worker: ' + data.action;
}
};
}
MessageHandler.prototype = {
on: function(actionName, handler, scope) {
var ah = this.actionHandler;
if (ah[actionName]) {
throw "There is already an actionName called '" + actionName + "'";
}
ah[actionName] = [handler, scope];
},
send: function(actionName, data) {
this.comObj.postMessage({
action: actionName,
data: data
});
}
};

View file

@ -1,19 +0,0 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
importScripts('console.js');
importScripts('message_handler.js');
importScripts('../pdf.js');
importScripts('../fonts.js');
importScripts('../crypto.js');
importScripts('../glyphlist.js');
importScripts('../metrics.js');
importScripts('processor_handler.js');
// Listen for messages from the main thread.
var pdfDoc = null;
var handler = new MessageHandler('worker_processor', this);
WorkerProcessorHandler.setup(handler);