Skip to content

Commit 5cba924

Browse files
committed
format code
1 parent 31ed5b9 commit 5cba924

File tree

6 files changed

+140
-34
lines changed

6 files changed

+140
-34
lines changed

src/connector.js

+46-9
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ class Connector {
2121
this._handshakeTimeout.stop();
2222
this._nextDelay = this.options.mindelay;
2323
this._disconnectionReason = 'broken';
24+
2425
return this.handlers.connected(this.protocol);
2526
},
2627
error: e => {
2728
this.handlers.error(e);
29+
2830
return this._closeOnError();
2931
},
3032
message: message => {
@@ -33,13 +35,21 @@ class Connector {
3335
});
3436

3537
this._handshakeTimeout = new this.Timer(() => {
36-
if (!this._isSocketConnected()) { return; }
38+
if (!this._isSocketConnected()) {
39+
return;
40+
}
41+
3742
this._disconnectionReason = 'handshake-timeout';
43+
3844
return this.socket.close();
3945
});
4046

4147
this._reconnectTimer = new this.Timer(() => {
42-
if (!this._connectionDesired) { return; } // shouldn't hit this, but just in case
48+
if (!this._connectionDesired) {
49+
// shouldn't hit this, but just in case
50+
return;
51+
}
52+
4353
return this.connect();
4454
});
4555

@@ -52,7 +62,10 @@ class Connector {
5262

5363
connect () {
5464
this._connectionDesired = true;
55-
if (this._isSocketConnected()) { return; }
65+
66+
if (this._isSocketConnected()) {
67+
return;
68+
}
5669

5770
// prepare for a new connection
5871
this._reconnectTimer.stop();
@@ -71,21 +84,33 @@ class Connector {
7184
disconnect () {
7285
this._connectionDesired = false;
7386
this._reconnectTimer.stop(); // in case it was running
74-
if (!this._isSocketConnected()) { return; }
87+
88+
if (!this._isSocketConnected()) {
89+
return;
90+
}
91+
7592
this._disconnectionReason = 'manual';
93+
7694
return this.socket.close();
7795
}
7896

7997
_scheduleReconnection () {
80-
if (!this._connectionDesired) { return; } // don't reconnect after manual disconnection
98+
if (!this._connectionDesired) {
99+
// don't reconnect after manual disconnection
100+
return;
101+
}
102+
81103
if (!this._reconnectTimer.running) {
82104
this._reconnectTimer.start(this._nextDelay);
83105
this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2);
84106
}
85107
}
86108

87109
sendCommand (command) {
88-
if (!this.protocol) { return; }
110+
if (!this.protocol) {
111+
return;
112+
}
113+
89114
return this._sendCommand(command);
90115
}
91116

@@ -106,16 +131,28 @@ class Connector {
106131
// start handshake
107132
const hello = { command: 'hello', protocols: [PROTOCOL_6, PROTOCOL_7] };
108133
hello.ver = VERSION;
109-
if (this.options.ext) { hello.ext = this.options.ext; }
110-
if (this.options.extver) { hello.extver = this.options.extver; }
111-
if (this.options.snipver) { hello.snipver = this.options.snipver; }
134+
135+
if (this.options.ext) {
136+
hello.ext = this.options.ext;
137+
}
138+
139+
if (this.options.extver) {
140+
hello.extver = this.options.extver;
141+
}
142+
143+
if (this.options.snipver) {
144+
hello.snipver = this.options.snipver;
145+
}
146+
112147
this._sendCommand(hello);
148+
113149
return this._handshakeTimeout.start(this.options.handshake_timeout);
114150
}
115151

116152
_onclose (e) {
117153
this.protocol = 0;
118154
this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
155+
119156
return this._scheduleReconnection();
120157
}
121158

src/less.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ class LessPlugin {
99
if (path.match(/\.less$/i)) {
1010
return this.reloadLess(path);
1111
}
12+
1213
if (options.originalPath.match(/\.less$/i)) {
1314
return this.reloadLess(options.originalPath);
1415
}
1516
}
17+
1618
return false;
1719
}
1820

@@ -28,19 +30,24 @@ class LessPlugin {
2830
return result;
2931
})());
3032

31-
if (links.length === 0) { return false; }
33+
if (links.length === 0) {
34+
return false;
35+
}
3236

3337
for (link of Array.from(links)) {
3438
link.href = this.host.generateCacheBustUrl(link.href);
3539
}
3640

3741
this.host.console.log('LiveReload is asking LESS to recompile all stylesheets');
3842
this.window.less.refresh(true);
43+
3944
return true;
4045
}
4146

4247
analyze () {
43-
return { disable: !!(this.window.less && this.window.less.refresh) };
48+
return {
49+
disable: !!(this.window.less && this.window.less.refresh)
50+
};
4451
}
4552
};
4653

src/livereload.js

+42-15
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ class LiveReload {
6060
if (typeof this.listeners.connect === 'function') {
6161
this.listeners.connect();
6262
}
63+
6364
this.log(`LiveReload is connected to ${this.options.host}:${this.options.port} (protocol v${protocol}).`);
65+
6466
return this.analyze();
6567
},
6668

