From 11f6158445e7b54785b5764cdd9f1e64255aaec1 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 27 Mar 2022 13:09:55 -0700 Subject: refactor: trigger _onWireWithMetadata after selections are updated (#2103) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Diego Rodríguez Baquero --- lib/torrent.js | 19 ++++++++++--------- 1 file 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 -- cgit v1.2.3