fix empty infoHash issue

This commit is contained in:
Nate Goldman
2016-03-07 10:51:32 -08:00
parent 383659ad1a
commit de7bc442a7

View File

@@ -54,7 +54,7 @@ function init () {
state.client = new WebTorrent() state.client = new WebTorrent()
state.client.on('warning', onWarning) state.client.on('warning', onWarning)
state.client.on('error', onError) state.client.on('error', onError)
state.client.on('torrent', updateTorrentData) state.client.on('torrent', saveTorrentData)
// The UI is built with virtual-dom, a minimalist library extracted from React // The UI is built with virtual-dom, a minimalist library extracted from React
// The concepts--one way data flow, a pure function that renders state to a // The concepts--one way data flow, a pure function that renders state to a
@@ -74,7 +74,7 @@ function init () {
// Resume all saved torrents now that state is loaded and vdom is ready // Resume all saved torrents now that state is loaded and vdom is ready
resumeAllTorrents() resumeAllTorrents()
document.addEventListener('unload', saveState) window.addEventListener('beforeunload', saveState)
// listen for messages from the main process // listen for messages from the main process
setupIpc() setupIpc()
@@ -281,21 +281,22 @@ function addTorrent (torrentId) {
var exists = state.saved.torrents.find((x) => x.infoHash === torrent.infoHash) var exists = state.saved.torrents.find((x) => x.infoHash === torrent.infoHash)
if (exists) return window.alert('That torrent is already downloading.') if (exists) return window.alert('That torrent is already downloading.')
// only infoHash is available until torrent is ready // save only if infoHash is available
state.saved.torrents.push({ if (torrent.infoHash) {
infoHash: torrent.infoHash state.saved.torrents.push({
}) infoHash: torrent.infoHash
})
} else {
torrent.on('infoHash', () => saveTorrentData(torrent))
}
saveState() saveState()
} }
// add torrent metadata to state once it's available // add torrent metadata to state once it's available
function updateTorrentData (torrent) { function saveTorrentData (torrent) {
// get torrent index var ix = state.saved.torrents.findIndex((x) => x.infoHash === torrent.infoHash)
var i = state.saved.torrents.findIndex((x) => x.infoHash === torrent.infoHash) var data = {
if (i === -1) return
// add the goods
state.saved.torrents[i] = {
name: torrent.name, name: torrent.name,
magnetURI: torrent.magnetURI, magnetURI: torrent.magnetURI,
infoHash: torrent.infoHash, infoHash: torrent.infoHash,
@@ -305,6 +306,9 @@ function updateTorrentData (torrent) {
announce: torrent.announce announce: torrent.announce
} }
if (ix === -1) state.saved.torrents.push(data)
else state.saved.torrents[ix] = data
saveState() saveState()
} }