Bläddra i källkod

zero timeout so errors can be listened for

Michelle Bu 12 år sedan
förälder
incheckning
fef74a4def
3 ändrade filer med 43 tillägg och 21 borttagningar
  1. 25 11
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 18 10
      lib/peer.js

+ 25 - 11
dist/peer.js

@@ -854,12 +854,17 @@ function Peer(id, options) {
   util.debug = options.debug;
 
   // Ensure alphanumeric_-
+  var self = this;
   if (id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id)) {
-    this._abort('invalid-id', 'ID "' + id + '" is invalid');
+    util.setZeroTimeout(function() {
+      self._abort('invalid-id', 'ID "' + id + '" is invalid');
+    });
     return
   }
   if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
-    this._abort('invalid-key', 'API KEY "' + options.key + '" is invalid');
+    util.setZeroTimeout(function() {
+      self._abort('invalid-key', 'API KEY "' + options.key + '" is invalid');
+    });
     return
   }
 
@@ -995,21 +1000,22 @@ Peer.prototype._processQueue = function() {
 Peer.prototype._abort = function(type, message) {
   var err = new Error(message);
   err.type = type;
-  this.emit('error', err);
   this.destroy();
+  this.emit('error', err);
 };
 
 Peer.prototype._cleanup = function() {
   var self = this;
-  var peers = Object.keys(this.connections);
-  for (var i = 0, ii = peers.length; i < ii; i++) {
-    this.connections[peers[i]].close();
+  if (!!this.connections) {
+    var peers = Object.keys(this.connections);
+    for (var i = 0, ii = peers.length; i < ii; i++) {
+      this.connections[peers[i]].close();
+    }
+    util.setZeroTimeout(function(){
+      self._socket.close();
+    });
   }
-  util.setZeroTimeout(function(){
-    self._socket.close();
-  });
   this.emit('close');
-  this.destroyed = true;
 };
 
 /** Listeners for DataConnection events. */
@@ -1029,6 +1035,11 @@ 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.');
+    return;
+  }
+
   options = util.extend({
     config: this._options.config
   }, options);
@@ -1044,7 +1055,10 @@ Peer.prototype.connect = function(peer, options) {
 };
 
 Peer.prototype.destroy = function() {
-  this._cleanup();
+  if (!this.destroyed) {
+    this._cleanup();
+    this.destroyed = true;
+  }
 };
 
 

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
dist/peer.min.js


+ 18 - 10
lib/peer.js

@@ -20,12 +20,17 @@ function Peer(id, options) {
   util.debug = options.debug;
 
   // Ensure alphanumeric_-
+  var self = this;
   if (id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id)) {
-    this._abort('invalid-id', 'ID "' + id + '" is invalid');
+    util.setZeroTimeout(function() {
+      self._abort('invalid-id', 'ID "' + id + '" is invalid');
+    });
     return
   }
   if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
-    this._abort('invalid-key', 'API KEY "' + options.key + '" is invalid');
+    util.setZeroTimeout(function() {
+      self._abort('invalid-key', 'API KEY "' + options.key + '" is invalid');
+    });
     return
   }
 
@@ -161,19 +166,21 @@ Peer.prototype._processQueue = function() {
 Peer.prototype._abort = function(type, message) {
   var err = new Error(message);
   err.type = type;
-  this.emit('error', err);
   this.destroy();
+  this.emit('error', err);
 };
 
 Peer.prototype._cleanup = function() {
   var self = this;
-  var peers = Object.keys(this.connections);
-  for (var i = 0, ii = peers.length; i < ii; i++) {
-    this.connections[peers[i]].close();
+  if (!!this.connections) {
+    var peers = Object.keys(this.connections);
+    for (var i = 0, ii = peers.length; i < ii; i++) {
+      this.connections[peers[i]].close();
+    }
+    util.setZeroTimeout(function(){
+      self._socket.close();
+    });
   }
-  util.setZeroTimeout(function(){
-    self._socket.close();
-  });
   this.emit('close');
 };
 
@@ -195,7 +202,8 @@ Peer.prototype._attachConnectionListeners = function(connection) {
 // 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.')
+    this._abort('peer-destroyed', 'This Peer has been destroyed and is no longer able to make connections.');
+    return;
   }
 
   options = util.extend({

Vissa filer visades inte eftersom för många filer har ändrats