Commit d9f6a733 authored by lukas.niegsch's avatar lukas.niegsch

handles event messages via devtools

parent bb01303d
......@@ -36,34 +36,9 @@
var sessionId;
var base64data;
/**
* Steps:
*
* // new page setup
* Target.createTarget();
* Target.attachToTarget();
* Target.activateTarget();
*
* // enable events
* Network.enable();
* Page.enable();
*
* // change content
* Page.frameAttached();
* Page.setDocumentContent();
* Page.loadEventFired(); // Is this event fired once loading starts or stops?
*
* // convert html to pdf
* Page.printToPDF();
*
* // cleanup
* Target.detachFromTarget();
* Target.closeTarget();
*
* todo: detaching should reset session properly
* todo: implement receiving events to avoid waiting
* todo: close target stops receiving messages ...
*/
return gadget.getDeclaredGadget("devtools")
.push(function (subgadget) {
return subgadget.getDevtools();
......@@ -90,16 +65,13 @@
return devtools.Page.getFrameTree();
})
.push(function (result) {
console.log(result);
var frameId = result.frameTree.frame.id;
return devtools.Page.setDocumentContent({frameId: frameId, html: html});
})
.push(function () {
console.log("begin delay");
return RSVP.delay(3 /* seconds*/ * 1000);
return devtools.Page.loadEventFired();
})
.push(function () {
console.log("end delay");
return devtools.Page.printToPDF(optionsForPrintToPdf);
})
.push(function (result) {
......
......@@ -70,10 +70,9 @@ var BROWSER_PASSWORD = "ignored";
params: params,
}
var message = JSON.stringify(command);
console.log(message);
gadget.state.websocket.send(message);
var handler = {
sessionId,
id: gadget.state.nextIndex,
result: resolve,
error: reject
......@@ -96,10 +95,14 @@ var BROWSER_PASSWORD = "ignored";
function addEventAPI (object, domain, method) {
var result = (gadget, params) => {
var callback = (resolve, reject) => {
// todo: implement this method
resolve(null);
var sessionId = gadget.state.sessionId;
var handler = {
sessionId,
method: `${domain.domain}.${method.name}`,
result: resolve,
error: reject
}
gadget.state.handlers.add(handler);
}
return RSVP.Queue().push(function () {
return RSVP.Promise(callback);
......@@ -167,7 +170,6 @@ var BROWSER_PASSWORD = "ignored";
gadget.changeState({nextIndex: gadget.state.nextIndex + 1});
}
websocket.onerror = (error) => {
console.log(error);
websocket.close();
updateWebsocket(gadget, websocketUrl);
}
......@@ -177,6 +179,45 @@ var BROWSER_PASSWORD = "ignored";
return RSVP.Promise(callback);
});
}
/**
* Find the corresponding handler for the message and resolves it.
*
* @param {object} message The message that contains the result data.
* @param {object} gadget The gadget that contains all the handlers.
*/
function handleMethodMessage(gadget, message)
{
for (let it = gadget.state.handlers.values(), handler = null; handler = it.next().value;) {
if (handler.id != message.id) {
continue;
}
gadget.state.handlers.delete(handler);
if ('error' in message) {
handler.error(message.error);
} else {
handler.result(message.result)
}
}
}
/**
* Find the corresponding handler for the event and resolves it.
*
* @param {object} message The message that contains the event data.
* @param {object} gadget The gadget that contains all the handlers.
*/
function handleEventMessage(gadget, message)
{
for (let it = gadget.state.handlers.values(), handler = null; handler = it.next().value;) {
if (handler.method != message.method) {
continue;
}
gadget.state.handlers.delete(handler);
handler.result(message.params);
}
}
rJS(window)
.setState({
......@@ -220,31 +261,18 @@ var BROWSER_PASSWORD = "ignored";
return gadget.state.devtools;
})
})
.onStateChange(function (modifications) {
.onStateChange(function () {
var gadget = this;
/*
* This is O(n * m) with n = messages.size and m = handlers.size.
*
* I think it is fine since new messages should complete all handlers,
* which means m stays very small. We also have messages from events,
* but there are only finite amount, so n should stay small aswell.
*
* This can be implemented much faster if performance becomes an issue.
*/
for (let i = gadget.state.handlers.values(), handler = null; handler = i.next().value;) {
for (let j = gadget.state.messages.values(), message = null; message = j.next().value;) {
if (handler.id != message.id) {
continue;
}
gadget.state.handlers.delete(handler);
gadget.state.messages.delete(message);
if ('error' in message) {
handler.error(message.error);
} else {
handler.result(message.result)
}
for (let it = gadget.state.messages.values(), message = null; message = it.next().value;) {
console.log(message);
if (message.hasOwnProperty("id")) {
handleMethodMessage(gadget, message);
}
if (message.hasOwnProperty("method")) {
handleEventMessage(gadget, message);
}
gadget.state.messages.delete(message);
}
})
}(window, rJS, RSVP));
\ No newline at end of file
......@@ -43,7 +43,6 @@
return converter.convert(html);
})
.push(function (base64data) {
console.log(base64data);
viewer.src = "data:application/pdf;base64," + base64data;
})
.push(function () {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment