From 380fa8f905ea74eaa133f47ad79b2118d6bd7164 Mon Sep 17 00:00:00 2001 From: Andrey Tarantsov Date: Sat, 4 Feb 2012 09:35:47 +0700 Subject: [PATCH] Update livereload.js --- dist/livereload.js | 205 ++++++++++++++++++++++++--------------------- 1 file changed, 108 insertions(+), 97 deletions(-) diff --git a/dist/livereload.js b/dist/livereload.js index 327e940..59abc68 100644 --- a/dist/livereload.js +++ b/dist/livereload.js @@ -124,11 +124,15 @@ __protocol.Parser = Parser = (function() { // connector 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; -Version = '2.0.4'; + +Version = '2.0.5'; + __connector.Connector = Connector = (function() { + function Connector(options, WebSocket, Timer, handlers) { + var _this = this; this.options = options; this.WebSocket = WebSocket; this.Timer = Timer; @@ -138,96 +142,91 @@ __connector.Connector = Connector = (function() { this._connectionDesired = false; this.protocol = 0; this.protocolParser = new Parser({ - connected: __bind(function(protocol) { - this.protocol = protocol; - this._handshakeTimeout.stop(); - this._nextDelay = this.options.mindelay; - this._disconnectionReason = 'broken'; - return this.handlers.connected(protocol); - }, this), - error: __bind(function(e) { - this.handlers.error(e); - return this._closeOnError(); - }, this), - message: __bind(function(message) { - return this.handlers.message(message); - }, this) - }); - this._handshakeTimeout = new Timer(__bind(function() { - if (!this._isSocketConnected()) { - return; - } - this._disconnectionReason = 'handshake-timeout'; - return this.socket.close(); - }, this)); - this._reconnectTimer = new Timer(__bind(function() { - if (!this._connectionDesired) { - return; + connected: function(protocol) { + _this.protocol = protocol; + _this._handshakeTimeout.stop(); + _this._nextDelay = _this.options.mindelay; + _this._disconnectionReason = 'broken'; + return _this.handlers.connected(protocol); + }, + error: function(e) { + _this.handlers.error(e); + return _this._closeOnError(); + }, + message: function(message) { + return _this.handlers.message(message); } - return this.connect(); - }, this)); + }); + this._handshakeTimeout = new Timer(function() { + if (!_this._isSocketConnected()) return; + _this._disconnectionReason = 'handshake-timeout'; + return _this.socket.close(); + }); + this._reconnectTimer = new Timer(function() { + if (!_this._connectionDesired) return; + return _this.connect(); + }); this.connect(); } + Connector.prototype._isSocketConnected = function() { return this.socket && this.socket.readyState === this.WebSocket.OPEN; }; + Connector.prototype.connect = function() { + var _this = this; this._connectionDesired = true; - if (this._isSocketConnected()) { - return; - } - if (this._reconnectTimer) { - clearTimeout(this._reconnectTimer); - } + if (this._isSocketConnected()) return; + if (this._reconnectTimer) clearTimeout(this._reconnectTimer); this._disconnectionReason = 'cannot-connect'; this.protocolParser.reset(); this.handlers.connecting(); this.socket = new this.WebSocket(this._uri); - this.socket.onopen = __bind(function(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.onopen = function(e) { + return _this._onopen(e); + }; + 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() { this._connectionDesired = false; this._reconnectTimer.stop(); - if (!this._isSocketConnected()) { - return; - } + if (!this._isSocketConnected()) return; this._disconnectionReason = 'manual'; return this.socket.close(); }; + Connector.prototype._scheduleReconnection = function() { - if (!this._connectionDesired) { - return; - } + if (!this._connectionDesired) return; if (!this._reconnectTimer.running) { this._reconnectTimer.start(this._nextDelay); return this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2); } }; + Connector.prototype.sendCommand = function(command) { - if (this.protocol == null) { - return; - } + if (this.protocol == null) return; return this._sendCommand(command); }; + Connector.prototype._sendCommand = function(command) { return this.socket.send(JSON.stringify(command)); }; + Connector.prototype._closeOnError = function() { this._handshakeTimeout.stop(); this._disconnectionReason = 'error'; return this.socket.close(); }; + Connector.prototype._onopen = function(e) { var hello; this.handlers.socketConnected(); @@ -237,28 +236,27 @@ __connector.Connector = Connector = (function() { protocols: [PROTOCOL_6, PROTOCOL_7] }; hello.ver = Version; - if (this.options.ext) { - hello.ext = this.options.ext; - } - if (this.options.extver) { - hello.extver = this.options.extver; - } - if (this.options.snipver) { - hello.snipver = this.options.snipver; - } + if (this.options.ext) hello.ext = this.options.ext; + if (this.options.extver) hello.extver = this.options.extver; + if (this.options.snipver) hello.snipver = this.options.snipver; this._sendCommand(hello); return this._handshakeTimeout.start(this.options.handshake_timeout); }; + Connector.prototype._onclose = function(e) { this.protocol = 0; this.handlers.disconnected(this._disconnectionReason, this._nextDelay); return this._scheduleReconnection(); }; + Connector.prototype._onerror = function(e) {}; + Connector.prototype._onmessage = function(e) { return this.protocolParser.process(e.data); }; + return Connector; + })(); // timer @@ -706,18 +704,24 @@ __reloader.Reloader = Reloader = (function() { // livereload var Connector, LiveReload, Options, Reloader, Timer; -var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + Connector = __connector.Connector; + Timer = __timer.Timer; + Options = __options.Options; + Reloader = __reloader.Reloader; + __livereload.LiveReload = LiveReload = (function() { + function LiveReload(window) { + var _this = this; this.window = window; this.listeners = {}; this.plugins = []; 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() {}, error: function() {} }; @@ -731,61 +735,64 @@ __livereload.LiveReload = LiveReload = (function() { } this.reloader = new Reloader(this.window, this.console, Timer); this.connector = new Connector(this.options, this.WebSocket, Timer, { - connecting: __bind(function() {}, this), - socketConnected: __bind(function() {}, this), - connected: __bind(function(protocol) { + connecting: function() {}, + socketConnected: function() {}, + connected: function(protocol) { var _base; - if (typeof (_base = this.listeners).connect === "function") { + if (typeof (_base = _this.listeners).connect === "function") { _base.connect(); } - this.log("LiveReload is connected to " + this.options.host + ":" + this.options.port + " (protocol v" + protocol + ")."); - return this.analyze(); - }, this), - error: __bind(function(e) { + _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ")."); + return _this.analyze(); + }, + error: function(e) { if (e instanceof ProtocolError) { return console.log("" + e.message + "."); } else { return console.log("LiveReload internal error: " + e.message); } - }, this), - disconnected: __bind(function(reason, nextDelay) { + }, + disconnected: function(reason, nextDelay) { var _base; - if (typeof (_base = this.listeners).disconnect === "function") { + if (typeof (_base = _this.listeners).disconnect === "function") { _base.disconnect(); } switch (reason) { 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': - 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': - 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': - 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': break; case 'error': break; 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) { case 'reload': - return this.performReload(message); + return _this.performReload(message); case 'alert': - return this.performAlert(message); + return _this.performAlert(message); } - }, this) + } }); } + LiveReload.prototype.on = function(eventName, handler) { return this.listeners[eventName] = handler; }; + LiveReload.prototype.log = function(message) { return this.console.log("" + message); }; + LiveReload.prototype.performReload = function(message) { var _ref, _ref2; this.log("LiveReload received reload request for " + message.path + "."); @@ -795,23 +802,26 @@ __livereload.LiveReload = LiveReload = (function() { originalPath: message.originalPath || '' }); }; + LiveReload.prototype.performAlert = function(message) { return alert(message.message); }; + LiveReload.prototype.shutDown = function() { var _base; this.connector.disconnect(); this.log("LiveReload disconnected."); return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0; }; + LiveReload.prototype.hasPlugin = function(identifier) { return !!this.pluginIdentifiers[identifier]; }; + LiveReload.prototype.addPlugin = function(pluginClass) { var plugin; - if (this.hasPlugin(pluginClass.identifier)) { - return; - } + var _this = this; + if (this.hasPlugin(pluginClass.identifier)) return; this.pluginIdentifiers[pluginClass.identifier] = true; plugin = new pluginClass(this.window, { _livereload: this, @@ -819,18 +829,17 @@ __livereload.LiveReload = LiveReload = (function() { _connector: this.connector, console: this.console, Timer: Timer, - generateCacheBustUrl: __bind(function(url) { - return this.reloader.generateCacheBustUrl(url); - }, this) + generateCacheBustUrl: function(url) { + return _this.reloader.generateCacheBustUrl(url); + } }); this.plugins.push(plugin); this.reloader.addPlugin(plugin); }; + LiveReload.prototype.analyze = function() { var plugin, pluginData, pluginsData, _i, _len, _ref; - if (!(this.connector.protocol >= 7)) { - return; - } + if (!(this.connector.protocol >= 7)) return; pluginsData = {}; _ref = this.plugins; for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -844,7 +853,9 @@ __livereload.LiveReload = LiveReload = (function() { url: this.window.location.href }); }; + return LiveReload; + })(); // less