Skip to content

Commit 25ce852

Browse files
authored
Merge pull request #16 from youngmonkeys/dev
v1.2.0: merge dev
2 parents 2e2fd87 + f023f4a commit 25ce852

15 files changed

+469
-368
lines changed

ezy-client.js

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import Const from './ezy-constants'
2-
import Util from './ezy-util'
3-
import Event from './ezy-events'
4-
import Manager from './ezy-managers'
5-
import Socket from './ezy-sockets'
6-
import EzySetup from './ezy-setup'
7-
import EzyEventMessageHandler from './ezy-event-message-handler'
1+
import Const from './ezy-constants';
2+
import Util from './ezy-util';
3+
import Event from './ezy-events';
4+
import Manager from './ezy-managers';
5+
import Socket from './ezy-sockets';
6+
import EzySetup from './ezy-setup';
7+
import EzyEventMessageHandler from './ezy-event-message-handler';
88

99
/**
1010
* Wrapper for JS built-in WebSocket to communicate with websocket server
@@ -29,47 +29,51 @@ class EzyConnector {
2929
connect(client, url) {
3030
this.disconnectReason = null;
3131
this.ws = new WebSocket(url);
32-
var thiz = this;
32+
let that = this;
3333
var failed = false;
34-
var pingManager = client.pingManager;
35-
var eventMessageHandler = client.eventMessageHandler;
34+
let pingManager = client.pingManager;
35+
let eventMessageHandler = client.eventMessageHandler;
3636

3737
this.ws.onerror = function (e) {
38-
Util.EzyLogger.console('connect to: ' + url + ' error : ' + JSON.stringify(e));
38+
Util.EzyLogger.console(
39+
'connect to: ' + url + ' error : ' + JSON.stringify(e)
40+
);
3941
failed = true;
40-
var event = new Event.EzyConnectionFailureEvent(Const.EzyConnectionFailedReason.UNKNOWN);
42+
var event = new Event.EzyConnectionFailureEvent(
43+
Const.EzyConnectionFailedReason.UNKNOWN
44+
);
4145
eventMessageHandler.handleEvent(event);
42-
}
46+
};
4347

4448
this.ws.onopen = function (e) {
4549
Util.EzyLogger.console('connected to: ' + url);
4650
client.reconnectCount = 0;
4751
client.status = Const.EzyConnectionStatus.CONNECTED;
4852
var event = new Event.EzyConnectionSuccessEvent();
4953
eventMessageHandler.handleEvent(event);
50-
}
54+
};
5155

5256
this.ws.onclose = function (e) {
53-
if (failed)
54-
return;
55-
if (thiz.destroyed)
56-
return;
57+
if (failed) return;
58+
if (that.destroyed) return;
5759
if (client.isConnected()) {
58-
var reason = thiz.disconnectReason || Const.EzyDisconnectReason.UNKNOWN;
60+
var reason =
61+
that.disconnectReason || Const.EzyDisconnectReason.UNKNOWN;
5962
eventMessageHandler.handleDisconnection(reason);
6063
} else {
61-
Util.EzyLogger.console('connection to: ' + url + " has disconnected before");
64+
Util.EzyLogger.console(
65+
'connection to: ' + url + ' has disconnected before'
66+
);
6267
}
63-
}
68+
};
6469

6570
this.ws.onmessage = function (event) {
66-
if (thiz.destroyed)
67-
return;
71+
if (that.destroyed) return;
6872
pingManager.lostPingCount = 0;
6973
var data = event.data;
7074
var message = JSON.parse(data);
7175
eventMessageHandler.handleMessage(message);
72-
}
76+
};
7377
}
7478

7579
/**
@@ -124,7 +128,10 @@ class EzyClient {
124128
this.pingSchedule = new Socket.EzyPingSchedule(this);
125129
this.handlerManager = new Manager.EzyHandlerManager(this);
126130
this.setup = new EzySetup(this.handlerManager);
127-
this.unloggableCommands = [Const.EzyCommand.PING, Const.EzyCommand.PONG];
131+
this.unloggableCommands = [
132+
Const.EzyCommand.PING,
133+
Const.EzyCommand.PONG,
134+
];
128135
this.eventMessageHandler = new EzyEventMessageHandler(this);
129136
this.pingSchedule.eventMessageHandler = this.eventMessageHandler;
130137
}
@@ -151,17 +158,13 @@ class EzyClient {
151158
reconnect() {
152159
var reconnectConfig = this.config.reconnect;
153160
var maxReconnectCount = reconnectConfig.maxReconnectCount;
154-
if (this.reconnectCount >= maxReconnectCount)
155-
return false;
161+
if (this.reconnectCount >= maxReconnectCount) return false;
156162
this.preconnect();
157163
this.status = Const.EzyConnectionStatus.RECONNECTING;
158-
this.reconnectTimeout = setTimeout(
159-
() => {
160-
this.connector = new EzyConnector();
161-
this.connector.connect(this, this.url);
162-
},
163-
reconnectConfig.reconnectPeriod
164-
);
164+
this.reconnectTimeout = setTimeout(() => {
165+
this.connector = new EzyConnector();
166+
this.connector.connect(this, this.url);
167+
}, reconnectConfig.reconnectPeriod);
165168
this.reconnectCount++;
166169
var event = new Event.EzyTryConnectEvent(this.reconnectCount);
167170
this.eventMessageHandler.handleEvent(event);
@@ -174,10 +177,8 @@ class EzyClient {
174177
this.zone = null;
175178
this.me = null;
176179
this.appsById = {};
177-
if (this.connector)
178-
this.connector.destroy();
179-
if (this.reconnectTimeout)
180-
clearTimeout(this.reconnectTimeout);
180+
if (this.connector) this.connector.destroy();
181+
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
181182
}
182183

183184
/**
@@ -194,16 +195,15 @@ class EzyClient {
194195
}
195196

196197
internalDisconnect(reason) {
197-
if (this.connector)
198-
this.connector.disconnect(reason);
198+
if (this.connector) this.connector.disconnect(reason);
199199
}
200200

201201
/**
202202
* Send data to websocket server
203203
* @param data
204204
*/
205205
send(cmd, data) {
206-
this.sendRequest(cmd, data)
206+
this.sendRequest(cmd, data);
207207
}
208208

