Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ Agent.prototype.close = function(err) {
};

Agent.prototype._cleanup = function() {

// Only clean up once if the stream emits both 'end' and 'close'.
if (this.closed) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this will be defensive against 'end' accidentally being emitted twice by the stream too, which the old code didn't do.


this.closed = true;

this.backend.agentsCount--;
if (!this.stream.isServer) this.backend.remoteAgentsCount--;

// Clean up doc subscription streams
for (var collection in this.subscribedDocs) {
var docs = this.subscribedDocs[collection];
Expand Down Expand Up @@ -258,12 +265,10 @@ Agent.prototype._open = function() {
agent._handleMessage(request.data, callback);
});
});

this.stream.on('end', function() {
agent.backend.agentsCount--;
if (!agent.stream.isServer) agent.backend.remoteAgentsCount--;
agent._cleanup();
});

var cleanup = agent._cleanup.bind(agent);
this.stream.on('end', cleanup);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion by Nate for the future, paraphrased by me:

In the future, we should consider not reacting to 'end', and only reacting to 'close'. He says that Share originally reacted to 'end' since the original stream implementation by Avital emitted both 'end' and 'close', since that was the recommendation for old versions of Node.

Nowadays, we probably no longer need to do that.

this.stream.on('close', cleanup);
};

// Check a request to see if its valid. Returns an error if there's a problem.
Expand Down
21 changes: 21 additions & 0 deletions test/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ describe('client connection', function() {
});
});

it('updates after connection socket stream emits "close"', function(done) {
var backend = this.backend;
var connection = backend.connect();
connection.on('connected', function() {
connection.socket.stream.emit('close')
expect(backend.agentsCount).equal(0);
done();
});
});

it('updates correctly after stream emits both "end" and "close"', function(done) {
var backend = this.backend;
var connection = backend.connect();
connection.on('connected', function() {
connection.socket.stream.emit('end')
connection.socket.stream.emit('close')
expect(backend.agentsCount).equal(0);
done();
});
});

it('does not increment when agent connect is rejected', function() {
var backend = this.backend;
backend.use('connect', function(request, next) {
Expand Down