From 7e0d447c3e8cc0c4739fa3c06e13de331da6bc2b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Sep 2018 10:56:46 +0200 Subject: Try to reduce dl impact on slow computers (web) On slow computers with a fast network, web browsers may no longer respond because webtorrent is downloading chunks too fast. The main issue is that you cannot stream a torrent video: the web browser does not have time to play the video, it is too busy downloading the video chunks. On web browser supporting [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback#See_also), we download chunks with a lower priority allowing the web browser to execute other tasks (like playing the video). --- lib/torrent.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/torrent.js b/lib/torrent.js index 0213f3b..4ceca06 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -1156,7 +1156,11 @@ class Torrent extends EventEmitter { if (wire.requests.length >= minOutstandingRequests) return const maxOutstandingRequests = getBlockPipelineLength(wire, PIPELINE_MAX_DURATION) - trySelectWire(false) || trySelectWire(true) + if (typeof window !== 'undefined' && typeof window.requestIdleCallback === 'function') { + window.requestIdleCallback(function () { trySelectWire(false) || trySelectWire(true) }) + } else { + trySelectWire(false) || trySelectWire(true) + } function genPieceFilterFunc (start, end, tried, rank) { return i => i >= start && i <= end && !(i in tried) && wire.peerPieces.get(i) && (!rank || rank(i)) -- cgit v1.2.3