2 var Connector, LiveReload, Options, Reloader, Timer,
3 __hasProp = {}.hasOwnProperty;
5 Connector = require('./connector').Connector;
7 Timer = require('./timer').Timer;
9 Options = require('./options').Options;
11 Reloader = require('./reloader').Reloader;
13 exports.LiveReload = LiveReload = (function() {
14 function LiveReload(window) {
19 this.pluginIdentifiers = {};
20 this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.location.href.match(/LR-verbose/) ? this.window.console : {
22 error: this.window.console.error.bind(this.window.console)
27 if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) {
28 this.console.error("LiveReload disabled because the browser does not seem to support web sockets");
31 if ('LiveReloadOptions' in window) {
32 this.options = new Options();
33 _ref = window['LiveReloadOptions'];
35 if (!__hasProp.call(_ref, k)) continue;
37 this.options.set(k, v);
40 this.options = Options.extract(this.window.document);
42 this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag");
46 this.reloader = new Reloader(this.window, this.console, Timer);
47 this.connector = new Connector(this.options, this.WebSocket, Timer, {
48 connecting: (function(_this) {
51 socketConnected: (function(_this) {
54 connected: (function(_this) {
55 return function(protocol) {
57 if (typeof (_base = _this.listeners).connect === "function") {
60 _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ").");
61 return _this.analyze();
64 error: (function(_this) {
66 if (e instanceof ProtocolError) {
67 if (typeof console !== "undefined" && console !== null) {
68 return console.log("" + e.message + ".");
71 if (typeof console !== "undefined" && console !== null) {
72 return console.log("LiveReload internal error: " + e.message);
77 disconnected: (function(_this) {
78 return function(reason, nextDelay) {
80 if (typeof (_base = _this.listeners).disconnect === "function") {
84 case 'cannot-connect':
85 return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + ", will retry in " + nextDelay + " sec.");
87 return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + ", reconnecting in " + nextDelay + " sec.");
88 case 'handshake-timeout':
89 return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec.");
90 case 'handshake-failed':
91 return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake failed), will retry in " + nextDelay + " sec.");
97 return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec.");
101 message: (function(_this) {
102 return function(message) {
103 switch (message.command) {
105 return _this.performReload(message);
107 return _this.performAlert(message);
112 this.initialized = true;
115 LiveReload.prototype.on = function(eventName, handler) {
116 return this.listeners[eventName] = handler;
119 LiveReload.prototype.log = function(message) {
120 return this.console.log("" + message);
123 LiveReload.prototype.performReload = function(message) {
125 this.log("LiveReload received reload request: " + (JSON.stringify(message, null, 2)));
126 return this.reloader.reload(message.path, {
127 liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
128 liveImg: (_ref1 = message.liveImg) != null ? _ref1 : true,
129 originalPath: message.originalPath || '',
130 overrideURL: message.overrideURL || '',
131 serverURL: "http://" + this.options.host + ":" + this.options.port
135 LiveReload.prototype.performAlert = function(message) {
136 return alert(message.message);
139 LiveReload.prototype.shutDown = function() {
141 if (!this.initialized) {
144 this.connector.disconnect();
145 this.log("LiveReload disconnected.");
146 return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
149 LiveReload.prototype.hasPlugin = function(identifier) {
150 return !!this.pluginIdentifiers[identifier];
153 LiveReload.prototype.addPlugin = function(pluginClass) {
155 if (!this.initialized) {
158 if (this.hasPlugin(pluginClass.identifier)) {
161 this.pluginIdentifiers[pluginClass.identifier] = true;
162 plugin = new pluginClass(this.window, {
164 _reloader: this.reloader,
165 _connector: this.connector,
166 console: this.console,
168 generateCacheBustUrl: (function(_this) {
169 return function(url) {
170 return _this.reloader.generateCacheBustUrl(url);
174 this.plugins.push(plugin);
175 this.reloader.addPlugin(plugin);
178 LiveReload.prototype.analyze = function() {
179 var plugin, pluginData, pluginsData, _i, _len, _ref;
180 if (!this.initialized) {
183 if (!(this.connector.protocol >= 7)) {
188 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
190 pluginsData[plugin.constructor.identifier] = pluginData = (typeof plugin.analyze === "function" ? plugin.analyze() : void 0) || {};
191 pluginData.version = plugin.constructor.version;
193 this.connector.sendCommand({
195 plugins: pluginsData,
196 url: this.window.location.href