Fix state loading and saving

This is the rest of PR #56
This commit is contained in:
DC
2016-03-06 21:19:11 -08:00
parent 3e43100e2c
commit 5aa1cce09e

View File

@@ -43,7 +43,6 @@ 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', saveState)
// 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
@@ -66,6 +65,7 @@ function init () {
// All state lives in state.js. `state.saved` is read from and written to a // All state lives in state.js. `state.saved` is read from and written to a
// file. All other state is ephemeral. Here we'll load state.saved: // file. All other state is ephemeral. Here we'll load state.saved:
loadState() loadState()
document.addEventListener('unload', saveState)
// For easy debugging in Developer Tools // For easy debugging in Developer Tools
global.state = state global.state = state
@@ -256,20 +256,26 @@ function isNotTorrentFile (file) {
return !isTorrentFile(file) return !isTorrentFile(file)
} }
// Adds a torrent to the list, starts downloading/seeding // Adds a torrent to the list, starts downloading/seeding. TorrentID can be a
function addTorrent (torrentOrMagnetURI) { // magnet URI, infohash, or torrent file: https://github.com/feross/webtorrent#clientaddtorrentid-opts-function-ontorrent-torrent-
if (!torrentOrMagnetURI) torrentOrMagnetURI = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4' function addTorrent (torrentId) {
var torrent = startTorrenting(torrentOrMagnetURI) if (!torrentId) torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4'
var torrent = startTorrenting(torrentId)
state.saved.torrents.push({ state.saved.torrents.push({
name: torrent.name, name: torrent.name,
magnetURI: torrent.magnetURI, magnetURI: torrent.magnetURI,
path: torrent.path infoHash: torrent.infoHash,
path: torrent.path,
xt: torrent.xt,
dn: torrent.dn,
announce: torrent.announce
}) })
saveState()
} }
// Starts downloading and/or seeding a given torrent file or magnet URI // Starts downloading and/or seeding a given torrent file or magnet URI
function startTorrenting (torrentOrMagnetURI) { function startTorrenting (torrentId) {
var torrent = state.client.add(torrentOrMagnetURI) var torrent = state.client.add(torrentId)
addTorrentEvents(torrent) addTorrentEvents(torrent)
return torrent return torrent
} }
@@ -344,6 +350,8 @@ function openPlayer (torrent) {
} }
function deleteTorrent (torrent) { function deleteTorrent (torrent) {
var ix = state.saved.torrents.findIndex((x) => x.infoHash === torrent.infoHash)
if (ix > -1) state.saved.torrents.splice(ix, 1)
torrent.destroy(saveState) torrent.destroy(saveState)
} }