Change to passing promises.
This commit is contained in:
parent
c2b91f1272
commit
d1f4e7c7d5
23
src/core.js
23
src/core.js
@ -515,8 +515,7 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
// Tell the worker the file it was created from.
|
// Tell the worker the file it was created from.
|
||||||
messageHandler.send('workerSrc', workerSrc);
|
messageHandler.send('workerSrc', workerSrc);
|
||||||
|
|
||||||
messageHandler.on('test', function pdfDocTest(message) {
|
messageHandler.on('test', function pdfDocTest(supportTypedArray) {
|
||||||
var supportTypedArray = message.data;
|
|
||||||
if (supportTypedArray) {
|
if (supportTypedArray) {
|
||||||
this.worker = worker;
|
this.worker = worker;
|
||||||
this.setupMessageHandler(messageHandler);
|
this.setupMessageHandler(messageHandler);
|
||||||
@ -554,8 +553,7 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
setupMessageHandler: function(messageHandler) {
|
setupMessageHandler: function(messageHandler) {
|
||||||
this.messageHandler = messageHandler;
|
this.messageHandler = messageHandler;
|
||||||
|
|
||||||
messageHandler.on('page', function pdfDocPage(message) {
|
messageHandler.on('page', function pdfDocPage(data) {
|
||||||
var data = message.data;
|
|
||||||
var pageNum = data.pageNum;
|
var pageNum = data.pageNum;
|
||||||
var page = this.pageCache[pageNum];
|
var page = this.pageCache[pageNum];
|
||||||
var depFonts = data.depFonts;
|
var depFonts = data.depFonts;
|
||||||
@ -563,8 +561,7 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
page.startRenderingFromIRQueue(data.IRQueue, depFonts);
|
page.startRenderingFromIRQueue(data.IRQueue, depFonts);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
messageHandler.on('obj', function pdfDocObj(message) {
|
messageHandler.on('obj', function pdfDocObj(data) {
|
||||||
var data = message.data;
|
|
||||||
var id = data[0];
|
var id = data[0];
|
||||||
var type = data[1];
|
var type = data[1];
|
||||||
|
|
||||||
@ -601,8 +598,7 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
messageHandler.on('font_ready', function pdfDocFontReady(message) {
|
messageHandler.on('font_ready', function pdfDocFontReady(data) {
|
||||||
var data = message.data;
|
|
||||||
var id = data[0];
|
var id = data[0];
|
||||||
var font = new FontShape(data[1]);
|
var font = new FontShape(data[1]);
|
||||||
|
|
||||||
@ -614,8 +610,7 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
messageHandler.on('page_error', function pdfDocError(message) {
|
messageHandler.on('page_error', function pdfDocError(data) {
|
||||||
var data = message.data;
|
|
||||||
var page = this.pageCache[data.pageNum];
|
var page = this.pageCache[data.pageNum];
|
||||||
if (page.callback)
|
if (page.callback)
|
||||||
page.callback(data.error);
|
page.callback(data.error);
|
||||||
@ -623,9 +618,9 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
throw data.error;
|
throw data.error;
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
messageHandler.on('jpeg_decode', function(message) {
|
messageHandler.on('jpeg_decode', function(data, promise) {
|
||||||
var imageData = message.data[0];
|
var imageData = data[0];
|
||||||
var components = message.data[1];
|
var components = data[1];
|
||||||
if (components != 3 && components != 1)
|
if (components != 3 && components != 1)
|
||||||
error('Only 3 component or 1 component can be returned');
|
error('Only 3 component or 1 component can be returned');
|
||||||
|
|
||||||
@ -652,7 +647,7 @@ var PDFDoc = (function PDFDocClosure() {
|
|||||||
buf[j] = data[i];
|
buf[j] = data[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
message.reply({ data: buf, width: width, height: height});
|
promise.resolve({ data: buf, width: width, height: height});
|
||||||
}).bind(this);
|
}).bind(this);
|
||||||
var src = 'data:image/jpeg;base64,' + window.btoa(imageData);
|
var src = 'data:image/jpeg;base64,' + window.btoa(imageData);
|
||||||
img.src = src;
|
img.src = src;
|
||||||
|
@ -3,41 +3,6 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/**
|
|
||||||
* A wrapper for data to facilitate adding functionality to messages.
|
|
||||||
*/
|
|
||||||
function Message(data) {
|
|
||||||
this.data = data;
|
|
||||||
this.allowsReply = false;
|
|
||||||
this.combObj;
|
|
||||||
this.id;
|
|
||||||
}
|
|
||||||
Message.prototype = {
|
|
||||||
/**
|
|
||||||
* Reply to the action handler that sent the message.
|
|
||||||
*/
|
|
||||||
reply: function messageReply(data) {
|
|
||||||
if (!this.allowsReply)
|
|
||||||
error('This message does not accept replies.');
|
|
||||||
|
|
||||||
this.combObj.postMessage({
|
|
||||||
isReply: true,
|
|
||||||
callbackId: this.id,
|
|
||||||
data: data
|
|
||||||
});
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* Setup the message to allow a reply.
|
|
||||||
* @param {Object} combObj The handler that has a postMessage function.
|
|
||||||
* @param {String} id The id to identify this message.
|
|
||||||
*/
|
|
||||||
setupReply: function setupReply(combObj, id) {
|
|
||||||
this.allowsReply = true;
|
|
||||||
this.combObj = combObj;
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function MessageHandler(name, comObj) {
|
function MessageHandler(name, comObj) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.comObj = comObj;
|
this.comObj = comObj;
|
||||||
@ -65,11 +30,19 @@ function MessageHandler(name, comObj) {
|
|||||||
}
|
}
|
||||||
} else if (data.action in ah) {
|
} else if (data.action in ah) {
|
||||||
var action = ah[data.action];
|
var action = ah[data.action];
|
||||||
var message = new Message(data.data);
|
if (data.callbackId) {
|
||||||
if (data.callbackId)
|
var promise = new Promise();
|
||||||
message.setupReply(comObj, data.callbackId);
|
promise.then(function(resolvedData) {
|
||||||
|
comObj.postMessage({
|
||||||
action[0].call(action[1], message);
|
isReply: true,
|
||||||
|
callbackId: data.callbackId,
|
||||||
|
data: resolvedData
|
||||||
|
});
|
||||||
|
});
|
||||||
|
action[0].call(action[1], data.data, promise);
|
||||||
|
} else {
|
||||||
|
action[0].call(action[1], data.data);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw 'Unkown action from worker: ' + data.action;
|
throw 'Unkown action from worker: ' + data.action;
|
||||||
}
|
}
|
||||||
@ -108,8 +81,7 @@ var WorkerMessageHandler = {
|
|||||||
setup: function wphSetup(handler) {
|
setup: function wphSetup(handler) {
|
||||||
var pdfDoc = null;
|
var pdfDoc = null;
|
||||||
|
|
||||||
handler.on('test', function wphSetupTest(message) {
|
handler.on('test', function wphSetupTest(data) {
|
||||||
var data = message.data;
|
|
||||||
handler.send('test', data instanceof Uint8Array);
|
handler.send('test', data instanceof Uint8Array);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -120,15 +92,13 @@ var WorkerMessageHandler = {
|
|||||||
// undefined action `workerSrc`.
|
// undefined action `workerSrc`.
|
||||||
});
|
});
|
||||||
|
|
||||||
handler.on('doc', function wphSetupDoc(message) {
|
handler.on('doc', function wphSetupDoc(data) {
|
||||||
var data = message.data;
|
|
||||||
// Create only the model of the PDFDoc, which is enough for
|
// Create only the model of the PDFDoc, which is enough for
|
||||||
// processing the content of the pdf.
|
// processing the content of the pdf.
|
||||||
pdfDoc = new PDFDocModel(new Stream(data));
|
pdfDoc = new PDFDocModel(new Stream(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
handler.on('page_request', function wphSetupPageRequest(message) {
|
handler.on('page_request', function wphSetupPageRequest(pageNum) {
|
||||||
var pageNum = message.data;
|
|
||||||
pageNum = parseInt(pageNum);
|
pageNum = parseInt(pageNum);
|
||||||
|
|
||||||
|
|
||||||
@ -177,8 +147,7 @@ var WorkerMessageHandler = {
|
|||||||
});
|
});
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
handler.on('font', function wphSetupFont(message) {
|
handler.on('font', function wphSetupFont(data) {
|
||||||
var data = message.data;
|
|
||||||
var objId = data[0];
|
var objId = data[0];
|
||||||
var name = data[1];
|
var name = data[1];
|
||||||
var file = data[2];
|
var file = data[2];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user