-
Notifications
You must be signed in to change notification settings - Fork 5
/
ezy-event-message-handler.js
51 lines (44 loc) · 1.63 KB
/
ezy-event-message-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Const from './ezy-constants';
import Util from './ezy-util';
import Event from './ezy-events';
class EzyEventMessageHandler {
constructor(client) {
this.client = client;
this.handlerManager = client.handlerManager;
this.unloggableCommands = client.unloggableCommands;
}
handleEvent(event) {
var eventHandler = this.handlerManager.getEventHandler(event.getType());
if (eventHandler) eventHandler.handle(event);
else
Util.EzyLogger.console(
'has no handler with event: ' + event.getType()
);
}
handleDisconnection(reason) {
this.client.onDisconnected(reason);
var event = new Event.EzyDisconnectionEvent(reason);
this.handleEvent(event);
}
handleMessage(message) {
var cmd = Const.EzyCommands[message[0]];
var data = message.length > 1 ? message[1] : [];
if (!this.unloggableCommands.includes(cmd))
Util.EzyLogger.console(
'received cmd: ' + cmd.name + ', data: ' + JSON.stringify(data)
);
if (cmd === Const.EzyCommand.DISCONNECT)
this.handleDisconnectionData(data);
else this.handleResponseData(cmd, data);
}
handleDisconnectionData(resonseData) {
var reason = resonseData[0];
this.handleDisconnection(reason);
}
handleResponseData(cmd, responseData) {
var handler = this.handlerManager.getDataHandler(cmd);
if (handler) handler.handle(responseData);
else Util.EzyLogger.console('has no handler with command: ' + cmd.name);
}
}
export default EzyEventMessageHandler;