Skip to content

Commit c2fd18c

Browse files
committed
WebTorrent: Fix stats remain stuck after download finishes
Fixes: brave/brave-browser#5352 - Always update stats at least once every 5 seconds, even when no torrent events fire - remove stray console logs - Listen for 'wire' (new peer) events - Listen for 'done' event
1 parent 4fd2ce3 commit c2fd18c

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

components/brave_webtorrent/extension/background/events/torrentEvents.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ export const addTorrentEvents = (torrent: Torrent) => {
1313
webtorrentActions.progressUpdated(torrent)
1414
})
1515
torrent.on('infoHash', () => {
16-
console.log('infoHash event')
1716
webtorrentActions.infoUpdated(torrent)
1817
})
1918
torrent.on('metadata', () => {
20-
console.log('metadata event')
2119
webtorrentActions.infoUpdated(torrent)
2220
})
2321
torrent.on('download', throttle((bytes: number) => {
@@ -26,8 +24,13 @@ export const addTorrentEvents = (torrent: Torrent) => {
2624
torrent.on('upload', throttle((bytes: number) => {
2725
webtorrentActions.progressUpdated(torrent)
2826
}, 1000))
27+
torrent.on('done', () => {
28+
webtorrentActions.progressUpdated(torrent)
29+
})
30+
torrent.on('wire', () => {
31+
webtorrentActions.progressUpdated(torrent)
32+
})
2933
torrent.on('ready', () => {
30-
console.log('ready', torrent)
3134
createServer(torrent, (serverURL: string) => {
3235
webtorrentActions.serverUpdated(torrent, serverURL)
3336
})

components/brave_webtorrent/extension/background/events/webtorrentEvents.ts

+15
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { Instance } from 'webtorrent'
6+
import webtorrentActions from '../actions/webtorrentActions'
7+
8+
let interval: number
69

710
export const addWebtorrentEvents = (webtorrent: Instance) => {
811
webtorrent.on('error', (e: Error | string) => {
912
console.log('WebTorrent error: ', e)
1013
})
14+
15+
// Always update stats at least once every 5 seconds, even when no torrent
16+
// events fire
17+
interval = setInterval(() => {
18+
webtorrent.torrents.forEach(torrent => {
19+
webtorrentActions.progressUpdated(torrent)
20+
})
21+
}, 5000)
22+
}
23+
24+
export const removeWebTorrentEvents = (webtorrent: Instance) => {
25+
clearInterval(interval)
1126
}

components/brave_webtorrent/extension/background/webtorrent.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import * as WebTorrent from 'webtorrent'
66
import { addTorrentEvents } from './events/torrentEvents'
7-
import { addWebtorrentEvents } from './events/webtorrentEvents'
7+
import { addWebtorrentEvents, removeWebTorrentEvents } from './events/webtorrentEvents'
88
import { AddressInfo } from 'net'
99
import { Instance } from 'parse-torrent'
1010

@@ -61,6 +61,7 @@ export const findTorrent = (infoHash: string) => {
6161

6262
const maybeDestroyWebTorrent = () => {
6363
if (!webTorrent || webTorrent.torrents.length !== 0) return
64+
removeWebTorrentEvents(webTorrent)
6465
webTorrent.destroy()
6566
webTorrent = undefined
6667
}

components/test/brave_webtorrent/background/reducers/index_test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import reducers from '../../../../brave_webtorrent/extension/background/reducers/index'
66
import { applicationState } from '../../testData'
77

8-
// this import seems to trigger createStore and get an undefined reducer
8+
// these imports seems to trigger createStore and get an undefined reducer
99
jest.mock('../../../../brave_webtorrent/extension/background/events/torrentEvents')
10+
jest.mock('../../../../brave_webtorrent/extension/background/events/webtorrentEvents')
1011

1112
describe('webtorrent reducers test', () => {
1213
it('reducers are a combined reducer function', () => {

0 commit comments

Comments
 (0)