UNPKG

1.04 kBJavaScriptView Raw
1var Frame = require('./frame')
2
3var Event = function(data) {
4 this.type = data.type;
5 this.state = data.state;
6};
7
8var chooseProtocol = exports.chooseProtocol = function(header) {
9 var protocol;
10 switch(header.version) {
11 case 1:
12 case 2:
13 case 3:
14 case 4:
15 protocol = JSONProtocol(header.version, function(data) {
16 return data.event ? new Event(data.event) : new Frame(data);
17 });
18 protocol.sendBackground = function(connection, state) {
19 connection.send(protocol.encode({background: state}));
20 }
21 protocol.sendFocused = function(connection, state) {
22 connection.send(protocol.encode({focused: state}));
23 }
24 break;
25 default:
26 throw "unrecognized version";
27 }
28 return protocol;
29}
30
31var JSONProtocol = function(version, cb) {
32 var protocol = cb;
33 protocol.encode = function(message) {
34 return JSON.stringify(message);
35 }
36 protocol.version = version;
37 protocol.versionLong = 'Version ' + version;
38 protocol.type = 'protocol';
39 return protocol;
40};