update livereload.js

This commit is contained in:
John Bintz 2012-07-10 17:50:52 -04:00
parent e59329ed38
commit 0d82995d8c
2 changed files with 533 additions and 404 deletions

View File

@ -123,12 +123,17 @@ __protocol.Parser = Parser = (function() {
})(); })();
// connector // connector
// Generated by CoffeeScript 1.3.3
var Connector, PROTOCOL_6, PROTOCOL_7, Parser, Version, _ref; var Connector, PROTOCOL_6, PROTOCOL_7, Parser, Version, _ref;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
_ref = __protocol, Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7; _ref = __protocol, Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
Version = '2.0.3';
Version = '2.0.8';
__connector.Connector = Connector = (function() { __connector.Connector = Connector = (function() {
function Connector(options, WebSocket, Timer, handlers) { function Connector(options, WebSocket, Timer, handlers) {
var _this = this;
this.options = options; this.options = options;
this.WebSocket = WebSocket; this.WebSocket = WebSocket;
this.Timer = Timer; this.Timer = Timer;
@ -138,64 +143,66 @@ __connector.Connector = Connector = (function() {
this._connectionDesired = false; this._connectionDesired = false;
this.protocol = 0; this.protocol = 0;
this.protocolParser = new Parser({ this.protocolParser = new Parser({
connected: __bind(function(protocol) { connected: function(protocol) {
this.protocol = protocol; _this.protocol = protocol;
this._handshakeTimeout.stop(); _this._handshakeTimeout.stop();
this._nextDelay = this.options.mindelay; _this._nextDelay = _this.options.mindelay;
this._disconnectionReason = 'broken'; _this._disconnectionReason = 'broken';
return this.handlers.connected(protocol); return _this.handlers.connected(protocol);
}, this), },
error: __bind(function(e) { error: function(e) {
this.handlers.error(e); _this.handlers.error(e);
return this._closeOnError(); return _this._closeOnError();
}, this), },
message: __bind(function(message) { message: function(message) {
return this.handlers.message(message); return _this.handlers.message(message);
}, this) }
}); });
this._handshakeTimeout = new Timer(__bind(function() { this._handshakeTimeout = new Timer(function() {
if (!this._isSocketConnected()) { if (!_this._isSocketConnected()) {
return; return;
} }
this._disconnectionReason = 'handshake-timeout'; _this._disconnectionReason = 'handshake-timeout';
return this.socket.close(); return _this.socket.close();
}, this)); });
this._reconnectTimer = new Timer(__bind(function() { this._reconnectTimer = new Timer(function() {
if (!this._connectionDesired) { if (!_this._connectionDesired) {
return; return;
} }
return this.connect(); return _this.connect();
}, this)); });
this.connect(); this.connect();
} }
Connector.prototype._isSocketConnected = function() { Connector.prototype._isSocketConnected = function() {
return this.socket && this.socket.readyState === this.WebSocket.OPEN; return this.socket && this.socket.readyState === this.WebSocket.OPEN;
}; };
Connector.prototype.connect = function() { Connector.prototype.connect = function() {
var _this = this;
this._connectionDesired = true; this._connectionDesired = true;
if (this._isSocketConnected()) { if (this._isSocketConnected()) {
return; return;
} }
if (this._reconnectTimer) { this._reconnectTimer.stop();
clearTimeout(this._reconnectTimer);
}
this._disconnectionReason = 'cannot-connect'; this._disconnectionReason = 'cannot-connect';
this.protocolParser.reset(); this.protocolParser.reset();
this.handlers.connecting(); this.handlers.connecting();
this.socket = new this.WebSocket(this._uri); this.socket = new this.WebSocket(this._uri);
this.socket.onopen = __bind(function(e) { this.socket.onopen = function(e) {
return this._onopen(e); return _this._onopen(e);
}, this);
this.socket.onclose = __bind(function(e) {
return this._onclose(e);
}, this);
this.socket.onmessage = __bind(function(e) {
return this._onmessage(e);
}, this);
return this.socket.onerror = __bind(function(e) {
return this._onerror(e);
}, this);
}; };
this.socket.onclose = function(e) {
return _this._onclose(e);
};
this.socket.onmessage = function(e) {
return _this._onmessage(e);
};
return this.socket.onerror = function(e) {
return _this._onerror(e);
};
};
Connector.prototype.disconnect = function() { Connector.prototype.disconnect = function() {
this._connectionDesired = false; this._connectionDesired = false;
this._reconnectTimer.stop(); this._reconnectTimer.stop();
@ -205,6 +212,7 @@ __connector.Connector = Connector = (function() {
this._disconnectionReason = 'manual'; this._disconnectionReason = 'manual';
return this.socket.close(); return this.socket.close();
}; };
Connector.prototype._scheduleReconnection = function() { Connector.prototype._scheduleReconnection = function() {
if (!this._connectionDesired) { if (!this._connectionDesired) {
return; return;
@ -214,20 +222,24 @@ __connector.Connector = Connector = (function() {
return this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2); return this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2);
} }
}; };
Connector.prototype.sendCommand = function(command) { Connector.prototype.sendCommand = function(command) {
if (this.protocol == null) { if (this.protocol == null) {
return; return;
} }
return this._sendCommand(command); return this._sendCommand(command);
}; };
Connector.prototype._sendCommand = function(command) { Connector.prototype._sendCommand = function(command) {
return this.socket.send(JSON.stringify(command)); return this.socket.send(JSON.stringify(command));
}; };
Connector.prototype._closeOnError = function() { Connector.prototype._closeOnError = function() {
this._handshakeTimeout.stop(); this._handshakeTimeout.stop();
this._disconnectionReason = 'error'; this._disconnectionReason = 'error';
return this.socket.close(); return this.socket.close();
}; };
Connector.prototype._onopen = function(e) { Connector.prototype._onopen = function(e) {
var hello; var hello;
this.handlers.socketConnected(); this.handlers.socketConnected();
@ -249,16 +261,21 @@ __connector.Connector = Connector = (function() {
this._sendCommand(hello); this._sendCommand(hello);
return this._handshakeTimeout.start(this.options.handshake_timeout); return this._handshakeTimeout.start(this.options.handshake_timeout);
}; };
Connector.prototype._onclose = function(e) { Connector.prototype._onclose = function(e) {
this.protocol = 0; this.protocol = 0;
this.handlers.disconnected(this._disconnectionReason, this._nextDelay); this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
return this._scheduleReconnection(); return this._scheduleReconnection();
}; };
Connector.prototype._onerror = function(e) {}; Connector.prototype._onerror = function(e) {};
Connector.prototype._onmessage = function(e) { Connector.prototype._onmessage = function(e) {
return this.protocolParser.process(e.data); return this.protocolParser.process(e.data);
}; };
return Connector; return Connector;
})(); })();
// timer // timer
@ -349,8 +366,10 @@ Options.extract = function(document) {
}; };
// reloader // reloader
// Generated by CoffeeScript 1.3.1
(function() {
var IMAGE_STYLES, Reloader, numberOfMatchingSegments, pathFromUrl, pathsMatch, pickBestMatch, splitUrl; var IMAGE_STYLES, Reloader, numberOfMatchingSegments, pathFromUrl, pathsMatch, pickBestMatch, splitUrl;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
splitUrl = function(url) { splitUrl = function(url) {
var hash, index, params; var hash, index, params;
if ((index = url.indexOf('#')) >= 0) { if ((index = url.indexOf('#')) >= 0) {
@ -371,6 +390,7 @@ splitUrl = function(url) {
hash: hash hash: hash
}; };
}; };
pathFromUrl = function(url) { pathFromUrl = function(url) {
var path; var path;
url = splitUrl(url).url; url = splitUrl(url).url;
@ -381,6 +401,7 @@ pathFromUrl = function(url) {
} }
return decodeURIComponent(path); return decodeURIComponent(path);
}; };
pickBestMatch = function(path, objects, pathFunc) { pickBestMatch = function(path, objects, pathFunc) {
var bestMatch, object, score, _i, _len; var bestMatch, object, score, _i, _len;
bestMatch = { bestMatch = {
@ -402,6 +423,7 @@ pickBestMatch = function(path, objects, pathFunc) {
return null; return null;
} }
}; };
numberOfMatchingSegments = function(path1, path2) { numberOfMatchingSegments = function(path1, path2) {
var comps1, comps2, eqCount, len; var comps1, comps2, eqCount, len;
path1 = path1.replace(/^\/+/, '').toLowerCase(); path1 = path1.replace(/^\/+/, '').toLowerCase();
@ -418,9 +440,11 @@ numberOfMatchingSegments = function(path1, path2) {
} }
return eqCount; return eqCount;
}; };
pathsMatch = function(path1, path2) { pathsMatch = function(path1, path2) {
return numberOfMatchingSegments(path1, path2) > 0; return numberOfMatchingSegments(path1, path2) > 0;
}; };
IMAGE_STYLES = [ IMAGE_STYLES = [
{ {
selector: 'background', selector: 'background',
@ -430,24 +454,34 @@ IMAGE_STYLES = [
styleNames: ['borderImage', 'webkitBorderImage', 'MozBorderImage'] styleNames: ['borderImage', 'webkitBorderImage', 'MozBorderImage']
} }
]; ];
__reloader.Reloader = Reloader = (function() { __reloader.Reloader = Reloader = (function() {
Reloader.name = 'Reloader';
function Reloader(window, console, Timer) { function Reloader(window, console, Timer) {
this.window = window; this.window = window;
this.console = console; this.console = console;
this.Timer = Timer; this.Timer = Timer;
this.document = this.window.document; this.document = this.window.document;
this.stylesheetGracePeriod = 200;
this.importCacheWaitPeriod = 200; this.importCacheWaitPeriod = 200;
this.plugins = []; this.plugins = [];
} }
Reloader.prototype.addPlugin = function(plugin) { Reloader.prototype.addPlugin = function(plugin) {
return this.plugins.push(plugin); return this.plugins.push(plugin);
}; };
Reloader.prototype.analyze = function(callback) { Reloader.prototype.analyze = function(callback) {
return results; return results;
}; };
Reloader.prototype.reload = function(path, options) { Reloader.prototype.reload = function(path, options) {
var plugin, _i, _len, _ref; var plugin, _base, _i, _len, _ref;
this.options = options;
if ((_base = this.options).stylesheetReloadTimeout == null) {
_base.stylesheetReloadTimeout = 15000;
}
_ref = this.plugins; _ref = this.plugins;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
plugin = _ref[_i]; plugin = _ref[_i];
@ -470,11 +504,13 @@ __reloader.Reloader = Reloader = (function() {
} }
return this.reloadPage(); return this.reloadPage();
}; };
Reloader.prototype.reloadPage = function() { Reloader.prototype.reloadPage = function() {
return this.window.document.location.reload(); return this.window.document.location.reload();
}; };
Reloader.prototype.reloadImages = function(path) { Reloader.prototype.reloadImages = function(path) {
var expando, img, selector, styleNames, styleSheet, _i, _j, _k, _l, _len, _len2, _len3, _len4, _ref, _ref2, _ref3, _ref4, _results; var expando, img, selector, styleNames, styleSheet, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3, _results;
expando = this.generateUniqueString(); expando = this.generateUniqueString();
_ref = this.document.images; _ref = this.document.images;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -484,27 +520,28 @@ __reloader.Reloader = Reloader = (function() {
} }
} }
if (this.document.querySelectorAll) { if (this.document.querySelectorAll) {
for (_j = 0, _len2 = IMAGE_STYLES.length; _j < _len2; _j++) { for (_j = 0, _len1 = IMAGE_STYLES.length; _j < _len1; _j++) {
_ref2 = IMAGE_STYLES[_j], selector = _ref2.selector, styleNames = _ref2.styleNames; _ref1 = IMAGE_STYLES[_j], selector = _ref1.selector, styleNames = _ref1.styleNames;
_ref3 = this.document.querySelectorAll("[style*=" + selector + "]"); _ref2 = this.document.querySelectorAll("[style*=" + selector + "]");
for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) { for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
img = _ref3[_k]; img = _ref2[_k];
this.reloadStyleImages(img.style, styleNames, path, expando); this.reloadStyleImages(img.style, styleNames, path, expando);
} }
} }
} }
if (this.document.styleSheets) { if (this.document.styleSheets) {
_ref4 = this.document.styleSheets; _ref3 = this.document.styleSheets;
_results = []; _results = [];
for (_l = 0, _len4 = _ref4.length; _l < _len4; _l++) { for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
styleSheet = _ref4[_l]; styleSheet = _ref3[_l];
_results.push(this.reloadStylesheetImages(styleSheet, path, expando)); _results.push(this.reloadStylesheetImages(styleSheet, path, expando));
} }
return _results; return _results;
} }
}; };
Reloader.prototype.reloadStylesheetImages = function(styleSheet, path, expando) { Reloader.prototype.reloadStylesheetImages = function(styleSheet, path, expando) {
var rule, rules, styleNames, _i, _j, _len, _len2; var rule, rules, styleNames, _i, _j, _len, _len1;
try { try {
rules = styleSheet != null ? styleSheet.cssRules : void 0; rules = styleSheet != null ? styleSheet.cssRules : void 0;
} catch (e) { } catch (e) {
@ -520,7 +557,7 @@ __reloader.Reloader = Reloader = (function() {
this.reloadStylesheetImages(rule.styleSheet, path, expando); this.reloadStylesheetImages(rule.styleSheet, path, expando);
break; break;
case CSSRule.STYLE_RULE: case CSSRule.STYLE_RULE:
for (_j = 0, _len2 = IMAGE_STYLES.length; _j < _len2; _j++) { for (_j = 0, _len1 = IMAGE_STYLES.length; _j < _len1; _j++) {
styleNames = IMAGE_STYLES[_j].styleNames; styleNames = IMAGE_STYLES[_j].styleNames;
this.reloadStyleImages(rule.style, styleNames, path, expando); this.reloadStyleImages(rule.style, styleNames, path, expando);
} }
@ -530,27 +567,31 @@ __reloader.Reloader = Reloader = (function() {
} }
} }
}; };
Reloader.prototype.reloadStyleImages = function(style, styleNames, path, expando) { Reloader.prototype.reloadStyleImages = function(style, styleNames, path, expando) {
var newValue, styleName, value, _i, _len; var newValue, styleName, value, _i, _len,
_this = this;
for (_i = 0, _len = styleNames.length; _i < _len; _i++) { for (_i = 0, _len = styleNames.length; _i < _len; _i++) {
styleName = styleNames[_i]; styleName = styleNames[_i];
value = style[styleName]; value = style[styleName];
if (typeof value === 'string') { if (typeof value === 'string') {
newValue = value.replace(/\burl\s*\(([^)]*)\)/, __bind(function(match, src) { newValue = value.replace(/\burl\s*\(([^)]*)\)/, function(match, src) {
if (pathsMatch(path, pathFromUrl(src))) { if (pathsMatch(path, pathFromUrl(src))) {
return "url(" + (this.generateCacheBustUrl(src, expando)) + ")"; return "url(" + (_this.generateCacheBustUrl(src, expando)) + ")";
} else { } else {
return match; return match;
} }
}, this)); });
if (newValue !== value) { if (newValue !== value) {
style[styleName] = newValue; style[styleName] = newValue;
} }
} }
} }
}; };
Reloader.prototype.reloadStylesheet = function(path) { Reloader.prototype.reloadStylesheet = function(path) {
var imported, link, links, match, style, _i, _j, _k, _len, _len2, _len3, _ref; var imported, link, links, match, style, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1,
_this = this;
links = (function() { links = (function() {
var _i, _len, _ref, _results; var _i, _len, _ref, _results;
_ref = this.document.getElementsByTagName('link'); _ref = this.document.getElementsByTagName('link');
@ -571,40 +612,48 @@ __reloader.Reloader = Reloader = (function() {
this.collectImportedStylesheets(style, style.sheet, imported); this.collectImportedStylesheets(style, style.sheet, imported);
} }
} }
for (_j = 0, _len2 = links.length; _j < _len2; _j++) { for (_j = 0, _len1 = links.length; _j < _len1; _j++) {
link = links[_j]; link = links[_j];
this.collectImportedStylesheets(link, link.sheet, imported); this.collectImportedStylesheets(link, link.sheet, imported);
} }
if (this.window.StyleFix && this.document.querySelectorAll) {
_ref1 = this.document.querySelectorAll('style[data-href]');
for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
style = _ref1[_k];
links.push(style);
}
}
this.console.log("LiveReload found " + links.length + " LINKed stylesheets, " + imported.length + " @imported stylesheets"); this.console.log("LiveReload found " + links.length + " LINKed stylesheets, " + imported.length + " @imported stylesheets");
match = pickBestMatch(path, links.concat(imported), function(l) { match = pickBestMatch(path, links.concat(imported), function(l) {
return pathFromUrl(l.href); return pathFromUrl(_this.linkHref(l));
}); });
if (match) { if (match) {
if (match.object.rule) { if (match.object.rule) {
this.console.log("LiveReload is reloading imported stylesheet: " + match.object.href); this.console.log("LiveReload is reloading imported stylesheet: " + match.object.href);
this.reattachImportedRule(match.object); this.reattachImportedRule(match.object);
} else { } else {
this.console.log("LiveReload is reloading stylesheet: " + match.object.href); this.console.log("LiveReload is reloading stylesheet: " + (this.linkHref(match.object)));
this.reattachStylesheetLink(match.object); this.reattachStylesheetLink(match.object);
} }
} else { } else {
this.console.log("LiveReload will reload all stylesheets because path '" + path + "' did not match any specific one"); this.console.log("LiveReload will reload all stylesheets because path '" + path + "' did not match any specific one");
for (_k = 0, _len3 = links.length; _k < _len3; _k++) { for (_l = 0, _len3 = links.length; _l < _len3; _l++) {
link = links[_k]; link = links[_l];
this.reattachStylesheetLink(link); this.reattachStylesheetLink(link);
} }
} }
return true; return true;
}; };
Reloader.prototype.collectImportedStylesheets = function(link, styleSheet, result) { Reloader.prototype.collectImportedStylesheets = function(link, styleSheet, result) {
var index, rule, rules, _len; var index, rule, rules, _i, _len;
try { try {
rules = styleSheet != null ? styleSheet.cssRules : void 0; rules = styleSheet != null ? styleSheet.cssRules : void 0;
} catch (e) { } catch (e) {
} }
if (rules && rules.length) { if (rules && rules.length) {
for (index = 0, _len = rules.length; index < _len; index++) { for (index = _i = 0, _len = rules.length; _i < _len; index = ++_i) {
rule = rules[index]; rule = rules[index];
switch (rule.type) { switch (rule.type) {
case CSSRule.CHARSET_RULE: case CSSRule.CHARSET_RULE:
@ -624,29 +673,84 @@ __reloader.Reloader = Reloader = (function() {
} }
} }
}; };
Reloader.prototype.waitUntilCssLoads = function(clone, func) {
var callbackExecuted, executeCallback, poll,
_this = this;
callbackExecuted = false;
executeCallback = function() {
if (callbackExecuted) {
return;
}
callbackExecuted = true;
return func();
};
clone.onload = function() {
console.log("onload!");
_this.knownToSupportCssOnLoad = true;
return executeCallback();
};
if (!this.knownToSupportCssOnLoad) {
(poll = function() {
if (clone.sheet) {
console.log("polling!");
return executeCallback();
} else {
return _this.Timer.start(50, poll);
}
})();
}
return this.Timer.start(this.options.stylesheetReloadTimeout, executeCallback);
};
Reloader.prototype.linkHref = function(link) {
return link.href || link.getAttribute('data-href');
};
Reloader.prototype.reattachStylesheetLink = function(link) { Reloader.prototype.reattachStylesheetLink = function(link) {
var clone, parent, timer; var clone, parent,
_this = this;
if (link.__LiveReload_pendingRemoval) { if (link.__LiveReload_pendingRemoval) {
return; return;
} }
link.__LiveReload_pendingRemoval = true; link.__LiveReload_pendingRemoval = true;
if (link.tagName === 'STYLE') {
clone = this.document.createElement('link');
clone.rel = 'stylesheet';
clone.media = link.media;
clone.disabled = link.disabled;
} else {
clone = link.cloneNode(false); clone = link.cloneNode(false);
clone.href = this.generateCacheBustUrl(link.href); }
clone.href = this.generateCacheBustUrl(this.linkHref(link));
parent = link.parentNode; parent = link.parentNode;
if (parent.lastChild === link) { if (parent.lastChild === link) {
parent.appendChild(clone); parent.appendChild(clone);
} else { } else {
parent.insertBefore(clone, link.nextSibling); parent.insertBefore(clone, link.nextSibling);
} }
timer = new this.Timer(function() { return this.waitUntilCssLoads(clone, function() {
if (link.parentNode) { var additionalWaitingTime;
return link.parentNode.removeChild(link); if (/AppleWebKit/.test(navigator.userAgent)) {
additionalWaitingTime = 5;
} else {
additionalWaitingTime = 200;
} }
return _this.Timer.start(additionalWaitingTime, function() {
var _ref;
if (!link.parentNode) {
return;
}
link.parentNode.removeChild(link);
clone.onreadystatechange = null;
return (_ref = _this.window.StyleFix) != null ? _ref.link(clone) : void 0;
});
}); });
return timer.start(this.stylesheetGracePeriod);
}; };
Reloader.prototype.reattachImportedRule = function(_arg) { Reloader.prototype.reattachImportedRule = function(_arg) {
var href, index, link, media, newRule, parent, rule, tempLink; var href, index, link, media, newRule, parent, rule, tempLink,
_this = this;
rule = _arg.rule, index = _arg.index, link = _arg.link; rule = _arg.rule, index = _arg.index, link = _arg.link;
parent = rule.parentStyleSheet; parent = rule.parentStyleSheet;
href = this.generateCacheBustUrl(rule.href); href = this.generateCacheBustUrl(rule.href);
@ -660,7 +764,7 @@ __reloader.Reloader = Reloader = (function() {
if (link.parentNode) { if (link.parentNode) {
link.parentNode.insertBefore(tempLink, link); link.parentNode.insertBefore(tempLink, link);
} }
return this.Timer.start(this.importCacheWaitPeriod, __bind(function() { return this.Timer.start(this.importCacheWaitPeriod, function() {
if (tempLink.parentNode) { if (tempLink.parentNode) {
tempLink.parentNode.removeChild(tempLink); tempLink.parentNode.removeChild(tempLink);
} }
@ -671,24 +775,31 @@ __reloader.Reloader = Reloader = (function() {
parent.deleteRule(index + 1); parent.deleteRule(index + 1);
rule = parent.cssRules[index]; rule = parent.cssRules[index];
rule.__LiveReload_newHref = href; rule.__LiveReload_newHref = href;
return this.Timer.start(this.importCacheWaitPeriod, __bind(function() { return _this.Timer.start(_this.importCacheWaitPeriod, function() {
if (rule.__LiveReload_newHref !== href) { if (rule.__LiveReload_newHref !== href) {
return; return;
} }
parent.insertRule(newRule, index); parent.insertRule(newRule, index);
return parent.deleteRule(index + 1); return parent.deleteRule(index + 1);
}, this)); });
}, this)); });
}; };
Reloader.prototype.generateUniqueString = function() { Reloader.prototype.generateUniqueString = function() {
return 'livereload=' + Date.now(); return 'livereload=' + Date.now();
}; };
Reloader.prototype.generateCacheBustUrl = function(url, expando) { Reloader.prototype.generateCacheBustUrl = function(url, expando) {
var hash, oldParams, params, _ref; var hash, oldParams, params, _ref;
if (expando == null) { if (expando == null) {
expando = this.generateUniqueString(); expando = this.generateUniqueString();
} }
_ref = splitUrl(url), url = _ref.url, hash = _ref.hash, oldParams = _ref.params; _ref = splitUrl(url), url = _ref.url, hash = _ref.hash, oldParams = _ref.params;
if (this.options.overrideURL) {
if (url.indexOf(this.options.serverURL) < 0) {
url = this.options.serverURL + this.options.overrideURL + "?url=" + encodeURIComponent(url);
}
}
params = oldParams.replace(/(\?|&)livereload=(\d+)/, function(match, sep) { params = oldParams.replace(/(\?|&)livereload=(\d+)/, function(match, sep) {
return "" + sep + expando; return "" + sep + expando;
}); });
@ -701,23 +812,33 @@ __reloader.Reloader = Reloader = (function() {
} }
return url + params + hash; return url + params + hash;
}; };
return Reloader; return Reloader;
})(); })();
}).call(this);
// livereload // livereload
var Connector, LiveReload, Options, Reloader, Timer; var Connector, LiveReload, Options, Reloader, Timer;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Connector = __connector.Connector; Connector = __connector.Connector;
Timer = __timer.Timer; Timer = __timer.Timer;
Options = __options.Options; Options = __options.Options;
Reloader = __reloader.Reloader; Reloader = __reloader.Reloader;
__livereload.LiveReload = LiveReload = (function() { __livereload.LiveReload = LiveReload = (function() {
function LiveReload(window) { function LiveReload(window) {
var _this = this;
this.window = window; this.window = window;
this.listeners = {}; this.listeners = {};
this.plugins = []; this.plugins = [];
this.pluginIdentifiers = {}; this.pluginIdentifiers = {};
this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.console : { this.console = this.window.location.href.match(/LR-verbose/) && this.window.console && this.window.console.log && this.window.console.error ? this.window.console : {
log: function() {}, log: function() {},
error: function() {} error: function() {}
}; };
@ -731,87 +852,95 @@ __livereload.LiveReload = LiveReload = (function() {
} }
this.reloader = new Reloader(this.window, this.console, Timer); this.reloader = new Reloader(this.window, this.console, Timer);
this.connector = new Connector(this.options, this.WebSocket, Timer, { this.connector = new Connector(this.options, this.WebSocket, Timer, {
connecting: __bind(function() {}, this), connecting: function() {},
socketConnected: __bind(function() {}, this), socketConnected: function() {},
connected: __bind(function(protocol) { connected: function(protocol) {
var _base; var _base;
if (typeof (_base = this.listeners).connect === "function") { if (typeof (_base = _this.listeners).connect === "function") {
_base.connect(); _base.connect();
} }
this.log("LiveReload is connected to " + this.options.host + ":" + this.options.port + " (protocol v" + protocol + ")."); _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ").");
return this.analyze(); return _this.analyze();
}, this), },
error: __bind(function(e) { error: function(e) {
if (e instanceof ProtocolError) { if (e instanceof ProtocolError) {
return console.log("" + e.message + "."); return console.log("" + e.message + ".");
} else { } else {
return console.log("LiveReload internal error: " + e.message); return console.log("LiveReload internal error: " + e.message);
} }
}, this), },
disconnected: __bind(function(reason, nextDelay) { disconnected: function(reason, nextDelay) {
var _base; var _base;
if (typeof (_base = this.listeners).disconnect === "function") { if (typeof (_base = _this.listeners).disconnect === "function") {
_base.disconnect(); _base.disconnect();
} }
switch (reason) { switch (reason) {
case 'cannot-connect': case 'cannot-connect':
return this.log("LiveReload cannot connect to " + this.options.host + ":" + this.options.port + ", will retry in " + nextDelay + " sec."); return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + ", will retry in " + nextDelay + " sec.");
case 'broken': case 'broken':
return this.log("LiveReload disconnected from " + this.options.host + ":" + this.options.port + ", reconnecting in " + nextDelay + " sec."); return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + ", reconnecting in " + nextDelay + " sec.");
case 'handshake-timeout': case 'handshake-timeout':
return this.log("LiveReload cannot connect to " + this.options.host + ":" + this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec."); return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec.");
case 'handshake-failed': case 'handshake-failed':
return this.log("LiveReload cannot connect to " + this.options.host + ":" + this.options.port + " (handshake failed), will retry in " + nextDelay + " sec."); return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake failed), will retry in " + nextDelay + " sec.");
case 'manual': case 'manual':
break; break;
case 'error': case 'error':
break; break;
default: default:
return this.log("LiveReload disconnected from " + this.options.host + ":" + this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec."); return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec.");
} }
}, this), },
message: __bind(function(message) { message: function(message) {
switch (message.command) { switch (message.command) {
case 'reload': case 'reload':
return this.performReload(message); return _this.performReload(message);
case 'alert': case 'alert':
return this.performAlert(message); return _this.performAlert(message);
}
} }
}, this)
}); });
} }
LiveReload.prototype.on = function(eventName, handler) { LiveReload.prototype.on = function(eventName, handler) {
return this.listeners[eventName] = handler; return this.listeners[eventName] = handler;
}; };
LiveReload.prototype.log = function(message) { LiveReload.prototype.log = function(message) {
return this.console.log("" + message); return this.console.log("" + message);
}; };
LiveReload.prototype.performReload = function(message) { LiveReload.prototype.performReload = function(message) {
var _ref, _ref2; var _ref, _ref2;
this.log("LiveReload received reload request for " + message.path + "."); this.log("LiveReload received reload request for " + message.path + ".");
return this.reloader.reload(message.path, { return this.reloader.reload(message.path, {
liveCSS: (_ref = message.liveCSS) != null ? _ref : true, liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
liveImg: (_ref2 = message.liveImg) != null ? _ref2 : true, liveImg: (_ref2 = message.liveImg) != null ? _ref2 : true,
originalPath: message.originalPath || '' originalPath: message.originalPath || '',
overrideURL: message.overrideURL || '',
serverURL: "http://" + this.options.host + ":" + this.options.port
}); });
}; };
LiveReload.prototype.performAlert = function(message) { LiveReload.prototype.performAlert = function(message) {
return alert(message.message); return alert(message.message);
}; };
LiveReload.prototype.shutDown = function() { LiveReload.prototype.shutDown = function() {
var _base; var _base;
this.connector.disconnect(); this.connector.disconnect();
this.log("LiveReload disconnected."); this.log("LiveReload disconnected.");
return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0; return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
}; };
LiveReload.prototype.hasPlugin = function(identifier) { LiveReload.prototype.hasPlugin = function(identifier) {
return !!this.pluginIdentifiers[identifier]; return !!this.pluginIdentifiers[identifier];
}; };
LiveReload.prototype.addPlugin = function(pluginClass) { LiveReload.prototype.addPlugin = function(pluginClass) {
var plugin; var plugin;
if (this.hasPlugin(pluginClass.identifier)) { var _this = this;
return; if (this.hasPlugin(pluginClass.identifier)) return;
}
this.pluginIdentifiers[pluginClass.identifier] = true; this.pluginIdentifiers[pluginClass.identifier] = true;
plugin = new pluginClass(this.window, { plugin = new pluginClass(this.window, {
_livereload: this, _livereload: this,
@ -819,18 +948,17 @@ __livereload.LiveReload = LiveReload = (function() {
_connector: this.connector, _connector: this.connector,
console: this.console, console: this.console,
Timer: Timer, Timer: Timer,
generateCacheBustUrl: __bind(function(url) { generateCacheBustUrl: function(url) {
return this.reloader.generateCacheBustUrl(url); return _this.reloader.generateCacheBustUrl(url);
}, this) }
}); });
this.plugins.push(plugin); this.plugins.push(plugin);
this.reloader.addPlugin(plugin); this.reloader.addPlugin(plugin);
}; };
LiveReload.prototype.analyze = function() { LiveReload.prototype.analyze = function() {
var plugin, pluginData, pluginsData, _i, _len, _ref; var plugin, pluginData, pluginsData, _i, _len, _ref;
if (!(this.connector.protocol >= 7)) { if (!(this.connector.protocol >= 7)) return;
return;
}
pluginsData = {}; pluginsData = {};
_ref = this.plugins; _ref = this.plugins;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -844,7 +972,9 @@ __livereload.LiveReload = LiveReload = (function() {
url: this.window.location.href url: this.window.location.href
}); });
}; };
return LiveReload; return LiveReload;
})(); })();
// less // less
@ -901,13 +1031,12 @@ __less = LessPlugin = (function() {
})(); })();
// startup // startup
var CustomEvents, LiveReload, k, v; var CustomEvents, LiveReload, k;
CustomEvents = __customevents; CustomEvents = __customevents;
LiveReload = window.LiveReload = new (__livereload.LiveReload)(window); LiveReload = window.LiveReload = new (__livereload.LiveReload)(window);
for (k in window) { for (k in window) {
v = window[k];
if (k.match(/^LiveReloadPlugin/)) { if (k.match(/^LiveReloadPlugin/)) {
LiveReload.addPlugin(v); LiveReload.addPlugin(window[k]);
} }
} }
LiveReload.addPlugin(__less); LiveReload.addPlugin(__less);

View File

@ -1,6 +1,6 @@
require "rack/livereload" require "rack/livereload"
class Rack::LiveReload class Rack::LiveReload
VERSION = '0.3.6' VERSION = '0.3.7'
end end