209209
/**
@@ -213,7 +213,9 @@ class EzyClient {
213213
*/
214214
sendRequest(cmd, data) {
215215
if (!this.unloggableCommands.includes(cmd)) {
216-
Util.EzyLogger.console('send cmd: ' + cmd.name + ", data: " + JSON.stringify(data));
216+
Util.EzyLogger.console(
217+
'send cmd: ' + cmd.name + ', data: ' + JSON.stringify(data)
218+
);
217219
}
218220
var request = [cmd.id, data];
219221
this.connector.send(request);
@@ -234,15 +236,15 @@ class EzyClient {
234236
* @returns {boolean} Whether or not the status is CONNECTED
235237
*/
236238
isConnected() {
237-
var connected = (this.status == Const.EzyConnectionStatus.CONNECTED);
239+
var connected = this.status === Const.EzyConnectionStatus.CONNECTED;
238240
return connected;
239241
}
240242

241243
/**
242244
* Get first app from this client zone
243245
* @returns {EzyApp} Queried app
244246
*/
245-
getApp() {
247+
getApp() {
246248
if (!this.zone) return null;
247249
var appManager = this.zone.appManager;
248250
return appManager.getApp();
@@ -270,6 +272,10 @@ class EzyClient {
270272
return pluginManager.getPluginById(pluginId);
271273
}
272274

275+
newAppManager(zoneName) {
276+
return new Manager.EzyAppManager(zoneName);
277+
}
278+
273279
/**
274280
* Get the app manager of this client zone
275281
* @returns {EzyAppManager} App manager of current client zone
@@ -279,6 +285,10 @@ class EzyClient {
279285
return this.zone.appManager;
280286
}
281287

288+
newPluginManager(zoneName) {
289+
return new Manager.EzyPluginManager(zoneName);
290+
}
291+
282292
/**
283293
* Get the plugin manager of this client zone
284294
* @returns {EzyPluginManager} Plugin manager of current client zone
@@ -289,4 +299,4 @@ class EzyClient {
289299
}
290300
}
291301

292-
export default EzyClient
302+
export default EzyClient;

ezy-clients.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import EzyClient from './ezy-client'
1+
import EzyClient from './ezy-client';
22

33
/**
44
* Singleton object to manage all clients of a server.
@@ -8,7 +8,7 @@ import EzyClient from './ezy-client'
88
class EzyClients {
99
constructor() {
1010
this.clients = {};
11-
this.defaultClientName = "";
11+
this.defaultClientName = '';
1212
}
1313

1414
/**
@@ -30,8 +30,7 @@ class EzyClients {
3030
newClient(config) {
3131
var client = new EzyClient(config);
3232
this.addClient(client);
33-
if (this.defaultClientName == "")
34-
this.defaultClientName = client.name;
33+
if (this.defaultClientName === '') this.defaultClientName = client.name;
3534
return client;
3635
}
3736

@@ -72,4 +71,4 @@ class EzyClients {
7271
}
7372
}
7473

75-
export default EzyClients
74+
export default EzyClients;

ezy-configs.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class EzyClientConfig {
22
constructor() {
3-
this.zoneName = "";
4-
this.clientName = "";
3+
this.zoneName = '';
4+
this.clientName = '';
55
this.reconnect = new EzyReconnectConfig();
66
}
77

@@ -10,8 +10,7 @@ class EzyClientConfig {
1010
* @returns {string} Client name
1111
*/
1212
getClientName() {
13-
if (this.clientName == "")
14-
return this.zoneName;
13+
if (this.clientName === '') return this.zoneName;
1514
return this.clientName;
1615
}
1716
}
@@ -24,4 +23,4 @@ class EzyReconnectConfig {
2423
}
2524
}
2625

27-
export default {EzyClientConfig}
26+
export default { EzyClientConfig };

0 commit comments

Comments
 (0)