Skip to content

Commit

Permalink
fix: handling disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Dec 7, 2017
1 parent 3f649a8 commit 47a8e33
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ module.exports = class Connection extends EventEmitter {
pull(
pushable,
conn,
pull.onEnd(() => delete this._connection)
pull.onEnd(() => {
delete this._connection
this.emit('disconnect')
})
)
this.emit('connect', pushable)
})
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class PubSubRoom extends EventEmitter {
conn = new Connection(peer, this._ipfs, this)
conn.on('error', (err) => this.emit('error', err))
this._connections[peer] = conn

conn.once('disconnect', () => {
delete this._connections[peer]
this._peers = this._peers.filter((p) => p !== peer)
this.emit('peer left', peer)
})
}

// We should use the same sequence number generation as js-libp2p-floosub does:
Expand Down

0 comments on commit 47a8e33

Please sign in to comment.