Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WebTorrent add and remove events and docs update #2766

Merged
merged 5 commits into from
May 31, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ buf.name = 'Some file name'
client.seed(buf, cb)
```

## `client.on('add', function (torrent) {})`

Emitted when a torrent is added to client.torrents. This allows attaching to torrent events that may be emitted before the client 'torrent' event is emitted. See the torrent section for more info on what methods a `torrent` has.

## `client.on('remove', function (torrent) {})`

Emitted when a torrent is removed from client.torrents. See the torrent section for more info on what methods a `torrent` has.

## `client.on('torrent', function (torrent) {})`

Emitted when a torrent is ready to be used (i.e. metadata is available and store is
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export default class WebTorrent extends EventEmitter {
torrent.once('ready', onReady)
torrent.once('close', onClose)

this.emit('add', torrent)
return torrent
}

Expand Down Expand Up @@ -411,11 +412,14 @@ export default class WebTorrent extends EventEmitter {
_remove (torrent, opts, cb) {
if (!torrent) return
if (typeof opts === 'function') return this._remove(torrent, null, opts)
this.torrents.splice(this.torrents.indexOf(torrent), 1)
const index = this.torrents.indexOf(torrent)
if (index === -1) return
this.torrents.splice(index, 1)
torrent.destroy(opts, cb)
if (this.dht) {
this.dht._tables.remove(torrent.infoHash)
}
this.emit('remove', torrent)
}

address () {
Expand Down