Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 7bfddd1

Browse files
committed
Merge pull request #7119 from adobe/jasonsanjose/live-dev-fixes
Do not close/reopen live preview on currentDocumentChange
2 parents 2a7c1c2 + e34e931 commit 7bfddd1

7 files changed

Lines changed: 84 additions & 36 deletions

File tree

src/LiveDevelopment/Agents/NetworkAgent.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ define(function NetworkAgent(require, exports, module) {
6464
_logURL(res.request.url);
6565
}
6666

67+
function _reset() {
68+
_urlRequested = {};
69+
}
70+
6771
// WebInspector Event: Page.frameNavigated
6872
function _onFrameNavigated(event, res) {
6973
// res = {frame}
74+
_reset();
7075
_logURL(res.frame.url);
7176
}
7277

@@ -86,8 +91,7 @@ define(function NetworkAgent(require, exports, module) {
8691

8792
/** Unload the agent */
8893
function unload() {
89-
_urlRequested = {};
90-
94+
_reset();
9195
$(Inspector.Page).off(".NetworkAgent");
9296
$(Inspector.Network).off(".NetworkAgent");
9397
}

src/LiveDevelopment/Agents/RemoteAgent.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,11 @@ define(function RemoteAgent(require, exports, module) {
121121
call("keepAlive");
122122
}, 1000);
123123
}
124-
125-
/**
126-
* @private
127-
* Cancel the keepAlive interval if the page reloads
128-
*/
129-
function _onFrameStartedLoading(event, res) {
130-
_stopKeepAliveInterval();
131-
}
132-
124+
133125
// WebInspector Event: Page.frameNavigated
134126
function _onFrameNavigated(event, res) {
135127
// res = {timestamp}
128+
_stopKeepAliveInterval();
136129

137130
// inject RemoteFunctions
138131
var command = "window._LD=" + RemoteFunctions + "(" + LiveDevelopment.config.experimental + ");";
@@ -153,7 +146,7 @@ define(function RemoteAgent(require, exports, module) {
153146
function load() {
154147
_load = new $.Deferred();
155148
$(Inspector.Page).on("frameNavigated.RemoteAgent", _onFrameNavigated);
156-
$(Inspector.Page).on("frameStartedLoading.RemoteAgent", _onFrameStartedLoading);
149+
$(Inspector.Page).on("frameStartedLoading.RemoteAgent", _stopKeepAliveInterval);
157150
$(Inspector.DOM).on("attributeModified.RemoteAgent", _onAttributeModified);
158151

159152
return _load.promise();

src/LiveDevelopment/Agents/ScriptAgent.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,25 @@ define(function ScriptAgent(require, exports, module) {
117117

118118
}
119119

120-
/** Initialize the agent */
121-
function load() {
120+
function _reset() {
122121
_urlToScript = {};
123122
_idToScript = {};
123+
}
124+
125+
/**
126+
* @private
127+
* WebInspector Event: Page.frameNavigated
128+
* @param {jQuery.Event} event
129+
* @param {frame: Frame} res
130+
*/
131+
function _onFrameNavigated(event, res) {
132+
// Clear maps when navigating to a new page
133+
_reset();
134+
}
135+
136+
/** Initialize the agent */
137+
function load() {
138+
_reset();
124139
_load = new $.Deferred();
125140

126141
var enableResult = new $.Deferred();
@@ -131,6 +146,7 @@ define(function ScriptAgent(require, exports, module) {
131146
});
132147
});
133148

149+
$(Inspector.Page).on("frameNavigated.ScriptAgent", _onFrameNavigated);
134150
$(DOMAgent).on("getDocument.ScriptAgent", _onGetDocument);
135151
$(Inspector.Debugger)
136152
.on("scriptParsed.ScriptAgent", _onScriptParsed)
@@ -143,6 +159,8 @@ define(function ScriptAgent(require, exports, module) {
143159

144160
/** Clean up */
145161
function unload() {
162+
_reset();
163+
$(Inspector.Page).off(".ScriptAgent");
146164
$(DOMAgent).off(".ScriptAgent");
147165
$(Inspector.Debugger).off(".ScriptAgent");
148166
$(Inspector.DOM).off(".ScriptAgent");

src/LiveDevelopment/Inspector/Inspector.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ define(function Inspector(require, exports, module) {
8888
// jQuery exports object for events
8989
var $exports = $(exports);
9090

91+
/**
92+
* Map message IDs to the callback function and original JSON message
93+
* @type {Object.<number, {callback: function, message: Object}}
94+
*/
95+
var _messageCallbacks = {};
96+
9197
var _messageId = 1; // id used for remote method calls, auto-incrementing
92-
var _messageCallbacks = {}; // {id -> function} for remote method calls
9398
var _socket; // remote debugger WebSocket
9499
var _connectDeferred; // The deferred connect
95100

@@ -125,7 +130,7 @@ define(function Inspector(require, exports, module) {
125130
return (new $.Deferred()).reject().promise();
126131
}
127132

128-
var id, callback, args, i, params = {}, promise;
133+
var id, callback, args, i, params = {}, promise, msg;
129134

130135
// extract the parameters, the callback function, and the message id
131136
args = Array.prototype.slice.call(arguments, 2);
@@ -144,7 +149,6 @@ define(function Inspector(require, exports, module) {
144149
}
145150

146151
id = _messageId++;
147-
_messageCallbacks[id] = callback;
148152

149153
// verify the parameters against the method signature
150154
// this also constructs the params object of type {name -> value}
@@ -156,7 +160,10 @@ define(function Inspector(require, exports, module) {
156160
}
157161
}
158162

159-
_socket.send(JSON.stringify({ method: method, id: id, params: params }));
163+
// Store message callback and send message
164+
msg = { method: method, id: id, params: params };
165+
_messageCallbacks[id] = { callback: callback, message: msg };
166+
_socket.send(JSON.stringify(msg));
160167

161168
return promise;
162169
}
@@ -204,10 +211,12 @@ define(function Inspector(require, exports, module) {
204211
* @param {object} message
205212
*/
206213
function _onMessage(message) {
207-
var response = JSON.parse(message.data),
208-
callback = _messageCallbacks[response.id];
214+
var response = JSON.parse(message.data),
215+
msgRecord = _messageCallbacks[response.id],
216+
callback = msgRecord && msgRecord.callback,
217+
msgText = (msgRecord && msgRecord.message) || "No message";
209218

210-
if (callback) {
219+
if (msgRecord) {
211220
// Messages with an ID are a response to a command, fire callback
212221
callback(response.result, response.error);
213222
delete _messageCallbacks[response.id];
@@ -224,7 +233,7 @@ define(function Inspector(require, exports, module) {
224233
$exports.triggerHandler("message", [response]);
225234

226235
if (response.error) {
227-
$exports.triggerHandler("error", [response.error]);
236+
$exports.triggerHandler("error", [response.error, msgText]);
228237
}
229238
}
230239

src/LiveDevelopment/LiveDevelopment.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ define(function LiveDevelopment(require, exports, module) {
407407
}
408408

409409
/** Triggered by Inspector.error */
410-
function _onError(event, error) {
410+
function _onError(event, error, msgData) {
411411
var message;
412412

413413
// Sometimes error.message is undefined
@@ -430,7 +430,7 @@ define(function LiveDevelopment(require, exports, module) {
430430
}
431431

432432
// Show the message, but include the error object for further information (e.g. error code)
433-
console.error(message, error);
433+
console.error(message, error, msgData);
434434
}
435435

436436
function _styleSheetAdded(event, url) {
@@ -1106,19 +1106,22 @@ define(function LiveDevelopment(require, exports, module) {
11061106
}
11071107
});
11081108
}
1109+
1110+
function _createLiveDocumentForFrame(doc) {
1111+
// create live document
1112+
doc._ensureMasterEditor();
1113+
_liveDocument = _createDocument(doc, doc._masterEditor);
1114+
_server.add(_liveDocument);
1115+
}
11091116

11101117
// helper function that actually does the launch once we are sure we have
11111118
// a doc and the server for that doc is up and running.
11121119
function _doLaunchAfterServerReady(initialDoc) {
11131120
// update status
11141121
_setStatus(STATUS_CONNECTING);
1115-
1116-
// create live document
1117-
initialDoc._ensureMasterEditor();
1118-
_liveDocument = _createDocument(initialDoc, initialDoc._masterEditor);
1122+
_createLiveDocumentForFrame(initialDoc);
11191123

11201124
// start listening for requests
1121-
_server.add(_liveDocument);
11221125
_server.start();
11231126

11241127
// Install a one-time event handler when connected to the launcher page
@@ -1255,9 +1258,22 @@ define(function LiveDevelopment(require, exports, module) {
12551258
isViewable = exports.config.experimental || (_server && _server.canServe(doc.file.fullPath));
12561259

12571260
if (!wasRequested && isViewable) {
1258-
// TODO (jasonsanjose): optimize this by reusing the same connection
1259-
// no need to fully teardown.
1260-
close().done(open);
1261+
// Update status
1262+
_setStatus(STATUS_CONNECTING);
1263+
1264+
// clear live doc and related docs
1265+
_closeDocuments();
1266+
1267+
// create new live doc
1268+
_createLiveDocumentForFrame(doc);
1269+
1270+
// Navigate to the new page within this site. Agents must handle
1271+
// frameNavigated event to clear any saved state.
1272+
Inspector.Page.navigate(docUrl).then(function () {
1273+
_setStatus(STATUS_ACTIVE);
1274+
}, function () {
1275+
_close(false, "closed_unknown_reason");
1276+
});
12611277
} else if (wasRequested) {
12621278
// Update highlight
12631279
showHighlight();

src/LiveDevelopment/Servers/BaseServer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ define(function (require, exports, module) {
169169
* @param {Object} liveDocument
170170
*/
171171
BaseServer.prototype.add = function (liveDocument) {
172+
if (!liveDocument) {
173+
return;
174+
}
175+
172176
// use the project relative path as a key to lookup requests
173177
var key = this._documentKey(liveDocument.doc.file.fullPath);
174178

@@ -181,6 +185,10 @@ define(function (require, exports, module) {
181185
* @param {Object} liveDocument
182186
*/
183187
BaseServer.prototype.remove = function (liveDocument) {
188+
if (!liveDocument) {
189+
return;
190+
}
191+
184192
var key = this._liveDocuments[this._documentKey(liveDocument.doc.file.fullPath)];
185193

186194
if (key) {

src/nls/root/strings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ define({
7777
"ERROR_MAX_FILES_TITLE" : "Error Indexing Files",
7878
"ERROR_MAX_FILES" : "The maximum number of files have been indexed. Actions that look up files in the index may function incorrectly.",
7979

80-
// Live Development error strings
80+
// Live Preview error strings
8181
"ERROR_LAUNCHING_BROWSER_TITLE" : "Error launching browser",
8282
"ERROR_CANT_FIND_CHROME" : "The Google Chrome browser could not be found. Please make sure it is installed.",
8383
"ERROR_LAUNCHING_BROWSER" : "An error occurred when launching the browser. (error {0})",
8484

8585
"LIVE_DEVELOPMENT_ERROR_TITLE" : "Live Preview Error",
8686
"LIVE_DEVELOPMENT_RELAUNCH_TITLE" : "Connecting to Browser",
8787
"LIVE_DEVELOPMENT_ERROR_MESSAGE" : "In order for Live Preview to connect, Chrome needs to be relaunched with remote debugging enabled.<br /><br />Would you like to relaunch Chrome and enable remote debugging?",
88-
"LIVE_DEV_LOADING_ERROR_MESSAGE" : "Unable to load Live Development page",
88+
"LIVE_DEV_LOADING_ERROR_MESSAGE" : "Unable to load Live Preview page",
8989
"LIVE_DEV_NEED_HTML_MESSAGE" : "Open an HTML file or make sure there is an index.html file in your project in order to launch live preview.",
9090
"LIVE_DEV_NEED_BASEURL_MESSAGE" : "To launch live preview with a server-side file, you need to specify a Base URL for this project.",
91-
"LIVE_DEV_SERVER_NOT_READY_MESSAGE" : "Error starting up the HTTP server for live development files. Please try again.",
91+
"LIVE_DEV_SERVER_NOT_READY_MESSAGE" : "Error starting up the HTTP server for live preview files. Please try again.",
9292
"LIVE_DEVELOPMENT_INFO_TITLE" : "Welcome to Live Preview!",
9393
"LIVE_DEVELOPMENT_INFO_MESSAGE" : "Live Preview connects {APP_NAME} to your browser. It launches a preview of your HTML file in the browser, then updates the preview instantly as you edit your code.<br /><br />In this early version of {APP_NAME}, Live Preview only works with <strong>Google Chrome</strong> and updates live as you edit <strong>CSS or HTML files</strong>. Changes to JavaScript files are automatically reloaded when you save.<br /><br />(You'll only see this message once.)",
94-
"LIVE_DEVELOPMENT_TROUBLESHOOTING" : "For more information, see <a href='{0}' title='{0}'>Troubleshooting Live Development connection errors</a>.",
94+
"LIVE_DEVELOPMENT_TROUBLESHOOTING" : "For more information, see <a href='{0}' title='{0}'>Troubleshooting Live Preview connection errors</a>.",
9595

9696
"LIVE_DEV_STATUS_TIP_NOT_CONNECTED" : "Live Preview",
9797
"LIVE_DEV_STATUS_TIP_PROGRESS1" : "Live Preview: Connecting\u2026",

0 commit comments

Comments
 (0)