Skip to content

Commit

Permalink
Use a Room class to efficiently track room size.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Baclet committed Jun 3, 2015
1 parent de5cba4 commit 5c79d6e
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions index.js
Expand Up @@ -44,8 +44,8 @@ Adapter.prototype.__proto__ = Emitter.prototype;
Adapter.prototype.add = function(id, room, fn){
this.sids[id] = this.sids[id] || {};
this.sids[id][room] = true;
this.rooms[room] = this.rooms[room] || {};
this.rooms[room][id] = true;
this.rooms[room] = this.rooms[room] || Room();
this.rooms[room].add(id);
if (fn) process.nextTick(fn.bind(null, null));
};

Expand All @@ -60,10 +60,10 @@ Adapter.prototype.add = function(id, room, fn){

Adapter.prototype.del = function(id, room, fn){
this.sids[id] = this.sids[id] || {};
this.rooms[room] = this.rooms[room] || {};
this.rooms[room] = this.rooms[room] || Room();
delete this.sids[id][room];
delete this.rooms[room][id];
if (this.rooms.hasOwnProperty(room) && !Object.keys(this.rooms[room]).length) {
this.rooms[room].del(id);
if (this.rooms[room].length === 0) {
delete this.rooms[room];
}

Expand All @@ -83,16 +83,15 @@ Adapter.prototype.delAll = function(id, fn){
if (rooms) {
for (var room in rooms) {
if (rooms.hasOwnProperty(room)) {
delete this.rooms[room][id];
}
this.rooms[room].del(id);

if (this.rooms.hasOwnProperty(room) && !Object.keys(this.rooms[room]).length) {
if (this.rooms[room].length === 0) {
delete this.rooms[room];
}
}
}
delete this.sids[id];

if (fn) process.nextTick(fn.bind(null, null));
};

Expand Down Expand Up @@ -127,8 +126,9 @@ Adapter.prototype.broadcast = function(packet, opts){
for (var i = 0; i < rooms.length; i++) {
var room = self.rooms[rooms[i]];
if (!room) continue;
for (var id in room) {
if (room.hasOwnProperty(id)) {
var sockets = room.sockets;
for (var id in sockets) {
if (sockets.hasOwnProperty(id)) {
if (ids[id] || ~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) {
Expand Down Expand Up @@ -174,8 +174,9 @@ Adapter.prototype.clients = function(rooms, fn){
for (var i = 0; i < rooms.length; i++) {
var room = self.rooms[rooms[i]];
if (!room) continue;
for (var id in room) {
if (room.hasOwnProperty(id)) {
var sockets = room.sockets;
for (var id in sockets) {
if (sockets.hasOwnProperty(id)) {
if (ids[id]) continue;
socket = self.nsp.connected[id];
if (socket) {
Expand All @@ -196,3 +197,29 @@ Adapter.prototype.clients = function(rooms, fn){

if (fn) process.nextTick(fn.bind(null, null, sids));
};

/**
* Room constructor.
*
* @api private
*/

function Room() {
if (!(this instanceof Room)) return new Room();
this.sockets = {};
this.length = 0;
}

Room.prototype.add = function(id) {
if (!this.sockets.hasOwnProperty(id)) {
this.sockets[id] = true;
this.length++;
}
};

Room.prototype.del = function(id) {
if (this.sockets.hasOwnProperty(id)) {
delete this.sockets[id];
this.length--;
}
};

0 comments on commit 5c79d6e

Please sign in to comment.