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

Commit 5df149d

Browse files
author
Andy Heimdahl
committed
Refactored to detect port changes between LiveDev launches.
1 parent d5dd7b8 commit 5df149d

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

src/extensions/default/StaticServer/StaticServer.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,46 @@ define(function (require, exports, module) {
111111
*/
112112
StaticServer.prototype.readyToServe = function () {
113113
var self = this;
114-
var port = _prefs.get("port");
114+
var deferred = new $.Deferred();
115115

116-
return this._nodeDomain.exec("getServer", self._root, port)
116+
function sanitizePort(port) {
117+
port = parseInt(port, 10);
118+
port = (port && !isNaN(port) && port > 0 && port < 65536) ? port : 0;
119+
return port;
120+
}
121+
122+
function onSuccess(address) {
123+
self._baseUrl = "http://" + address.address + ":" + address.port + "/";
124+
deferred.resolve();
125+
}
126+
127+
function onFailure() {
128+
self._baseUrl = "";
129+
deferred.resolve();
130+
}
131+
132+
var port = sanitizePort(_prefs.get("port"));
133+
134+
this._nodeDomain.exec("getServer", self._root, port)
117135
.done(function (address) {
118-
self._baseUrl = "http://" + address.address + ":" + address.port + "/";
136+
137+
// If the port returned wasn't what was requested, then the preference has
138+
// changed. Close the current server, and open a new one with the new port.
139+
if (address.port !== port && port > 0) {
140+
return self._nodeDomain.exec("closeServer", self._root)
141+
.done(function () {
142+
return self._nodeDomain.exec("getServer", self._root, port)
143+
.done(onSuccess)
144+
.fail(onFailure);
145+
})
146+
.fail(onFailure);
147+
}
148+
149+
onSuccess(address);
119150
})
120-
.fail(function () {
121-
self._baseUrl = "";
122-
});
151+
.fail(onFailure);
152+
153+
return deferred.promise();
123154
};
124155

125156
/**

0 commit comments

Comments
 (0)