ソースを参照

destroyed check when connecting

Michelle Bu 12 年 前
コミット
e0d9e22155
2 ファイル変更10 行追加4 行削除
  1. 2 2
      docs/api.md
  2. 8 2
      lib/peer.js

+ 2 - 2
docs/api.md

@@ -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.
   * `socket-error`: An error from the underlying socket.
   * `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'
 

+ 8 - 2
lib/peer.js

@@ -175,7 +175,6 @@ Peer.prototype._cleanup = function() {
     self._socket.close();
   });
   this.emit('close');
-  this.destroyed = true;
 };
 
 /** Listeners for DataConnection events. */
@@ -195,6 +194,10 @@ Peer.prototype._attachConnectionListeners = function(connection) {
 // TODO: pause XHR streaming when not in use and start again when this is
 // called.
 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({
     config: this._options.config
   }, options);
@@ -210,7 +213,10 @@ Peer.prototype.connect = function(peer, options) {
 };
 
 Peer.prototype.destroy = function() {
-  this._cleanup();
+  if (!this.destroyed) {
+    this._cleanup();
+    this.destroyed = true;
+  }
 };