Skip to content

Commit

Permalink
destroyed check when connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle committed Feb 21, 2013
1 parent a116ed5 commit e0d9e22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/api.md
Expand Up @@ -86,8 +86,8 @@ The `error` object also has a `type` parameter that may be helpful in responding
* `server-error`: Unable to reach the server. * `server-error`: Unable to reach the server.
* `socket-error`: An error from the underlying socket. * `socket-error`: An error from the underlying socket.
* `socket-closed`: The underlying socket closed unexpectedly. * `socket-closed`: The underlying socket closed unexpectedly.

(The Peer object is destroyed after one of the errors above are emitted.)
The Peer object is destroyed after one of the errors above are emitted. * `peer-destroyed`: A Peer that has been destroyed is being used to try to connect.


### Event: 'close' ### Event: 'close'


Expand Down
10 changes: 8 additions & 2 deletions lib/peer.js
Expand Up @@ -175,7 +175,6 @@ Peer.prototype._cleanup = function() {
self._socket.close(); self._socket.close();
}); });
this.emit('close'); this.emit('close');
this.destroyed = true;
}; };


/** Listeners for DataConnection events. */ /** Listeners for DataConnection events. */
Expand All @@ -195,6 +194,10 @@ Peer.prototype._attachConnectionListeners = function(connection) {
// TODO: pause XHR streaming when not in use and start again when this is // TODO: pause XHR streaming when not in use and start again when this is
// called. // called.
Peer.prototype.connect = function(peer, options) { Peer.prototype.connect = function(peer, options) {
if (this.destroyed) {
this._abort('peer-destroyed', 'This Peer has been destroyed and is no longer able to make connections.')
}

options = util.extend({ options = util.extend({
config: this._options.config config: this._options.config
}, options); }, options);
Expand All @@ -210,7 +213,10 @@ Peer.prototype.connect = function(peer, options) {
}; };


Peer.prototype.destroy = function() { Peer.prototype.destroy = function() {
this._cleanup(); if (!this.destroyed) {
this._cleanup();
this.destroyed = true;
}
}; };




Expand Down

0 comments on commit e0d9e22

Please sign in to comment.