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

fixed attach/detach + instant event loading problems

parent d9f6a733
...@@ -35,10 +35,6 @@ ...@@ -35,10 +35,6 @@
var targetId; var targetId;
var sessionId; var sessionId;
var base64data; var base64data;
/**
* todo: detaching should reset session properly
* todo: close target stops receiving messages ...
*/
return gadget.getDeclaredGadget("devtools") return gadget.getDeclaredGadget("devtools")
.push(function (subgadget) { .push(function (subgadget) {
return subgadget.getDevtools(); return subgadget.getDevtools();
...@@ -76,7 +72,10 @@ ...@@ -76,7 +72,10 @@
}) })
.push(function (result) { .push(function (result) {
base64data = result.data; base64data = result.data;
// return devtools.Target.closeTarget({targetId: targetId}); return devtools.Target.detachFromTarget({sessionId: sessionId});
})
.push(function () {
return devtools.Target.closeTarget({targetId: targetId});
}) })
.push(function () { .push(function () {
return base64data; return base64data;
......
...@@ -69,6 +69,7 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -69,6 +69,7 @@ var BROWSER_PASSWORD = "ignored";
method: `${domain.domain}.${method.name}`, method: `${domain.domain}.${method.name}`,
params: params, params: params,
} }
console.log(command);
var message = JSON.stringify(command); var message = JSON.stringify(command);
gadget.state.websocket.send(message); gadget.state.websocket.send(message);
var handler = { var handler = {
...@@ -78,6 +79,7 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -78,6 +79,7 @@ var BROWSER_PASSWORD = "ignored";
error: reject error: reject
} }
gadget.state.handlers.add(handler); gadget.state.handlers.add(handler);
return gadget.changeState({update: true});
} }
return RSVP.Queue().push(function () { return RSVP.Queue().push(function () {
return RSVP.Promise(callback); return RSVP.Promise(callback);
...@@ -103,6 +105,7 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -103,6 +105,7 @@ var BROWSER_PASSWORD = "ignored";
error: reject error: reject
} }
gadget.state.handlers.add(handler); gadget.state.handlers.add(handler);
return gadget.changeState({update: true});
} }
return RSVP.Queue().push(function () { return RSVP.Queue().push(function () {
return RSVP.Promise(callback); return RSVP.Promise(callback);
...@@ -140,14 +143,9 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -140,14 +143,9 @@ var BROWSER_PASSWORD = "ignored";
} }
let detachFromTarget = devtoolsAPI.Target.detachFromTarget; let detachFromTarget = devtoolsAPI.Target.detachFromTarget;
devtoolsAPI.Target.detachFromTarget = (params) => { devtoolsAPI.Target.detachFromTarget = (params) => {
var response; return gadget.changeState({sessionId: undefined})
return detachFromTarget(params)
.push(function (result) {
response = result;
return gadget.changeState({sessionId: gadget.state.browserId});
})
.push(function () { .push(function () {
return response; return detachFromTarget(params);
}) })
} }
return gadget.changeState({devtools: devtoolsAPI}); return gadget.changeState({devtools: devtoolsAPI});
...@@ -185,14 +183,14 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -185,14 +183,14 @@ var BROWSER_PASSWORD = "ignored";
* @param {object} message The message that contains the result data. * @param {object} message The message that contains the result data.
* @param {object} gadget The gadget that contains all the handlers. * @param {object} gadget The gadget that contains all the handlers.
*/ */
function handleMethodMessage(gadget, message) function handleMethodMessage(gadget, message) {
{
for (let it = gadget.state.handlers.values(), handler = null; handler = it.next().value;) { for (let it = gadget.state.handlers.values(), handler = null; handler = it.next().value;) {
if (handler.id != message.id) { if (handler.id != message.id) {
continue; continue;
} }
gadget.state.handlers.delete(handler); gadget.state.handlers.delete(handler);
gadget.state.messages.delete(message);
if ('error' in message) { if ('error' in message) {
handler.error(message.error); handler.error(message.error);
...@@ -207,9 +205,12 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -207,9 +205,12 @@ var BROWSER_PASSWORD = "ignored";
* @param {object} message The message that contains the event data. * @param {object} message The message that contains the event data.
* @param {object} gadget The gadget that contains all the handlers. * @param {object} gadget The gadget that contains all the handlers.
*/ */
function handleEventMessage(gadget, message) function handleEventMessage(gadget, message) {
{
for (let it = gadget.state.handlers.values(), handler = null; handler = it.next().value;) { for (let it = gadget.state.handlers.values(), handler = null; handler = it.next().value;) {
setTimeout(function () {
gadget.state.messages.delete(message);
}, 5000);
if (handler.method != message.method) { if (handler.method != message.method) {
continue; continue;
} }
...@@ -221,8 +222,8 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -221,8 +222,8 @@ var BROWSER_PASSWORD = "ignored";
rJS(window) rJS(window)
.setState({ .setState({
update: false,
websocket: null, websocket: null,
browserId: undefined,
sessionId: undefined, sessionId: undefined,
nextIndex: 0, nextIndex: 0,
handlers: null, handlers: null,
...@@ -236,11 +237,6 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -236,11 +237,6 @@ var BROWSER_PASSWORD = "ignored";
return getBrowserJSON("json/version") return getBrowserJSON("json/version")
.push(function (result) { .push(function (result) {
var browserUrl = result.webSocketDebuggerUrl; var browserUrl = result.webSocketDebuggerUrl;
// todo: somehow fetch browser id:
// -> not: browserUrl.split("/").slice(-1)[0]
// -> not: Target.getTargets()[x].browserContextId
// -> not: reopen new websocket on Target.detachFromTarget()
gadget.state.browserId = undefined;
return updateWebsocket(gadget, browserUrl); return updateWebsocket(gadget, browserUrl);
}) })
.push(function () { .push(function () {
...@@ -263,16 +259,15 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -263,16 +259,15 @@ var BROWSER_PASSWORD = "ignored";
}) })
.onStateChange(function () { .onStateChange(function () {
var gadget = this; var gadget = this;
gadget.state.update = false;
for (let it = gadget.state.messages.values(), message = null; message = it.next().value;) { for (let it = gadget.state.messages.values(), message = null; message = it.next().value;) {
console.log(message);
if (message.hasOwnProperty("id")) { if (message.hasOwnProperty("id")) {
handleMethodMessage(gadget, message); handleMethodMessage(gadget, message);
} }
if (message.hasOwnProperty("method")) { if (message.hasOwnProperty("method")) {
handleEventMessage(gadget, message); handleEventMessage(gadget, message);
} }
gadget.state.messages.delete(message);
} }
}) })
}(window, rJS, RSVP)); }(window, rJS, RSVP));
\ No newline at end of file
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