Skip to content

Commit d481a18

Browse files
Converted Arrays to Maps
1 parent ae5420b commit d481a18

File tree

5 files changed

+44
-43
lines changed

5 files changed

+44
-43
lines changed

lib/client.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Client(server, conn){
2828
this.id = conn.id;
2929
this.request = conn.request;
3030
this.setup();
31-
this.sockets = [];
31+
this.sockets = new Map();
3232
this.nsps = {};
3333
this.connectBuffer = [];
3434
}
@@ -72,7 +72,7 @@ Client.prototype.connect = function(name){
7272

7373
var self = this;
7474
var socket = nsp.add(this, function(){
75-
self.sockets.push(socket);
75+
self.sockets.set(socket.id, socket);
7676
self.nsps[nsp.name] = socket;
7777

7878
if ('/' == nsp.name && self.connectBuffer.length > 0) {
@@ -89,12 +89,10 @@ Client.prototype.connect = function(name){
8989
*/
9090

9191
Client.prototype.disconnect = function(){
92-
var socket;
93-
// we don't use a for loop because the length of
94-
// `sockets` changes upon each iteration
95-
while (socket = this.sockets.shift()) {
92+
for (var socket of this.sockets.values()) {
9693
socket.disconnect();
9794
}
95+
this.sockets.clear();
9896
this.close();
9997
};
10098

@@ -105,10 +103,9 @@ Client.prototype.disconnect = function(){
105103
*/
106104

107105
Client.prototype.remove = function(socket){
108-
var i = this.sockets.indexOf(socket);
109-
if (~i) {
110-
var nsp = this.sockets[i].nsp.name;
111-
this.sockets.splice(i, 1);
106+
if (this.sockets.has(socket.id)) {
107+
var nsp = this.sockets.get(socket.id).nsp.name;
108+
this.sockets.delete(socket.id);
112109
delete this.nsps[nsp];
113110
} else {
114111
debug('ignoring remove for %s', socket.id);
@@ -205,9 +202,9 @@ Client.prototype.ondecoded = function(packet) {
205202
*/
206203

207204
Client.prototype.onerror = function(err){
208-
this.sockets.forEach(function(socket){
205+
for (var socket of this.sockets.values()) {
209206
socket.onerror(err);
210-
});
207+
}
211208
this.onclose('client error');
212209
};
213210

@@ -225,10 +222,10 @@ Client.prototype.onclose = function(reason){
225222
this.destroy();
226223

227224
// `nsps` and `sockets` are cleaned up seamlessly
228-
var socket;
229-
while (socket = this.sockets.shift()) {
225+
for (var socket of this.sockets.values()) {
230226
socket.onclose(reason);
231227
}
228+
this.sockets.clear();
232229

233230
this.decoder.destroy(); // clean up decoder
234231
};

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ Server.prototype.of = function(name, fn){
346346
*/
347347

348348
Server.prototype.close = function(){
349-
this.nsps['/'].sockets.forEach(function(socket){
349+
for (var socket of this.nsps['/'].sockets.values()) {
350350
socket.onclose();
351-
});
351+
}
352352

353353
this.engine.close();
354354

lib/namespace.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var emit = Emitter.prototype.emit;
5151
function Namespace(server, name){
5252
this.name = name;
5353
this.server = server;
54-
this.sockets = [];
54+
this.sockets = new Map();
5555
this.connected = {};
5656
this.fns = [];
5757
this.ids = 0;
@@ -161,7 +161,7 @@ Namespace.prototype.add = function(client, fn){
161161
if (err) return socket.error(err.data || err.message);
162162

163163
// track socket
164-
self.sockets.push(socket);
164+
self.sockets.set(socket.id, socket);
165165

166166
// it's paramount that the internal `onconnect` logic
167167
// fires before user-set events to prevent state order
@@ -188,9 +188,8 @@ Namespace.prototype.add = function(client, fn){
188188
*/
189189

190190
Namespace.prototype.remove = function(socket){
191-
var i = this.sockets.indexOf(socket);
192-
if (~i) {
193-
this.sockets.splice(i, 1);
191+
if (this.sockets.has(socket.id)) {
192+
this.sockets.delete(socket.id);
194193
} else {
195194
debug('ignoring remove for %s', socket.id);
196195
}

lib/socket.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ function Socket(nsp, client){
5959
this.nsp = nsp;
6060
this.server = nsp.server;
6161
this.adapter = this.nsp.adapter;
62-
this.id = client.id;
62+
this.id = nsp.name + '#' + client.id;
6363
this.request = client.request;
6464
this.client = client;
6565
this.conn = client.conn;
66-
this.rooms = [];
66+
this.rooms = new Map();
6767
this.acks = {};
6868
this.connected = true;
6969
this.disconnected = false;
@@ -223,11 +223,11 @@ Socket.prototype.packet = function(packet, opts){
223223
Socket.prototype.join = function(room, fn){
224224
debug('joining room %s', room);
225225
var self = this;
226-
if (~this.rooms.indexOf(room)) return this;
226+
if (this.rooms.has(room)) return this;
227227
this.adapter.add(this.id, room, function(err){
228228
if (err) return fn && fn(err);
229229
debug('joined room %s', room);
230-
self.rooms.push(room);
230+
self.rooms.set(room, room);
231231
fn && fn(null);
232232
});
233233
return this;
@@ -248,10 +248,7 @@ Socket.prototype.leave = function(room, fn){
248248
this.adapter.del(this.id, room, function(err){
249249
if (err) return fn && fn(err);
250250
debug('left room %s', room);
251-
var idx = self.rooms.indexOf(room);
252-
if (idx >= 0) {
253-
self.rooms.splice(idx, 1);
254-
}
251+
self.rooms.delete(room);
255252
fn && fn(null);
256253
});
257254
return this;
@@ -265,7 +262,7 @@ Socket.prototype.leave = function(room, fn){
265262

266263
Socket.prototype.leaveAll = function(){
267264
this.adapter.delAll(this.id);
268-
this.rooms = [];
265+
this.rooms.clear();
269266
};
270267

271268
/**

test/socket.io.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ function client(srv, nsp, opts){
1919
return ioc(url, opts);
2020
}
2121

22+
// Returns an array of keys from a Map
23+
// Note: spread separator only available in Node.js >= 5.x.x
24+
function getKeys(map){
25+
var a = [];
26+
for (var k of map.keys()) { a.push(k); }
27+
return a;
28+
}
29+
2230
describe('socket.io', function(){
2331

2432
it('should be the same version as client', function(){
@@ -342,12 +350,12 @@ describe('socket.io', function(){
342350
var clientSocket = client(srv, { reconnection: false });
343351

344352
clientSocket.on('disconnect', function init() {
345-
expect(sio.nsps['/'].sockets.length).to.equal(0);
353+
expect(sio.nsps['/'].sockets.size).to.equal(0);
346354
server.listen(PORT);
347355
});
348356

349357
clientSocket.on('connect', function init() {
350-
expect(sio.nsps['/'].sockets.length).to.equal(1);
358+
expect(sio.nsps['/'].sockets.size).to.equal(1);
351359
sio.close();
352360
});
353361

@@ -369,12 +377,12 @@ describe('socket.io', function(){
369377
var clientSocket = ioc('ws://0.0.0.0:' + PORT);
370378

371379
clientSocket.on('disconnect', function init() {
372-
expect(sio.nsps['/'].sockets.length).to.equal(0);
380+
expect(sio.nsps['/'].sockets.size).to.equal(0);
373381
server.listen(PORT);
374382
});
375383

376384
clientSocket.on('connect', function init() {
377-
expect(sio.nsps['/'].sockets.length).to.equal(1);
385+
expect(sio.nsps['/'].sockets.size).to.equal(1);
378386
sio.close();
379387
});
380388

@@ -1906,15 +1914,15 @@ describe('socket.io', function(){
19061914
var socket = client(srv);
19071915
sio.on('connection', function(s){
19081916
s.join('a', function(){
1909-
expect(s.rooms).to.eql([s.id, 'a']);
1917+
expect(getKeys(s.rooms)).to.eql([s.id, 'a']);
19101918
s.join('b', function(){
1911-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1919+
expect(getKeys(s.rooms)).to.eql([s.id, 'a', 'b']);
19121920
s.join( 'c', function(){
1913-
expect(s.rooms).to.eql([s.id, 'a', 'b', 'c']);
1921+
expect(getKeys(s.rooms)).to.eql([s.id, 'a', 'b', 'c']);
19141922
s.leave('b', function(){
1915-
expect(s.rooms).to.eql([s.id, 'a', 'c']);
1923+
expect(getKeys(s.rooms)).to.eql([s.id, 'a', 'c']);
19161924
s.leaveAll();
1917-
expect(s.rooms).to.eql([]);
1925+
expect(getKeys(s.rooms)).to.eql([]);
19181926
done();
19191927
});
19201928
});
@@ -1950,13 +1958,13 @@ describe('socket.io', function(){
19501958
var socket = client(srv);
19511959
sio.on('connection', function(s){
19521960
s.join('a', function(){
1953-
expect(s.rooms).to.eql([s.id, 'a']);
1961+
expect(getKeys(s.rooms)).to.eql([s.id, 'a']);
19541962
s.join('b', function(){
1955-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1963+
expect(getKeys(s.rooms)).to.eql([s.id, 'a', 'b']);
19561964
s.leave('unknown', function(){
1957-
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1965+
expect(getKeys(s.rooms)).to.eql([s.id, 'a', 'b']);
19581966
s.leaveAll();
1959-
expect(s.rooms).to.eql([]);
1967+
expect(getKeys(s.rooms)).to.eql([]);
19601968
done();
19611969
});
19621970
});

0 commit comments

Comments
 (0)