From bc9975baa1d13d67b87192860fd48fe8884dda28 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 3 Feb 2015 14:33:39 +0100 Subject: [PATCH] Fix connection double closing. --- src/connection.js | 14 +++++++++++--- src/main.coffee | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/connection.js b/src/connection.js index 4ef6184d0..46e618c2b 100644 --- a/src/connection.js +++ b/src/connection.js @@ -12,26 +12,34 @@ var assign = require('lodash.assign'); var has = Object.prototype.hasOwnProperty; has = has.call.bind(has); +function noop() {} + //==================================================================== -var Connection = function Connection(opts) { +function Connection(opts) { + EventEmitter.call(this); + this.data = Object.create(null); this._close = opts.close; this.notify = opts.notify; -}; +} inherits(Connection, EventEmitter); assign(Connection.prototype, { // Close the connection. close: function () { + // Prevent errors when the connection is closed more than once. + this.close = noop; + this._close(); + this.emit('close'); // Releases values AMAP to ease the garbage collecting. for (var key in this) { - if (has(this, key)) + if (key !== 'close' && has(this, key)) { delete this[key]; } diff --git a/src/main.coffee b/src/main.coffee index 251e036fc..43c852b5d 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -158,7 +158,7 @@ exports = module.exports = $coroutine (args) -> # Close the connection with the socket. socket.on 'close', -> $debug '- WebSocket connection' - $bind connection.close, connection + connection.close() return # Connect the WebSocket to the JSON-RPC server