Эх сурвалжийг харах

get rid of callbacks, return dc upon creation

ericz 12 жил өмнө
parent
commit
8fa6ea1b4d
2 өөрчлөгдсөн 22 нэмэгдсэн , 39 устгасан
  1. 9 21
      lib/connection.js
  2. 13 18
      lib/peer.js

+ 9 - 21
lib/connection.js

@@ -1,7 +1,7 @@
 /**
  * A DataChannel PeerConnection between two Peers.
  */
-function DataConnection(id, peer, socket, cb, options) {
+function DataConnection(id, peer, socket, options) {
   if (!(this instanceof DataConnection)) return new DataConnection(options);
   EventEmitter.call(this);
 
@@ -16,10 +16,9 @@ function DataConnection(id, peer, socket, cb, options) {
   
   this.id = id;
   this.peer = peer;
-  this._originator = (options.sdp === undefined);
-  this._cb = cb;
   this.metadata = options.metadata;
 
+  this._originator = (options.sdp === undefined);
   this._socket = socket;
 
   // Firefoxism: connectDataConnection ports.
@@ -133,7 +132,6 @@ DataConnection.prototype._configureDataChannel = function() {
     util.log('Data channel connection success');
     self.open = true;
     self.emit('open');
-    self._callback(null, self);
   };
   this._dc.onmessage = function(e) {
     self._handleDataMessage(e);
@@ -166,7 +164,7 @@ DataConnection.prototype._makeOffer = function() {
         metadata: self.metadata
       });
     }, function(err) {
-      self._callback('Failed to setLocalDescription');
+      self.emit('error', 'Failed to setLocalDescription');
       util.log('Failed to setLocalDescription, ', err);
     });
   });
@@ -186,11 +184,11 @@ DataConnection.prototype._makeAnswer = function() {
         dst: self.peer
       });
     }, function(err) {
-      self._callback('Failed to setLocalDescription');
+      self.emit('error', 'Failed to setLocalDescription');
       util.log('Failed to setLocalDescription, ', err)
     });
   }, function(err) {
-    self._callback('Failed to create answer');
+    self.emit('error', 'Failed to create answer');
     util.log('Failed to create answer, ', err)
   });
 };
@@ -205,17 +203,8 @@ DataConnection.prototype._cleanup = function() {
     this._pc.close();
     this._pc = null;
   }
-  this.emit('close', this.peer);
 };
 
-// Make sure _cb only gets called once
-DataConnection.prototype._callback = function(err, dc) {
-  if (this._cb) {
-    this._cb(err, dc);
-    delete this._cb;
-  }
-}
-
 
 // Handles a DataChannel message.
 DataConnection.prototype._handleDataMessage = function(e) {
@@ -241,7 +230,7 @@ DataConnection.prototype._handleDataMessage = function(e) {
  */
 
 /** Allows user to close connection. */
-DataConnection.prototype.close = function(reason) {
+DataConnection.prototype.close = function() {
   this._cleanup();
   var self = this;
   if (this.open) {
@@ -250,10 +239,9 @@ DataConnection.prototype.close = function(reason) {
       dst: self.peer,
       src: self.id,
     });
-  } else {
-    this._callback(reason);
   }
   this.open = false;
+  this.emit('close', this.peer);
 };
 
 
@@ -292,7 +280,7 @@ DataConnection.prototype.handleSDP = function(message) {
       self._makeAnswer();
     }
   }, function(err) {
-    self._callback('Failed to setRemoteDescription');
+    self.emit('error', 'Failed to setRemoteDescription');
     util.log('Failed to setRemoteDescription, ', err);
   });
 };
@@ -307,7 +295,7 @@ DataConnection.prototype.handleCandidate = function(message) {
 
 DataConnection.prototype.handleLeave = function() {
   util.log('Peer ' + this.peer + ' disconnected');
-  this._cleanup();
+  this.close();
 };
 
 /*

+ 13 - 18
lib/peer.js

@@ -83,8 +83,8 @@ Peer.prototype._handleServerJSONMessage = function(message) {
       util.log(message.msg);
       break;
     case 'ID-TAKEN':
-      this.emit('error', message.msg);
-      this.destroy('ID `'+this.id+'` is taken');
+      this.emit('error', 'ID `'+this.id+'` is taken');
+      this.destroy();
       this.emit('close');
       break;
     case 'OFFER':
@@ -93,14 +93,10 @@ Peer.prototype._handleServerJSONMessage = function(message) {
         sdp: message.sdp,
         config: this._options.config,
       };
-      var self = this;
-      var connection = new DataConnection(this.id, peer, this._socket, function(err, connection) {
-        if (!err) {
-          self.emit('connection', connection, message.metadata);
-        }
-      }, options);
+      var connection = new DataConnection(this.id, peer, this._socket, options);
       this._attachConnectionListeners(connection);
       this.connections[peer] = connection;
+      this.emit('connection', connection, message.metadata);
       break;
     case 'EXPIRE':
       if (connection) {
@@ -142,11 +138,11 @@ Peer.prototype._processQueue = function() {
 };
 
 
-Peer.prototype._cleanup = function(reason) {
+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(reason);
+    this.connections[peers[i]].close();
   }
   util.setZeroTimeout(function(){
     self._socket.close();
@@ -169,26 +165,25 @@ Peer.prototype._attachConnectionListeners = function(connection) {
  * is waiting for an ID. */
 // TODO: pause XHR streaming when not in use and start again when this is
 // called.
-Peer.prototype.connect = function(peer, metadata, cb) {
-  if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
-
+Peer.prototype.connect = function(peer, metadata, options) {
   if (!this.id) {
     this._queued.push(Array.prototype.slice.apply(arguments));
     return;
   }
 
-  var options = {
+  options = util.extend(options, {
     metadata: metadata,
     config: this._options.config,
-  };
-  var connection = new DataConnection(this.id, peer, this._socket, cb, options);
+  });
+  var connection = new DataConnection(this.id, peer, this._socket, options);
   this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
+  return connection;
 };
 
-Peer.prototype.destroy = function(reason) {
-  this._cleanup(reason);
+Peer.prototype.destroy = function() {
+  this._cleanup();
 };