| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | (function() { |
| 2 | var Connector, LiveReload, Options, ProtocolError, Reloader, Timer, |
| 3 | hasProp = {}.hasOwnProperty; |
| 4 | |
| 5 | Connector = require('./connector').Connector; |
| 6 | |
| 7 | Timer = require('./timer').Timer; |
| 8 | |
| 9 | Options = require('./options').Options; |
| 10 | |
| 11 | Reloader = require('./reloader').Reloader; |
| 12 | |
| 13 | ProtocolError = require('./protocol').ProtocolError; |
| 14 | |
| 15 | exports.LiveReload = LiveReload = (function() { |
| 16 | function LiveReload(window1) { |
| 17 | var k, ref, v; |
| 18 | this.window = window1; |
| 19 | this.listeners = {}; |
| 20 | this.plugins = []; |
| 21 | this.pluginIdentifiers = {}; |
| 22 | this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.location.href.match(/LR-verbose/) ? this.window.console : { |
| 23 | log: function() {}, |
| 24 | error: this.window.console.error.bind(this.window.console) |
| 25 | } : { |
| 26 | log: function() {}, |
| 27 | error: function() {} |
| 28 | }; |
| 29 | if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) { |
| 30 | this.console.error("LiveReload disabled because the browser does not seem to support web sockets"); |
| 31 | return; |
| 32 | } |
| 33 | if ('LiveReloadOptions' in window) { |
| 34 | this.options = new Options(); |
| 35 | ref = window['LiveReloadOptions']; |
| 36 | for (k in ref) { |
| 37 | if (!hasProp.call(ref, k)) continue; |
| 38 | v = ref[k]; |
| 39 | this.options.set(k, v); |
| 40 | } |
| 41 | } else { |
| 42 | this.options = Options.extract(this.window.document); |
| 43 | if (!this.options) { |
| 44 | this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag"); |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | this.reloader = new Reloader(this.window, this.console, Timer); |
| 49 | this.connector = new Connector(this.options, this.WebSocket, Timer, { |
| 50 | connecting: (function(_this) { |
| 51 | return function() {}; |
| 52 | })(this), |
| 53 | socketConnected: (function(_this) { |
| 54 | return function() {}; |
| 55 | })(this), |
| 56 | connected: (function(_this) { |
| 57 | return function(protocol) { |
| 58 | var base; |
| 59 | if (typeof (base = _this.listeners).connect === "function") { |
| 60 | base.connect(); |
| 61 | } |
| 62 | _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ")."); |
| 63 | return _this.analyze(); |
| 64 | }; |
| 65 | })(this), |
| 66 | error: (function(_this) { |
| 67 | return function(e) { |
| 68 | if (e instanceof ProtocolError) { |
| 69 | if (typeof console !== "undefined" && console !== null) { |
| 70 | return console.log(e.message + "."); |
| 71 | } |
| 72 | } else { |
| 73 | if (typeof console !== "undefined" && console !== null) { |
| 74 | return console.log("LiveReload internal error: " + e.message); |
| 75 | } |
| 76 | } |
| 77 | }; |
| 78 | })(this), |
| 79 | disconnected: (function(_this) { |
| 80 | return function(reason, nextDelay) { |
| 81 | var base; |
| 82 | if (typeof (base = _this.listeners).disconnect === "function") { |
| 83 | base.disconnect(); |
| 84 | } |
| 85 | switch (reason) { |
| 86 | case 'cannot-connect': |
| 87 | return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + ", will retry in " + nextDelay + " sec."); |
| 88 | case 'broken': |
| 89 | return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + ", reconnecting in " + nextDelay + " sec."); |
| 90 | case 'handshake-timeout': |
| 91 | return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec."); |
| 92 | case 'handshake-failed': |
| 93 | return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake failed), will retry in " + nextDelay + " sec."); |
| 94 | case 'manual': |
| 95 | break; |
| 96 | case 'error': |
| 97 | break; |
| 98 | default: |
| 99 | return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec."); |
| 100 | } |
| 101 | }; |
| 102 | })(this), |
| 103 | message: (function(_this) { |
| 104 | return function(message) { |
| 105 | switch (message.command) { |
| 106 | case 'reload': |
| 107 | return _this.performReload(message); |
| 108 | case 'alert': |
| 109 | return _this.performAlert(message); |
| 110 | } |
| 111 | }; |
| 112 | })(this) |
| 113 | }); |
| 114 | this.initialized = true; |
| 115 | } |
| 116 | |
| 117 | LiveReload.prototype.on = function(eventName, handler) { |
| 118 | return this.listeners[eventName] = handler; |
| 119 | }; |
| 120 | |
| 121 | LiveReload.prototype.log = function(message) { |
| 122 | return this.console.log("" + message); |
| 123 | }; |
| 124 | |
| 125 | LiveReload.prototype.performReload = function(message) { |
| 126 | var ref, ref1, ref2; |
| 127 | this.log("LiveReload received reload request: " + (JSON.stringify(message, null, 2))); |
| 128 | return this.reloader.reload(message.path, { |
| 129 | liveCSS: (ref = message.liveCSS) != null ? ref : true, |
| 130 | liveImg: (ref1 = message.liveImg) != null ? ref1 : true, |
| 131 | reloadMissingCSS: (ref2 = message.reloadMissingCSS) != null ? ref2 : true, |
| 132 | originalPath: message.originalPath || '', |
| 133 | overrideURL: message.overrideURL || '', |
| 134 | serverURL: "http://" + this.options.host + ":" + this.options.port |
| 135 | }); |
| 136 | }; |
| 137 | |
| 138 | LiveReload.prototype.performAlert = function(message) { |
| 139 | return alert(message.message); |
| 140 | }; |
| 141 | |
| 142 | LiveReload.prototype.shutDown = function() { |
| 143 | var base; |
| 144 | if (!this.initialized) { |
| 145 | return; |
| 146 | } |
| 147 | this.connector.disconnect(); |
| 148 | this.log("LiveReload disconnected."); |
| 149 | return typeof (base = this.listeners).shutdown === "function" ? base.shutdown() : void 0; |
| 150 | }; |
| 151 | |
| 152 | LiveReload.prototype.hasPlugin = function(identifier) { |
| 153 | return !!this.pluginIdentifiers[identifier]; |
| 154 | }; |
| 155 | |
| 156 | LiveReload.prototype.addPlugin = function(pluginClass) { |
| 157 | var plugin; |
| 158 | if (!this.initialized) { |
| 159 | return; |
| 160 | } |
| 161 | if (this.hasPlugin(pluginClass.identifier)) { |
| 162 | return; |
| 163 | } |
| 164 | this.pluginIdentifiers[pluginClass.identifier] = true; |
| 165 | plugin = new pluginClass(this.window, { |
| 166 | _livereload: this, |
| 167 | _reloader: this.reloader, |
| 168 | _connector: this.connector, |
| 169 | console: this.console, |
| 170 | Timer: Timer, |
| 171 | generateCacheBustUrl: (function(_this) { |
| 172 | return function(url) { |
| 173 | return _this.reloader.generateCacheBustUrl(url); |
| 174 | }; |
| 175 | })(this) |
| 176 | }); |
| 177 | this.plugins.push(plugin); |
| 178 | this.reloader.addPlugin(plugin); |
| 179 | }; |
| 180 | |
| 181 | LiveReload.prototype.analyze = function() { |
| 182 | var i, len, plugin, pluginData, pluginsData, ref; |
| 183 | if (!this.initialized) { |
| 184 | return; |
| 185 | } |
| 186 | if (!(this.connector.protocol >= 7)) { |
| 187 | return; |
| 188 | } |
| 189 | pluginsData = {}; |
| 190 | ref = this.plugins; |
| 191 | for (i = 0, len = ref.length; i < len; i++) { |
| 192 | plugin = ref[i]; |
| 193 | pluginsData[plugin.constructor.identifier] = pluginData = (typeof plugin.analyze === "function" ? plugin.analyze() : void 0) || {}; |
| 194 | pluginData.version = plugin.constructor.version; |
| 195 | } |
| 196 | this.connector.sendCommand({ |
| 197 | command: 'info', |
| 198 | plugins: pluginsData, |
| 199 | url: this.window.location.href |
| 200 | }); |
| 201 | }; |
| 202 | |
| 203 | return LiveReload; |
| 204 | |
| 205 | })(); |
| 206 | |
| 207 | }).call(this); |