6769
error: e => {
6870
if (e instanceof ProtocolError) {
69-
if (typeof console !== 'undefined' && console !== null) { return console.log(`${e.message}.`); }
71+
if (typeof console !== 'undefined' && console !== null) {
72+
return console.log(`${e.message}.`);
73+
}
7074
} else {
71-
if (typeof console !== 'undefined' && console !== null) { return console.log(`LiveReload internal error: ${e.message}`); }
75+
if (typeof console !== 'undefined' && console !== null) {
76+
return console.log(`LiveReload internal error: ${e.message}`);
77+
}
7278
}
7379
},
7480

@@ -94,12 +100,13 @@ class LiveReload {
94100

95101
message: message => {
96102
switch (message.command) {
97-
case 'reload': return this.performReload(message);
98-
case 'alert': return this.performAlert(message);
103+
case 'reload':
104+
return this.performReload(message);
105+
case 'alert':
106+
return this.performAlert(message);
99107
}
100108
}
101-
}
102-
);
109+
});
103110

104111
this.initialized = true;
105112
}
@@ -114,34 +121,44 @@ class LiveReload {
114121

115122
performReload (message) {
116123
this.log(`LiveReload received reload request: ${JSON.stringify(message, null, 2)}`);
124+
117125
return this.reloader.reload(message.path, {
118126
liveCSS: message.liveCSS != null ? message.liveCSS : true,
119127
liveImg: message.liveImg != null ? message.liveImg : true,
120128
reloadMissingCSS: message.reloadMissingCSS != null ? message.reloadMissingCSS : true,
121129
originalPath: message.originalPath || '',
122130
overrideURL: message.overrideURL || '',
123131
serverURL: `http://${this.options.host}:${this.options.port}`
124-
}
125-
);
132+
});
126133
}
127134

128135
performAlert (message) {
129136
return alert(message.message);
130137
}
131138

132139
shutDown () {
133-
if (!this.initialized) { return; }
140+
if (!this.initialized) {
141+
return;
142+
}
143+
134144
this.connector.disconnect();
135145
this.log('LiveReload disconnected.');
136146
return (typeof this.listeners.shutdown === 'function' ? this.listeners.shutdown() : undefined);
137147
}
138148

139-
hasPlugin (identifier) { return !!this.pluginIdentifiers[identifier]; }
149+
hasPlugin (identifier) {
150+
return !!this.pluginIdentifiers[identifier];
151+
}
140152

141153
addPlugin (PluginClass) {
142-
if (!this.initialized) { return; }
154+
if (!this.initialized) {
155+
return;
156+
}
157+
158+
if (this.hasPlugin(PluginClass.identifier)) {
159+
return;
160+
}
143161

144-
if (this.hasPlugin(PluginClass.identifier)) { return; }
145162
this.pluginIdentifiers[PluginClass.identifier] = true;
146163

147164
const plugin = new PluginClass(this.window, {
@@ -183,17 +200,27 @@ class LiveReload {
183200
}
184201

185202
analyze () {
186-
if (!this.initialized) { return; }
187-
if (!(this.connector.protocol >= 7)) { return; }
203+
if (!this.initialized) {
204+
return;
205+
}
206+
207+
if (!(this.connector.protocol >= 7)) {
208+
return;
209+
}
188210

189211
const pluginsData = {};
212+
190213
for (let plugin of this.plugins) {
191214
var pluginData = (typeof plugin.analyze === 'function' ? plugin.analyze() : undefined) || {};
192215
pluginsData[plugin.constructor.identifier] = pluginData;
193216
pluginData.version = plugin.constructor.version;
194217
}
195218

196-
this.connector.sendCommand({ command: 'info', plugins: pluginsData, url: this.window.location.href });
219+
this.connector.sendCommand({
220+
command: 'info',
221+
plugins: pluginsData,
222+
url: this.window.location.href
223+
});
197224
}
198225
};
199226

src/protocol.js

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Parser {
3535
throw new ProtocolError('no supported protocols found');
3636
}
3737
}
38+
3839
return this.handlers.connected(this.protocol);
3940
} else if (this.protocol === 6) {
4041
message = JSON.parse(data);
@@ -53,6 +54,7 @@ class Parser {
5354
});
5455
} else {
5556
message = this._parseMessage(data, ['reload', 'alert']);
57+
5658
return this.handlers.message(message);
5759
}
5860
} catch (e) {
@@ -66,17 +68,21 @@ class Parser {
6668

6769
_parseMessage (data, validCommands) {
6870
let message;
71+
6972
try {
7073
message = JSON.parse(data);
7174
} catch (e) {
7275
throw new ProtocolError('unparsable JSON', data);
7376
}
77+
7478
if (!message.command) {
7579
throw new ProtocolError('missing "command" key', data);
7680
}
81+
7782
if (!validCommands.includes(message.command)) {
7883
throw new ProtocolError(`invalid command '${message.command}', only valid commands are: ${validCommands.join(', ')})`, data);
7984
}
85+
8086
return message;
8187
}
8288
};

0 commit comments

Comments
 (0)