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

modernize lib/rarity-map.js #1485

Merged
merged 4 commits into from Aug 27, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

stop using self

  • Loading branch information
jimmywarting committed Aug 26, 2018
commit 80b40226ba5b86edbe04809d1822bf719090fe46
@@ -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)

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

What is the fill here for? If it's not needed I think it will just consume O(n) to fill it and can be removed.


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)
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.