From f118bc9cd5f7c26d78c1bd0d5c2979ec2927f406 Mon Sep 17 00:00:00 2001 From: smhg Date: Mon, 27 Jul 2020 20:51:25 +0200 Subject: [PATCH] improve code readability --- src/connector.js | 2 + src/customevents.js | 11 ++-- src/less.js | 3 ++ src/livereload.js | 35 ++++++++----- src/options.js | 11 ++-- src/protocol.js | 20 ++++--- src/reloader.js | 95 +++++++++++++++++++++++---------- test/reloader_test.js | 119 +++++++++++++++++++++++++++++------------- version-bower.js | 6 ++- 9 files changed, 211 insertions(+), 91 deletions(-) diff --git a/src/connector.js b/src/connector.js index 5924e61..8f1ce5e 100644 --- a/src/connector.js +++ b/src/connector.js @@ -121,6 +121,7 @@ class Connector { _closeOnError () { this._handshakeTimeout.stop(); this._disconnectionReason = 'error'; + return this.socket.close(); } @@ -130,6 +131,7 @@ class Connector { // start handshake const hello = { command: 'hello', protocols: [PROTOCOL_6, PROTOCOL_7] }; + hello.ver = VERSION; if (this.options.ext) { diff --git a/src/customevents.js b/src/customevents.js index ab6fa71..9bf9582 100644 --- a/src/customevents.js +++ b/src/customevents.js @@ -2,21 +2,26 @@ const CustomEvents = { bind (element, eventName, handler) { if (element.addEventListener) { return element.addEventListener(eventName, handler, false); - } else if (element.attachEvent) { + } + + if (element.attachEvent) { element[eventName] = 1; + return element.attachEvent('onpropertychange', function (event) { if (event.propertyName === eventName) { return handler(); } }); - } else { - throw new Error(`Attempt to attach custom event ${eventName} to something which isn't a DOMElement`); } + + throw new Error(`Attempt to attach custom event ${eventName} to something which isn't a DOMElement`); }, fire (element, eventName) { if (element.addEventListener) { const event = document.createEvent('HTMLEvents'); + event.initEvent(eventName, true, true); + return document.dispatchEvent(event); } else if (element.attachEvent) { if (element[eventName]) { diff --git a/src/less.js b/src/less.js index dbbed14..7c46328 100644 --- a/src/less.js +++ b/src/less.js @@ -20,13 +20,16 @@ class LessPlugin { reloadLess (path) { let link; + const links = ((() => { const result = []; + for (link of Array.from(document.getElementsByTagName('link'))) { if ((link.href && link.rel.match(/^stylesheet\/less$/i)) || (link.rel.match(/stylesheet/i) && link.type.match(/^text\/(x-)?less$/i))) { result.push(link); } } + return result; })()); diff --git a/src/livereload.js b/src/livereload.js index e58badb..690fd12 100644 --- a/src/livereload.js +++ b/src/livereload.js @@ -29,20 +29,25 @@ class LiveReload { // i can haz sockets? if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) { this.console.error('LiveReload disabled because the browser does not seem to support web sockets'); + return; } // i can haz options? if ('LiveReloadOptions' in window) { this.options = new Options(); + for (const k of Object.keys(window.LiveReloadOptions || {})) { const v = window.LiveReloadOptions[k]; + this.options.set(k, v); } } else { this.options = Options.extract(this.window.document); + if (!this.options) { this.console.error('LiveReload disabled because it could not find its own