Skip to content

Commit

Permalink
Merge pull request #3004 from brave/webtorrent-stuck
Browse files Browse the repository at this point in the history
WebTorrent: Fix stats remain stuck after download finishes
  • Loading branch information
yrliou authored Jul 27, 2019
2 parents 236c089 + 01bd099 commit e8911cb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ export const addTorrentEvents = (torrent: Torrent) => {
webtorrentActions.progressUpdated(torrent)
})
torrent.on('infoHash', () => {
console.log('infoHash event')
webtorrentActions.infoUpdated(torrent)
})
torrent.on('metadata', () => {
console.log('metadata event')
webtorrentActions.infoUpdated(torrent)
})
torrent.on('download', throttle((bytes: number) => {
Expand All @@ -26,8 +24,13 @@ export const addTorrentEvents = (torrent: Torrent) => {
torrent.on('upload', throttle((bytes: number) => {
webtorrentActions.progressUpdated(torrent)
}, 1000))
torrent.on('done', () => {
webtorrentActions.progressUpdated(torrent)
})
torrent.on('wire', () => {
webtorrentActions.progressUpdated(torrent)
})
torrent.on('ready', () => {
console.log('ready', torrent)
createServer(torrent, (serverURL: string) => {
webtorrentActions.serverUpdated(torrent, serverURL)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import { Instance } from 'webtorrent'
import webtorrentActions from '../actions/webtorrentActions'

let interval: number

export const addWebtorrentEvents = (webtorrent: Instance) => {
webtorrent.on('error', (e: Error | string) => {
console.log('WebTorrent error: ', e)
})

// Always update stats at least once every 5 seconds, even when no torrent
// events fire
interval = setInterval(() => {
webtorrent.torrents.forEach(torrent => {
webtorrentActions.progressUpdated(torrent)
})
}, 5000)
}

export const removeWebTorrentEvents = (webtorrent: Instance) => {
clearInterval(interval)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import * as WebTorrent from 'webtorrent'
import { addTorrentEvents } from './events/torrentEvents'
import { addWebtorrentEvents } from './events/webtorrentEvents'
import { addWebtorrentEvents, removeWebTorrentEvents } from './events/webtorrentEvents'
import { AddressInfo } from 'net'
import { Instance } from 'parse-torrent'

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

const maybeDestroyWebTorrent = () => {
if (!webTorrent || webTorrent.torrents.length !== 0) return
removeWebTorrentEvents(webTorrent)
webTorrent.destroy()
webTorrent = undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import reducers from '../../../../brave_webtorrent/extension/background/reducers/index'
import { applicationState } from '../../testData'

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

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

0 comments on commit e8911cb

Please sign in to comment.