Change postMessage to send only one object that holds the action and data.

This commit is contained in:
Julian Viereck 2011-06-23 13:25:59 +02:00
parent a3d815074d
commit da7f555fd7
4 changed files with 70 additions and 101 deletions

View File

@ -11,10 +11,9 @@ var JpegStreamProxy = (function() {
this.dict = dict; this.dict = dict;
// Tell the main thread to create an image. // Tell the main thread to create an image.
postMessage("jpeg_stream");
postMessage({ postMessage({
id: this.id, action: jpeg_stream,
str: bytesToString(bytes) data: bytesToString(bytes)
}); });
} }
@ -235,12 +234,14 @@ function CanvasProxy(width, height) {
* resets the cmdQueue. * resets the cmdQueue.
*/ */
CanvasProxy.prototype.flush = function() { CanvasProxy.prototype.flush = function() {
postMessage("canvas_proxy_cmd_queue");
postMessage({ postMessage({
action: "canvas_proxy_cmd_queue",
data: {
id: this.id, id: this.id,
cmdQueue: this.cmdQueue, cmdQueue: this.cmdQueue,
width: this.width, width: this.width,
height: this.height height: this.height
}
}); });
this.cmdQueue.length = 0; this.cmdQueue.length = 0;
} }

View File

@ -768,12 +768,14 @@ var Font = (function () {
// Insert the font-face css on the page. In a web worker, this needs to // Insert the font-face css on the page. In a web worker, this needs to
// be forwareded on the main thread. // be forwareded on the main thread.
if (typeof window == "undefined") { if (typeof window == "undefined") {
postMessage("font"); postMessage({
postMessage(JSON.stringify({ action: "font",
str: str, data: {
raw: str,
fontName: fontName, fontName: fontName,
mimetype: this.mimetype mimetype: this.mimetype
})); }
});
} else { } else {
var base64 = window.btoa(str); var base64 = window.btoa(str);

View File

@ -15,8 +15,10 @@ function toc(msg) {
function log() { function log() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
postMessage("log"); postMessage({
postMessage(JSON.stringify(args)) action: "log",
args: args
});
} }
var console = { var console = {
@ -42,8 +44,10 @@ onmessage = function(event) {
// If there is no pdfDocument yet, then the sent data is the PDFDocument. // If there is no pdfDocument yet, then the sent data is the PDFDocument.
if (!pdfDocument) { if (!pdfDocument) {
pdfDocument = new PDFDoc(new Stream(data)); pdfDocument = new PDFDoc(new Stream(data));
postMessage("pdf_num_page"); postMessage({
postMessage(pdfDocument.numPages) action: "pdf_num_pages",
data: pdfDocument.numPages
});
return; return;
} }
// User requested to render a certain page. // User requested to render a certain page.

View File

@ -15,7 +15,7 @@ function WorkerPDFDoc(canvas) {
this.ctx = canvas.getContext("2d"); this.ctx = canvas.getContext("2d");
this.canvas = canvas; this.canvas = canvas;
this.worker = new Worker('worker.js'); this.worker = new Worker('pdf_worker.js');
this.numPage = 1; this.numPage = 1;
this.numPages = null; this.numPages = null;
@ -147,68 +147,22 @@ function WorkerPDFDoc(canvas) {
} }
/** /**
* onMessage state machine. * Functions to handle data sent by the WebWorker.
*/ */
const WAIT = 0; var actionHandler = {
const CANVAS_PROXY_CMD_QUEUE = 1; "log": function(data) {
const LOG = 2; console.log.apply(console, data);
const FONT = 3; },
const PDF_NUM_PAGE = 4;
const JPEG_STREAM = 5;
var onMessageState = WAIT; "pdf_num_pages": function(data) {
this.worker.onmessage = function(event) {
var data = event.data;
// console.log("onMessageRaw", data);
switch (onMessageState) {
case WAIT:
if (typeof data != "string") {
throw "expecting to get an string";
}
switch (data) {
case "pdf_num_page":
onMessageState = PDF_NUM_PAGE;
return;
case "log":
onMessageState = LOG;
return;
case "canvas_proxy_cmd_queue":
onMessageState = CANVAS_PROXY_CMD_QUEUE;
return;
case "font":
onMessageState = FONT;
return;
case "jpeg_stream":
onMessageState = JPEG_STREAM;
return;
default:
throw "unkown state: " + data
}
break;
case JPEG_STREAM:
var img = new Image();
img.src = "data:image/jpeg;base64," + window.btoa(data.str);
imagesList[data.id] = img;
console.log("got image", data.id)
break;
case PDF_NUM_PAGE:
this.numPages = parseInt(data); this.numPages = parseInt(data);
if (this.loadCallback) { if (this.loadCallback) {
this.loadCallback(); this.loadCallback();
} }
onMessageState = WAIT; },
break;
case FONT: "font": function(data) {
data = JSON.parse(data); var base64 = window.btoa(data.raw);
var base64 = window.btoa(data.str);
// Add the @font-face rule to the document // Add the @font-face rule to the document
var url = "url(data:" + data.mimetype + ";base64," + base64 + ");"; var url = "url(data:" + data.mimetype + ";base64," + base64 + ");";
@ -221,16 +175,16 @@ function WorkerPDFDoc(canvas) {
// add a div on the page using the loaded font. // add a div on the page using the loaded font.
var div = document.createElement("div"); var div = document.createElement("div");
document.getElementById("fonts").innerHTML += "<div style='font-family:" + data.fontName + "'>j</div>"; document.getElementById("fonts").innerHTML += "<div style='font-family:" + data.fontName + "'>j</div>";
},
onMessageState = WAIT; "jpeg_stream": function(data) {
break; var img = new Image();
img.src = "data:image/jpeg;base64," + window.btoa(data);
imagesList[data.id] = img;
console.log("got image", data.id)
},
case LOG: "canvas_proxy_cmd_queue": function(data) {
console.log.apply(console, JSON.parse(data));
onMessageState = WAIT;
break;
case CANVAS_PROXY_CMD_QUEUE:
var id = data.id; var id = data.id;
var cmdQueue = data.cmdQueue; var cmdQueue = data.cmdQueue;
@ -250,10 +204,18 @@ function WorkerPDFDoc(canvas) {
renderProxyCanvas(canvasList[id], cmdQueue); renderProxyCanvas(canvasList[id], cmdQueue);
if (id == 0) toc("canvas rendering") if (id == 0) toc("canvas rendering")
}, 0); }, 0);
onMessageState = WAIT;
break;
} }
}.bind(this); }
// List to the WebWorker for data and call actionHandler on it.
this.worker.onmessage = function(event) {
var data = event.data;
if (data.action in actionHandler) {
actionHandler[data.action].call(this, data.data);
} else {
throw "Unkown action from worker: " + data.action;
}
}
} }
WorkerPDFDoc.prototype.open = function(url, callback) { WorkerPDFDoc.prototype.open = function(url, callback) {