Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

fix: create separate events for connection tracking #328

Merged
merged 5 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,17 @@ Emitted when the switch encounters an error.

### `switch.on('peer-mux-closed', (peer) => {})`

- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection.
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection with.

### `switch.on('connection:start', (peer) => {})`
This will be triggered anytime a new connection is created.

- `peer`: is instance of [PeerInfo][] that has info of the peer we have just started a connection with.

### `switch.on('connection:end', (peer) => {})`
This will be triggered anytime an existing connection, regardless of state, is removed from the switch's internal connection tracking.

- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a connection with.

### `switch.on('start', () => {})`

Expand Down
1 change: 1 addition & 0 deletions src/connection/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class BaseConnection extends EventEmitter {
* @returns {void}
*/
_onDisconnected () {
this.switch.connection.remove(this)
this.log('disconnected from %s', this.theirB58Id)
this.emit('close')
this.removeAllListeners()
Expand Down
2 changes: 0 additions & 2 deletions src/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ class ConnectionFSM extends BaseConnection {
_onDisconnecting () {
this.log('disconnecting from %s', this.theirB58Id, Boolean(this.muxer))

this.switch.connection.remove(this)

delete this.switch.conns[this.theirB58Id]

let tasks = []
Expand Down
16 changes: 13 additions & 3 deletions src/connection/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ class ConnectionManager {
// Only add it if it's not there
if (!this.get(connection)) {
this.connections[connection.theirB58Id].push(connection)
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
this.switch.emit('connection:start', connection.theirPeerInfo)
if (connection.getState() === 'MUXED') {
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
} else {
connection.once('muxed', () => this.switch.emit('peer-mux-established', connection.theirPeerInfo))
}
}
}

Expand Down Expand Up @@ -81,8 +86,10 @@ class ConnectionManager {
remove (connection) {
// No record of the peer, disconnect it
if (!this.connections[connection.theirB58Id]) {
connection.theirPeerInfo.disconnect()
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
if (connection.theirPeerInfo) {
connection.theirPeerInfo.disconnect()
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
}
return
}

Expand All @@ -99,6 +106,9 @@ class ConnectionManager {
connection.theirPeerInfo.disconnect()
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
dirkmc marked this conversation as resolved.
Show resolved Hide resolved
}

// A tracked connection was closed, let the world know
this.switch.emit('connection:end', connection.theirPeerInfo)
}

/**
Expand Down