Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/lib/AphlictListener.js
| var JX = require('javelin').JX; | var JX = require('javelin').JX; | ||||
| JX.install('AphlictListener', { | JX.install('AphlictListener', { | ||||
| construct: function(id, socket) { | construct: function(id, socket) { | ||||
| this._id = id; | this._id = id; | ||||
| this._socket = socket; | this._socket = socket; | ||||
| }, | }, | ||||
epriestley: I'd expect `this._subscriptions = {}` here? | |||||
| members: { | members: { | ||||
| _id: null, | _id: null, | ||||
| _socket: null, | _socket: null, | ||||
| _subscriptions: {}, | |||||
| getID: function() { | getID: function() { | ||||
| return this._id; | return this._id; | ||||
| }, | }, | ||||
| subscribe: function(phids) { | |||||
| for (var i = 0; i < phids.length; i++) { | |||||
| var phid = phids[i]; | |||||
| this._subscriptions[phid] = true; | |||||
| } | |||||
| return this; | |||||
| }, | |||||
| unsubscribe: function(phids) { | |||||
| for (var i = 0; i < phids.length; i++) { | |||||
| var phid = phids[i]; | |||||
| delete this._subscriptions[phid]; | |||||
| } | |||||
| return this; | |||||
| }, | |||||
| isSubscribedToAny: function(phids) { | |||||
| var intersection = phids.filter(function(phid) { | |||||
| return phid in this._subscriptions; | |||||
| }, this); | |||||
| return intersection.length > 0; | |||||
| }, | |||||
| getSocket: function() { | getSocket: function() { | ||||
| return this._socket; | return this._socket; | ||||
| }, | }, | ||||
| getDescription: function() { | getDescription: function() { | ||||
| return 'Listener/' + this.getID(); | return 'Listener/' + this.getID(); | ||||
| }, | }, | ||||
Not Done Inline ActionsConsider this._subscriptions[phid] = true; epriestley: Consider `this._subscriptions[phid] = true;` | |||||
Not Done Inline ActionsYou could do this assignment/delete unconditionally if you want, they won't hurt anything. epriestley: You could do this assignment/delete unconditionally if you want, they won't hurt anything. | |||||
| writeMessage: function(message) { | writeMessage: function(message) { | ||||
| var serial = JSON.stringify(message); | var serial = JSON.stringify(message); | ||||
| var length = Buffer.byteLength(serial, 'utf8'); | var length = Buffer.byteLength(serial, 'utf8'); | ||||
| length = length.toString(); | length = length.toString(); | ||||
| while (length.length < 8) { | while (length.length < 8) { | ||||
| length = '0' + length; | length = '0' + length; | ||||
| } | } | ||||
| this._socket.write(length + serial); | this._socket.write(length + serial); | ||||
| } | } | ||||
Not Done Inline ActionsConsider delete this._subscriptions[phid]; epriestley: Consider `delete this._subscriptions[phid];` | |||||
| } | } | ||||
| }); | }); | ||||
Not Done Inline ActionsConsider ... if (phid in this._subscriptions) { return true; } ... epriestley: Consider `... if (phid in this._subscriptions) { return true; } ...` | |||||
Not Done Inline ActionsConsider isSubscribedToAny() or similar, for clarity. epriestley: Consider `isSubscribedToAny()` or similar, for clarity. | |||||
I'd expect this._subscriptions = {} here?