瀏覽代碼

refactoring removing connection from peer's connections
clean connection's lostMessages

afrokick 6 年之前
父節點
當前提交
09d556f281
共有 3 個文件被更改,包括 18 次插入18 次删除
  1. 1 9
      lib/dataconnection.ts
  2. 1 9
      lib/mediaconnection.ts
  3. 16 0
      lib/peer.ts

+ 1 - 9
lib/dataconnection.ts

@@ -174,15 +174,7 @@ export class DataConnection extends BaseConnection {
     }
 
     if (this.provider) {
-      const connections = this.provider.connections[this.peer];
-
-      if (connections) {
-        const index = connections.indexOf(this);
-
-        if (index !== -1) {
-          connections.splice(index, 1);
-        }
-      }
+      this.provider._removeConnection(this);
 
       this.provider = null;
     }

+ 1 - 9
lib/mediaconnection.ts

@@ -102,15 +102,7 @@ export class MediaConnection extends BaseConnection {
     this._remoteStream = null;
 
     if (this.provider) {
-      const connections = this.provider.connections[this.peer];
-
-      if (connections) {
-        const index = connections.indexOf(this);
-
-        if (index !== -1) {
-          connections.splice(index, 1);
-        }
-      }
+      this.provider._removeConnection(this);
 
       this.provider = null;
     }

+ 16 - 0
lib/peer.ts

@@ -400,6 +400,22 @@ export class Peer extends EventEmitter {
     this._connections.get(peerId).push(connection);
   }
 
+  //TODO should be private
+  _removeConnection(connection: BaseConnection): void {
+    const connections = this._connections.get(connection.peer);
+
+    if (connections) {
+      const index = connections.indexOf(connection);
+
+      if (index !== -1) {
+        connections.splice(index, 1);
+      }
+    }
+
+    //remove from lost messages
+    this._lostMessages.delete(connection.connectionId);
+  }
+
   /** Retrieve a data/media connection for this peer. */
   getConnection(peerId: string, connectionId: string): null | BaseConnection {
     const connections = this._connections.get(peerId);