diff options
-rw-r--r-- | lib/rarity-map.js | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/lib/rarity-map.js b/lib/rarity-map.js index f97e602..bc69921 100644 --- a/lib/rarity-map.js +++ b/lib/rarity-map.js @@ -5,28 +5,26 @@ */ class RarityMap { constructor (torrent) { - const self = this + this._torrent = torrent + this._numPieces = torrent.pieces.length + this._pieces = new Array(this._numPieces).fill(0) - self._torrent = torrent - self._numPieces = torrent.pieces.length - self._pieces = new Array(self._numPieces).fill(0) - - self._onWire = wire => { - self.recalculate() - self._initWire(wire) + this._onWire = wire => { + this.recalculate() + this._initWire(wire) } - self._onWireHave = index => { - self._pieces[index] += 1 + this._onWireHave = index => { + this._pieces[index] += 1 } - self._onWireBitfield = () => { - self.recalculate() + this._onWireBitfield = () => { + this.recalculate() } - self._torrent.wires.forEach(wire => { - self._initWire(wire) + this._torrent.wires.forEach(wire => { + this._initWire(wire) }) - self._torrent.on('wire', self._onWire) - self.recalculate() + this._torrent.on('wire', this._onWire) + this.recalculate() } /** @@ -61,31 +59,28 @@ class RarityMap { } destroy () { - const self = this - self._torrent.removeListener('wire', self._onWire) - self._torrent.wires.forEach(wire => { - self._cleanupWireEvents(wire) + this._torrent.removeListener('wire', this._onWire) + this._torrent.wires.forEach(wire => { + this._cleanupWireEvents(wire) }) - self._torrent = null - self._pieces = null + this._torrent = null + this._pieces = null - self._onWire = null - self._onWireHave = null - self._onWireBitfield = null + this._onWire = null + this._onWireHave = null + this._onWireBitfield = null } _initWire (wire) { - const self = this - - wire._onClose = function () { - self._cleanupWireEvents(wire) + wire._onClose = () => { + this._cleanupWireEvents(wire) for (let i = 0; i < this._numPieces; ++i) { - self._pieces[i] -= wire.peerPieces.get(i) + this._pieces[i] -= wire.peerPieces.get(i) } } - wire.on('have', self._onWireHave) - wire.on('bitfield', self._onWireBitfield) + wire.on('have', this._onWireHave) + wire.on('bitfield', this._onWireBitfield) wire.once('close', wire._onClose) } |