Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2017-02-14 05:02:43 +0300
committerFeross Aboukhadijeh <feross@feross.org>2017-02-14 05:02:43 +0300
commit6ef2785e4bbed07f2429725ed2b05bd91fc36233 (patch)
tree49a5ca41e506577144208866dd4a1e92d0ba180c /lib
parent01d8f470ca080994ab712cf16cb246936447d6e1 (diff)
wait to notify() or updateInterest() at end of GC
To prevent it from possibly being called twice, which will do extra work
Diffstat (limited to 'lib')
-rw-r--r--lib/torrent.js21
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/torrent.js b/lib/torrent.js
index 89ad91d..ae3e256 100644
--- a/lib/torrent.js
+++ b/lib/torrent.js
@@ -893,7 +893,7 @@ Torrent.prototype.deselect = function (start, end, priority) {
for (var i = 0; i < self._selections.length; ++i) {
var s = self._selections[i]
if (s.from === start && s.to === end && s.priority === priority) {
- self._selections.splice(i--, 1)
+ self._selections.splice(i, 1)
break
}
}
@@ -1131,13 +1131,16 @@ Torrent.prototype._updateSelections = function () {
Torrent.prototype._gcSelections = function () {
var self = this
- for (var i = 0; i < self._selections.length; i++) {
+ var i
+ var toRemove = []
+
+ for (i = 0; i < self._selections.length; ++i) {
var s = self._selections[i]
var oldOffset = s.offset
// check for newly downloaded pieces in selection
while (self.bitfield.get(s.from + s.offset) && s.from + s.offset < s.to) {
- s.offset++
+ s.offset += 1
}
if (oldOffset !== s.offset) s.notify()
@@ -1145,8 +1148,16 @@ Torrent.prototype._gcSelections = function () {
if (!self.bitfield.get(s.from + s.offset)) continue
// remove fully downloaded selection
- self._selections.splice(i--, 1) // decrement i to offset splice
- s.notify() // TODO: this may notify twice in a row. is this a problem?
+ toRemove.push(i)
+ }
+
+ if (toRemove.length > 0) {
+ // Since self._selections is being modified in-place, it's necessary to
+ // traverse `toRemove` in reverse.
+ for (i = toRemove.length - 1; i >= 0; --i) {
+ self._selections.splice(i, 1)
+ }
+ s.notify()
self._updateInterest()
}