Skip to content

Commit

Permalink
fix: lastFinished
Browse files Browse the repository at this point in the history
fix: close app on webtorrent crash
feat: torrent loading status and errors
  • Loading branch information
ThaUnknown committed Aug 25, 2024
1 parent 4712ae9 commit cc00aca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions common/modules/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ client.on('warn', ({ detail }) => {
toast.warning('Torrent Warning', { description: '' + (detail.message || detail) })
})

client.on('info', ({ detail }) => {
debug(`Info: ${detail.message || detail}`)
toast('Torrent Info', { description: '' + (detail.message || detail) })
})

export async function add (torrentID, hide) {
if (torrentID) {
debug('Adding torrent', { torrentID })
Expand Down
25 changes: 21 additions & 4 deletions common/modules/webtorrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class TorrentClient extends WebTorrent {
setInterval(() => {
if (this.torrents[0]?.pieces) this.dispatch('progress', this.current?.progress)
}, 2000)
this.on('torrent', this.handleTorrent.bind(this))
this.on('torrent', this.torrentReady.bind(this))

const createServer = controller => {
this.server = this.createServer({ controller }, serverMode)
Expand Down Expand Up @@ -142,7 +142,7 @@ export default class TorrentClient extends WebTorrent {
if (torrent) this.addTorrent(torrent, JSON.parse(localStorage.getItem('lastFinished')))
}

async handleTorrent (torrent) {
async torrentReady (torrent) {
debug('Got torrent metadata: ' + torrent.name)
const files = torrent.files.map(file => {
return {
Expand All @@ -156,6 +156,12 @@ export default class TorrentClient extends WebTorrent {
})
this.dispatch('files', files)
this.dispatch('magnet', { magnet: torrent.magnetURI, hash: torrent.infoHash })
setTimeout(() => {
if (torrent.destroyed) return
if (torrent.progress !== 1) {
if (torrent.numPeers === 0) this.dispatchError('No peers found for torrent, try using a different torrent.')
}
}, 10000).unref?.()
localStorage.setItem('torrent', JSON.stringify([...torrent.torrentFile])) // this won't work on mobile, but really it only speeds stuff up by ~1-2 seconds since magnet data doesn't need to be resolved
}

Expand Down Expand Up @@ -283,7 +289,7 @@ export default class TorrentClient extends WebTorrent {
debug('Adding torrent: ' + data)
const existing = await this.get(data)
if (existing) {
if (existing.ready) this.handleTorrent(existing)
if (existing.ready) this.torrentReady(existing)
return
}
localStorage.setItem('lastFinished', 'false')
Expand All @@ -298,8 +304,19 @@ export default class TorrentClient extends WebTorrent {
deselect: this.settings.torrentStreamedDownload
})

torrent.once('verified', () => {
if (!torrent.ready && !skipVerify) this.dispatch('info', 'Detected already downloaded files. Verifying file integrity. This might take a minute...')
})

setTimeout(() => {
if (torrent.destroyed || skipVerify) return
if (!torrent.progress || !torrent.ready) {
if (torrent.numPeers === 0) this.dispatchError('No peers found for torrent, try using a different torrent.')
}
}, 10000).unref?.()

torrent.once('done', () => {
if (SUPPORTS.torrentPersist && this.settings.torrentPath) localStorage.setItem('lastFinished', 'true')
if (this.settings.torrentPathNew) localStorage.setItem('lastFinished', 'true')
})
}

Expand Down
1 change: 1 addition & 0 deletions electron/src/main/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default class App {
ipcMain.on('ui-devtools', ({ sender }) => sender.openDevTools())

this.mainWindow.on('closed', () => this.destroy())
this.webtorrentWindow.on('closed', () => this.destroy())
ipcMain.on('close', () => this.destroy())
app.on('before-quit', e => {
if (this.destroyed) return
Expand Down

0 comments on commit cc00aca

Please sign in to comment.