webtorrent / bittorrent-protocol

Simple, robust, BitTorrent peer wire protocol implementation
https://webtorrent.io
MIT License
349 stars 71 forks source link

Emit the wire's peerId on close #66

Closed draeder closed 2 years ago

draeder commented 3 years ago

What version of this package are you using? 3.4.2

What problem do you want to solve? I'm tracking peers with an extension and would like to more easily remove peers from the list of peers when their wire is closed. The problem is when the wire closes it isn't clear which peerId the close event was for. This makes removing left peers unnecessarily difficult.

What do you think is the correct solution to this problem? Emit the wire's peerId in the 'close' event when the wire closes.

  destroy () {
    if (this.destroyed) return
    this.destroyed = true
    this._debug('destroy')
    this.emit('close', this.peerId) // <----- Emit the wire's peerId
    this.end()
  }

Are you willing to submit a pull request to implement this change? Yes

DiegoRBaquero commented 3 years ago

I don't understand why you need to use events for this. You can directly access the peerId of the peer you are destroying

DiegoRBaquero commented 2 years ago

@draeder Please share your flow

draeder commented 2 years ago

@DiegoRBaquero Apologies, I haven't worked on the project recently that led me to believe I needed this. Please feel free to close this issue and associated PR. If it turns out I still need it when I circle back to the project, I will open a new issue/PR.

DiegoRBaquero commented 2 years ago

Of course! And what I meant is that you can do this:

const peerId = wire.peerId
wire.destroy()

There's no need to tap into wire's events

draeder commented 2 years ago

Perfect thanks!