Skip to content

Commit

Permalink
Handle case of emitting both 'end' and 'close'. Closes share#282
Browse files Browse the repository at this point in the history
  • Loading branch information
curran committed Apr 9, 2019
1 parent 186e2ab commit ef236fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
14 changes: 8 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;

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 @@ -259,12 +266,7 @@ Agent.prototype._open = function() {
});
});

function cleanup() {
agent.backend.agentsCount--;
if (!agent.stream.isServer) agent.backend.remoteAgentsCount--;
agent._cleanup();
}

var cleanup = agent._cleanup.bind(agent);
this.stream.on('end', cleanup);
this.stream.on('close', cleanup);
};
Expand Down
17 changes: 13 additions & 4 deletions test/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,19 @@ describe('client connection', function() {
var connection = backend.connect();
connection.on('connected', function() {
connection.socket.stream.emit('close')
setTimeout(function() {
expect(backend.agentsCount).equal(0);
done();
}, 10);
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();
});
});

Expand Down

0 comments on commit ef236fa

Please sign in to comment.