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

Commit

Permalink
Refactored to detect port changes between LiveDev launches.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Heimdahl committed Feb 12, 2014
1 parent d5dd7b8 commit 5df149d
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/extensions/default/StaticServer/StaticServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,46 @@ define(function (require, exports, module) {
*/
StaticServer.prototype.readyToServe = function () {
var self = this;
var port = _prefs.get("port");
var deferred = new $.Deferred();

return this._nodeDomain.exec("getServer", self._root, port)
function sanitizePort(port) {
port = parseInt(port, 10);
port = (port && !isNaN(port) && port > 0 && port < 65536) ? port : 0;
return port;
}

function onSuccess(address) {
self._baseUrl = "http://" + address.address + ":" + address.port + "/";
deferred.resolve();
}

function onFailure() {
self._baseUrl = "";
deferred.resolve();
}

var port = sanitizePort(_prefs.get("port"));

this._nodeDomain.exec("getServer", self._root, port)
.done(function (address) {
self._baseUrl = "http://" + address.address + ":" + address.port + "/";

// If the port returned wasn't what was requested, then the preference has
// changed. Close the current server, and open a new one with the new port.
if (address.port !== port && port > 0) {
return self._nodeDomain.exec("closeServer", self._root)
.done(function () {
return self._nodeDomain.exec("getServer", self._root, port)
.done(onSuccess)
.fail(onFailure);
})
.fail(onFailure);
}

onSuccess(address);
})
.fail(function () {
self._baseUrl = "";
});
.fail(onFailure);

return deferred.promise();
};

/**
Expand Down

0 comments on commit 5df149d

Please sign in to comment.