From c1093b87ed419d8b7e32b7b4ee0b76cb9162ad3c Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Mon, 15 Apr 2013 13:39:40 -0700 Subject: [PATCH 1/5] Fix enabling of inspector Page domain. Cleanup live dev startup sequence. Add fallback promise to inspector method calls without explicit callback. --- src/LiveDevelopment/Agents/CSSAgent.js | 16 +++++---- src/LiveDevelopment/Agents/ConsoleAgent.js | 11 ++++--- src/LiveDevelopment/Agents/DOMAgent.js | 1 - src/LiveDevelopment/Agents/NetworkAgent.js | 5 +-- src/LiveDevelopment/Inspector/Inspector.js | 19 ++++++++--- src/LiveDevelopment/LiveDevelopment.js | 38 +++++++++++++--------- 6 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/LiveDevelopment/Agents/CSSAgent.js b/src/LiveDevelopment/Agents/CSSAgent.js index bc25a1965da..41931d5f344 100644 --- a/src/LiveDevelopment/Agents/CSSAgent.js +++ b/src/LiveDevelopment/Agents/CSSAgent.js @@ -53,13 +53,15 @@ define(function CSSAgent(require, exports, module) { function _onLoadEventFired(event, res) { // res = {timestamp} _urlToStyle = {}; - Inspector.CSS.getAllStyleSheets(function onGetAllStyleSheets(res) { - var i, header; - for (i in res.headers) { - header = res.headers[i]; - _urlToStyle[_canonicalize(header.sourceURL)] = header; - } - _load.resolve(); + Inspector.CSS.enable().done(function () { + Inspector.CSS.getAllStyleSheets(function onGetAllStyleSheets(res) { + var i, header; + for (i in res.headers) { + header = res.headers[i]; + _urlToStyle[_canonicalize(header.sourceURL)] = header; + } + _load.resolve(); + }); }); } diff --git a/src/LiveDevelopment/Agents/ConsoleAgent.js b/src/LiveDevelopment/Agents/ConsoleAgent.js index 5cef3d90e7b..d43c1a842e8 100644 --- a/src/LiveDevelopment/Agents/ConsoleAgent.js +++ b/src/LiveDevelopment/Agents/ConsoleAgent.js @@ -74,11 +74,12 @@ define(function ConsoleAgent(require, exports, module) { /** Initialize the agent */ function load() { - Inspector.Console.enable(); - $(Inspector.Console) - .on("messageAdded.ConsoleAgent", _onMessageAdded) - .on("messageRepeatCountUpdated.ConsoleAgent", _onMessageRepeatCountUpdated) - .on("messagesCleared.ConsoleAgent", _onMessagesCleared); + return Inspector.Console.enable().done(function () { + $(Inspector.Console) + .on("messageAdded.ConsoleAgent", _onMessageAdded) + .on("messageRepeatCountUpdated.ConsoleAgent", _onMessageRepeatCountUpdated) + .on("messagesCleared.ConsoleAgent", _onMessagesCleared); + }); } /** Clean up */ diff --git a/src/LiveDevelopment/Agents/DOMAgent.js b/src/LiveDevelopment/Agents/DOMAgent.js index f34e030f48a..36cb8fad443 100644 --- a/src/LiveDevelopment/Agents/DOMAgent.js +++ b/src/LiveDevelopment/Agents/DOMAgent.js @@ -312,7 +312,6 @@ define(function DOMAgent(require, exports, module) { .on("childNodeCountUpdated.DOMAgent", _onChildNodeCountUpdated) .on("childNodeInserted.DOMAgent", _onChildNodeInserted) .on("childNodeRemoved.DOMAgent", _onChildNodeRemoved); - Inspector.Page.enable(); return _load.promise(); } diff --git a/src/LiveDevelopment/Agents/NetworkAgent.js b/src/LiveDevelopment/Agents/NetworkAgent.js index aaafe461004..9a7e66339a9 100644 --- a/src/LiveDevelopment/Agents/NetworkAgent.js +++ b/src/LiveDevelopment/Agents/NetworkAgent.js @@ -64,8 +64,9 @@ define(function NetworkAgent(require, exports, module) { /** Initialize the agent */ function load() { _urlRequested = {}; - Inspector.Network.enable(); - $(Inspector.Network).on("requestWillBeSent.NetworkAgent", _onRequestWillBeSent); + return Inspector.Network.enable().done(function () { + $(Inspector.Network).on("requestWillBeSent.NetworkAgent", _onRequestWillBeSent); + }); } /** Unload the agent */ diff --git a/src/LiveDevelopment/Inspector/Inspector.js b/src/LiveDevelopment/Inspector/Inspector.js index 045bdcde524..004fb74f80f 100644 --- a/src/LiveDevelopment/Inspector/Inspector.js +++ b/src/LiveDevelopment/Inspector/Inspector.js @@ -122,17 +122,23 @@ define(function Inspector(require, exports, module) { } console.assert(_socket, "You must connect to the WebSocket before sending messages."); - var id, callback, args, i, params = {}; + var id, callback, args, i, params = {}, deferred, promise; // extract the parameters, the callback function, and the message id args = Array.prototype.slice.call(arguments, 2); if (typeof args[args.length - 1] === "function") { - id = _messageId++; - _messageCallbacks[id] = args.pop(); + callback = args.pop(); } else { - id = 0; + var deferred = new $.Deferred(); + promise = deferred.promise(); + callback = function (result) { + deferred.resolve(result); + }; } + id = _messageId++; + _messageCallbacks[id] = callback; + // verify the parameters against the method signature // this also constructs the params object of type {name -> value} for (i in signature) { @@ -141,6 +147,8 @@ define(function Inspector(require, exports, module) { } } _socket.send(JSON.stringify({ method: method, id: id, params: params })); + + return promise; } /** WebSocket did close */ @@ -182,6 +190,7 @@ define(function Inspector(require, exports, module) { } else if (response.result) { if (_messageCallbacks[response.id]) { _messageCallbacks[response.id](response.result); + delete _messageCallbacks[response.id]; } } else { var domainAndMethod = response.method.split("."); @@ -215,7 +224,9 @@ define(function Inspector(require, exports, module) { request.onerror = function onError() { def.reject(request.response); }; + request.send(null); + return def.promise(); } diff --git a/src/LiveDevelopment/LiveDevelopment.js b/src/LiveDevelopment/LiveDevelopment.js index 6dd20ea7a28..b6b7d7e6c54 100644 --- a/src/LiveDevelopment/LiveDevelopment.js +++ b/src/LiveDevelopment/LiveDevelopment.js @@ -403,8 +403,8 @@ define(function LiveDevelopment(require, exports, module) { /** Load the agents */ function loadAgents() { - var name, promises = []; - var agentsToLoad; + var name, promises = [], agentsToLoad, promise; + if (exports.config.experimental) { // load all agents agentsToLoad = agents; @@ -414,7 +414,12 @@ define(function LiveDevelopment(require, exports, module) { } for (name in agentsToLoad) { if (agentsToLoad.hasOwnProperty(name) && agents[name] && agents[name].load) { - promises.push(agents[name].load()); + promise = agents[name].load(); + + if (promise) { + promises.push(promise); + } + _loadedAgentNames.push(name); } } @@ -542,8 +547,8 @@ define(function LiveDevelopment(require, exports, module) { /** Triggered by Inspector.disconnect */ function _onDisconnect(event) { - $(Inspector.Inspector).off("detached", _onDetached); - $(Inspector.Page).off("frameNavigated.DOMAgent", _onFrameNavigated); + $(Inspector.Inspector).off("detached.livedev"); + $(Inspector.Page).off("frameNavigated.livedev"); unloadAgents(); _closeDocument(); @@ -814,22 +819,25 @@ define(function LiveDevelopment(require, exports, module) { * interstitial page has finished loading. */ function onInterstitialPageLoad() { + // Page domain must be enabled first before loading other agents + Inspector.Page.enable().done(function () { + // Load the right document (some agents are waiting for the page's load event) + var doc = _getCurrentDocument(); + if (doc) { + Inspector.Page.navigate(doc.root.url); + } else { + close(); + } + }); + // Load agents _setStatus(STATUS_LOADING_AGENTS); var promises = loadAgents(); $.when.apply(undefined, promises).done(_onLoad).fail(_onError); - - // Load the right document (some agents are waiting for the page's load event) - var doc = _getCurrentDocument(); - if (doc) { - Inspector.Page.navigate(doc.root.url); - } else { - close(); - } } - $(Inspector.Inspector).on("detached", _onDetached); - $(Inspector.Page).on("frameNavigated.DOMAgent", _onFrameNavigated); + $(Inspector.Inspector).on("detached.livedev", _onDetached); + $(Inspector.Page).on("frameNavigated.livedev", _onFrameNavigated); waitForInterstitialPageLoad() .fail(function () { From 644afdcce72153caadfbf9fb60a08a11cdc6eb41 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Mon, 15 Apr 2013 13:53:10 -0700 Subject: [PATCH 2/5] fix jshint error --- src/LiveDevelopment/Inspector/Inspector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LiveDevelopment/Inspector/Inspector.js b/src/LiveDevelopment/Inspector/Inspector.js index 004fb74f80f..07ec0ee5a77 100644 --- a/src/LiveDevelopment/Inspector/Inspector.js +++ b/src/LiveDevelopment/Inspector/Inspector.js @@ -122,7 +122,7 @@ define(function Inspector(require, exports, module) { } console.assert(_socket, "You must connect to the WebSocket before sending messages."); - var id, callback, args, i, params = {}, deferred, promise; + var id, callback, args, i, params = {}, promise; // extract the parameters, the callback function, and the message id args = Array.prototype.slice.call(arguments, 2); From 993ea5fba0e7d7b0cca36f04a2cd43eb8aa1c40f Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Tue, 16 Apr 2013 18:14:25 -0700 Subject: [PATCH 3/5] reconcile with iwehrman's cleanup in #3202 --- src/LiveDevelopment/Agents/RemoteAgent.js | 24 +++++++++---------- src/LiveDevelopment/Agents/RemoteFunctions.js | 2 +- src/LiveDevelopment/Agents/ScriptAgent.js | 14 ++++++++--- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/LiveDevelopment/Agents/RemoteAgent.js b/src/LiveDevelopment/Agents/RemoteAgent.js index fb03c24beff..0d307425683 100644 --- a/src/LiveDevelopment/Agents/RemoteAgent.js +++ b/src/LiveDevelopment/Agents/RemoteAgent.js @@ -37,8 +37,9 @@ define(function RemoteAgent(require, exports, module) { var $exports = $(exports); - var LiveDevelopment = require("LiveDevelopment/LiveDevelopment"); - var Inspector = require("LiveDevelopment/Inspector/Inspector"); + var LiveDevelopment = require("LiveDevelopment/LiveDevelopment"), + Inspector = require("LiveDevelopment/Inspector/Inspector"), + RemoteFunctions = require("text!LiveDevelopment/Agents/RemoteFunctions.js"); var _load; // deferred load var _objectId; // the object id of the remote object @@ -47,17 +48,16 @@ define(function RemoteAgent(require, exports, module) { // WebInspector Event: Page.loadEventFired function _onLoadEventFired(event, res) { // res = {timestamp} - var request = new XMLHttpRequest(); - request.open("GET", "LiveDevelopment/Agents/RemoteFunctions.js"); - request.onload = function onLoad() { - var run = "window._LD=" + request.response + "(" + LiveDevelopment.config.experimental + ")"; - Inspector.Runtime.evaluate(run, function onEvaluate(res) { - console.assert(!res.wasThrown, res.result.description); - _objectId = res.result.objectId; + var command = "window._LD=" + RemoteFunctions + "(" + LiveDevelopment.config.experimental + ")"; + + Inspector.Runtime.evaluate(command, function onEvaluate(response) { + if (response.error || response.wasThrown) { + _load.reject(null, response.error); + } else { + _objectId = response.result.objectId; _load.resolve(); - }); - }; - request.send(null); + } + }); } // WebInspector Event: DOM.attributeModified diff --git a/src/LiveDevelopment/Agents/RemoteFunctions.js b/src/LiveDevelopment/Agents/RemoteFunctions.js index 813c6f43cdb..4bfafeebece 100644 --- a/src/LiveDevelopment/Agents/RemoteFunctions.js +++ b/src/LiveDevelopment/Agents/RemoteFunctions.js @@ -22,7 +22,7 @@ */ -/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, forin: true, maxerr: 50, regexp: true */ +/*jslint vars: true, plusplus: true, browser: true, nomen: true, indent: 4, forin: true, maxerr: 50, regexp: true */ /*global define, $, window, document, navigator */ /** diff --git a/src/LiveDevelopment/Agents/ScriptAgent.js b/src/LiveDevelopment/Agents/ScriptAgent.js index fa723785a0c..58014f9dec0 100644 --- a/src/LiveDevelopment/Agents/ScriptAgent.js +++ b/src/LiveDevelopment/Agents/ScriptAgent.js @@ -122,15 +122,23 @@ define(function ScriptAgent(require, exports, module) { _urlToScript = {}; _idToScript = {}; _load = new $.Deferred(); - Inspector.Debugger.enable(); - Inspector.Debugger.setPauseOnExceptions("uncaught"); + + var enableResult = new $.Deferred(); + + Inspector.Debugger.enable().done(function () { + Inspector.Debugger.setPauseOnExceptions("uncaught").done(function () { + enableResult.resolve(); + }); + }); + $(DOMAgent).on("getDocument.ScriptAgent", _onGetDocument); $(Inspector.Debugger) .on("scriptParsed.ScriptAgent", _onScriptParsed) .on("scriptFailedToParse.ScriptAgent", _onScriptFailedToParse) .on("paused.ScriptAgent", _onPaused); $(Inspector.DOM).on("childNodeInserted.ScriptAgent", _onChildNodeInserted); - return _load; + + return $.when(_load.promise(), enableResult.promise()); } /** Clean up */ From 76c35f6c779270293917070e788d5f046fc87f91 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Wed, 17 Apr 2013 09:28:03 -0700 Subject: [PATCH 4/5] check for null after _createDocument --- src/LiveDevelopment/LiveDevelopment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LiveDevelopment/LiveDevelopment.js b/src/LiveDevelopment/LiveDevelopment.js index b6b7d7e6c54..b5ade9da8da 100644 --- a/src/LiveDevelopment/LiveDevelopment.js +++ b/src/LiveDevelopment/LiveDevelopment.js @@ -331,7 +331,7 @@ define(function LiveDevelopment(require, exports, module) { _liveDocument = _createDocument(doc, editor); // Enable instrumentation - if (_liveDocument.setInstrumentationEnabled) { + if (_liveDocument && _liveDocument.setInstrumentationEnabled) { var enableInstrumentation = false; if (_serverProvider && _serverProvider.setRequestFilterPaths) { From e57098044b32e4e09a5e606d0619c29b5d282376 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Wed, 17 Apr 2013 10:47:38 -0700 Subject: [PATCH 5/5] fix for #3410 to reconnect agents before reloading the live document in browser --- src/LiveDevelopment/LiveDevelopment.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LiveDevelopment/LiveDevelopment.js b/src/LiveDevelopment/LiveDevelopment.js index b5ade9da8da..d5af6395d27 100644 --- a/src/LiveDevelopment/LiveDevelopment.js +++ b/src/LiveDevelopment/LiveDevelopment.js @@ -890,11 +890,11 @@ define(function LiveDevelopment(require, exports, module) { function _onDocumentSaved(event, doc) { if (doc && Inspector.connected() && _classForDocument(doc) !== CSSDocument && agents.network && agents.network.wasURLRequested(doc.url)) { + // Unload and reload agents before reloading the page + reconnect(); + // Reload HTML page Inspector.Page.reload(); - - // Reload unsaved changes - reconnect(); } }