diff options
author | Alex <alxmorais8@msn.com> | 2022-03-27 23:09:55 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-27 23:09:55 +0300 |
commit | 11f6158445e7b54785b5764cdd9f1e64255aaec1 (patch) | |
tree | 349d012d5ef537caecc4000916eb319e881e2c45 /lib/torrent.js | |
parent | 38848a5410188f935d4c1d9b4476b5ac733e8b9f (diff) |
refactor: trigger _onWireWithMetadata after selections are updated (#2103)
Co-authored-by: Diego RodrÃguez Baquero <github@diegorbaquero.com>
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 99947eb..7c49e6e 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -529,14 +529,6 @@ class Torrent extends EventEmitter { this.bitfield = new BitField(this.pieces.length) - this.wires.forEach(wire => { - // If we didn't have the metadata at the time ut_metadata was initialized for this - // wire, we still want to make it available to the peer in case they request it. - if (wire.ut_metadata) wire.ut_metadata.setMetadata(this.metadata) - - this._onWireWithMetadata(wire) - }) - // Emit 'metadata' before 'ready' and 'done' this.emit('metadata') @@ -690,6 +682,15 @@ class Torrent extends EventEmitter { // In case any selections were made before torrent was ready this._updateSelections() + + // Start requesting pieces after we have initially verified them + this.wires.forEach(wire => { + // If we didn't have the metadata at the time ut_metadata was initialized for this + // wire, we still want to make it available to the peer in case they request it. + if (wire.ut_metadata) wire.ut_metadata.setMetadata(this.metadata) + + this._onWireWithMetadata(wire) + }) } destroy (opts, cb) { @@ -1109,7 +1110,7 @@ class Torrent extends EventEmitter { // More info: https://github.com/webtorrent/bittorrent-protocol#extension-api this.emit('wire', wire, addr) - if (this.metadata) { + if (this.ready) { queueMicrotask(() => { // This allows wire.handshake() to be called (by Peer.onHandshake) before any // messages get sent on the wire |