Merge pull request #46 from feross/persist

add persistent app config
This commit is contained in:
Feross Aboukhadijeh
2016-03-05 23:32:50 -08:00
3 changed files with 50 additions and 25 deletions

View File

@@ -12,6 +12,7 @@
},
"dependencies": {
"airplay-js": "guerrerocarlos/node-airplay-js",
"application-config": "^0.2.0",
"chromecasts": "^1.8.0",
"create-torrent": "^3.22.1",
"debug": "^2.2.0",

View File

@@ -13,6 +13,7 @@ var state = require('./state')
var throttle = require('throttleit')
var torrentPoster = require('./lib/torrent-poster')
var WebTorrent = require('webtorrent')
var cfg = require('application-config')('WebTorrent')
var createElement = require('virtual-dom/create-element')
var diff = require('virtual-dom/diff')
@@ -35,10 +36,7 @@ function init () {
state.client = new WebTorrent()
state.client.on('warning', onWarning)
state.client.on('error', onError)
state.saved.torrents.forEach(function (torrent) {
state.client.add(torrent.torrentFile)
})
state.client.on('torrent', saveTorrents)
// For easy debugging in Developer Tools
global.state = state
@@ -54,6 +52,8 @@ function init () {
dragDrop('body', onFiles)
restoreSession()
chromecasts.on('update', function (player) {
state.devices.chromecast = player
update()
@@ -251,6 +251,35 @@ function addTorrentEvents (torrent) {
}
}
function restoreSession () {
cfg.read(function (err, data) {
if (err) console.error(err)
state.saved = data
if (!state.saved.torrents) state.saved.torrents = []
state.saved.torrents.forEach(function (torrent) {
dispatch('addTorrent', torrent.magnetURI)
})
update()
})
}
function saveTorrents () {
state.saved.torrents = state.client.torrents.map(function (torrent) {
return {
name: torrent.name,
magnetURI: torrent.magnetURI,
path: torrent.path
}
})
cfg.write({
torrents: state.saved.torrents
}, function (err) {
if (err) console.error(err)
update()
})
}
function startServer (torrent, cb) {
if (state.server) return cb()
@@ -286,7 +315,9 @@ function openPlayer (torrent) {
}
function deleteTorrent (torrent) {
torrent.destroy(update)
torrent.destroy(function () {
saveTorrents() // updates after writing to config
})
}
function openChromecast (torrent) {

View File

@@ -27,27 +27,20 @@ module.exports = {
mouseStationarySince: 0 /* Unix time in ms */
},
/* Saved state is read from and written to ~/.webtorrent/state.json
* It should be simple and minimal and must be JSONifiable
/* Saved state is read from and written to application config.
* It should be simple and minimal and must be JSON stringifiable.
*
* Config path:
*
* OS XDG ~/Library/Application Support/WebTorrent/config.json
* Linux (XDG) $XDG_CONFIG_HOME/WebTorrent/config.json
* Linux (Legacy) ~/.config/WebTorrent/config.json
* Windows (> Vista) %LOCALAPPDATA%/WebTorrent/config.json
* Windows (XP, 2000) %USERPROFILE%/Local Settings/Application Data/WebTorrent/config.json
*
* Also accessible via `require('application-config')('WebTorrent').filePath`
*/
saved: {
torrents: [
{
name: 'Sintel',
torrentFile: 'resources/sintel.torrent'
}
// {
// name: 'Elephants Dream',
// torrentFile: 'resources/ElephantsDream_archive.torrent'
// },
// {
// name: 'Big Buck Bunny',
// torrentFile: 'resources/BigBuckBunny_archive.torrent'
// },
// {
// name: 'Tears of Steel',
// torrentFile: 'resources/TearsOfSteel_archive.torrent'
// }
]
torrents: []
}
}