Skip to content

Commit

Permalink
namespace: improve middleware logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Dec 18, 2012
1 parent 19837a0 commit 3a259d4
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,23 @@ Namespace.prototype.add = function(client, fn){
var socket = new Socket(this, client);
var self = this;
this.run(socket, function(err){
if (err) return socket.err(err.data || err.message);
self.sockets.push(socket);
self.emit('connect', socket);
self.emit('connection', socket);
socket.onconnect();
process.nextTick(function(){
if (err) return socket.err(err.data || err.message);

// track socket
self.sockets.push(socket);

// it's paramount that the internal `onconnect` logic
// fires before user-set events to prevent state order
// violations (such as a disconnection before the connection
// logic is complete)
socket.onconnect();
if (fn) fn();

// fire user-set events
self.emit('connect', socket);
self.emit('connection', socket);
});
});
return socket;
};
Expand Down

0 comments on commit 3a259d4

Please sign in to comment.