Git Product home page Git Product logo

Comments (5)

detarkende avatar detarkende commented on May 24, 2024

Interestingly, if I manually empty out the _selections array after the torrent is added, then everything works fine.
However, the deselect method doesn't have the same effect.

If I do this:

/** @type {WebTorrent.Torrent} */
const torrent = await new Promise((resolve, reject) => {
    // ...
});
torrent._selections.length = 0;

then the progress stays the same. (meaning that if 46% of the torrent was downloaded, then after the script starts, it doesn't download new chunks, unless a stream is opened. So it works as I would expect it to).

However, if I try to deselect everything the correct way (without using internal properties)...

/** @type {WebTorrent.Torrent} */
const torrent = await new Promise((resolve, reject) => {
    // ...
});
torrent.deselect(0, torrent.pieces.length - 1, 0)

then it doesn't work.

It's weird... I can use this workaround for now, but I would rather not use internal properties.
I'll comment further details if I find the root cause of this issue.

from webtorrent.

detarkende avatar detarkende commented on May 24, 2024

Here's what I've found (these line numbers are all in libs/torrent.js):

on lines 685-695, in the _markUnverified method, all unverified pieces are selected (so 1 item per piece is added to _selections):

_markUnverified (index) {
    const len = (index === this.pieces.length - 1)
      ? this.lastPieceLength
      : this.pieceLength
    this.pieces[index] = new Piece(len)
    this.bitfield.set(index, false)
    this.select(index, index, 1)
    this.files.forEach(file => {
      if (file.done && file.includes(index)) file.done = false
    })
  }

This is fine, but it's important later.

On lines 1052-1057, in the deselect method, in the for loop, we're only looking for selection items that are exact matches. I believe this is the culprit:

// current implementation:
deselect (start, end, priority) {
  if (this.destroyed) throw new Error('torrent is destroyed')

  priority = Number(priority) || 0
  this._debug('deselect %s-%s (priority %s)', start, end, priority)

  for (let i = 0; i < this._selections.length; ++i) {
    const s = this._selections[i]
    const isPieceInInterval = s.from >= from && s.to <= to 
    if (s.from === start && s.to === end && s.priority === priority) {
      this._selections.splice(i, 1)
      break
    }
  }

  this._updateSelections()
}

Is this intentional? I have a couple of questions about this method:

  1. Why is the priority argument necessary here? Don't we just want to remove the given selections? I don't see why knowing its current priority is necessary.
  2. Why are we only matching exact selections? Wouldn't it make more sense to remove every selection item that falls into the given from-to interval?

I believe this could be the partial cause of the "file.deselect supposedly not working" issue (#164)

from webtorrent.

ThaUnknown avatar ThaUnknown commented on May 24, 2024

this was meant to be fixed in #2115, but alex has since abandoned it and noone has picked up the PR, feel free to fix it

from webtorrent.

detarkende avatar detarkende commented on May 24, 2024

@ThaUnknown Thanks for pointing me in the right direction.

I opened a draft PR with a solution that I came up with. Could you or any other maintainers/contributers check it out?

I'm mainly looking for feedback on it atm, since I'm a first time contributer, so I'm looking for holes in my implementation.

Your feedback would be greatly appreciated 🙂

from webtorrent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.