load state first

This commit is contained in:
Nate Goldman
2016-03-06 23:06:30 -08:00
parent e2f978024d
commit 4e07ecf05c

View File

@@ -9,7 +9,6 @@ var EventEmitter = require('events')
var mainLoop = require('main-loop') var mainLoop = require('main-loop')
var networkAddress = require('network-address') var networkAddress = require('network-address')
var path = require('path') var path = require('path')
var state = require('./state')
var torrentPoster = require('./lib/torrent-poster') var torrentPoster = require('./lib/torrent-poster')
var WebTorrent = require('webtorrent') var WebTorrent = require('webtorrent')
var cfg = require('application-config')('WebTorrent') var cfg = require('application-config')('WebTorrent')
@@ -20,6 +19,9 @@ var patch = require('virtual-dom/patch')
var App = require('./views/app') var App = require('./views/app')
// For easy debugging in Developer Tools
var state = global.state = require('./state')
// Force use of webtorrent trackers on all torrents // Force use of webtorrent trackers on all torrents
global.WEBTORRENT_ANNOUNCE = createTorrent.announceList global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
.map(function (arr) { .map(function (arr) {
@@ -31,6 +33,10 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
var vdomLoop var vdomLoop
// All state lives in state.js. `state.saved` is read from and written to a file.
// All other state is ephemeral. First we load state.saved then initialize the app.
loadState(init)
/** /**
* Called once when the application loads. (Not once per window.) * Called once when the application loads. (Not once per window.)
* Connects to the torrent networks, sets up the UI and OS integrations like * Connects to the torrent networks, sets up the UI and OS integrations like
@@ -59,13 +65,12 @@ function init () {
// (eg % downloaded) and to keep the cursor in sync when playing a video // (eg % downloaded) and to keep the cursor in sync when playing a video
setInterval(update, 1000) setInterval(update, 1000)
// All state lives in state.js. `state.saved` is read from and written to a // Resume all saved torrents now that state is loaded and vdom is ready
// file. All other state is ephemeral. Here we'll load state.saved: resumeAllTorrents()
loadState()
document.addEventListener('unload', saveState) document.addEventListener('unload', saveState)
// For easy debugging in Developer Tools // listen for messages from the main process
global.state = state setupIpc()
// OS integrations: // OS integrations:
// ...Chromecast and Airplay // ...Chromecast and Airplay
@@ -108,7 +113,6 @@ function init () {
state.isFocused = false state.isFocused = false
}) })
} }
init()
// This is the (mostly) pure funtion from state -> UI. Returns a virtual DOM // This is the (mostly) pure funtion from state -> UI. Returns a virtual DOM
// tree. Any events, such as button clicks, will turn into calls to dispatch() // tree. Any events, such as button clicks, will turn into calls to dispatch()
@@ -178,37 +182,43 @@ function dispatch (action, ...args) {
} }
} }
electron.ipcRenderer.on('addTorrent', function (e, torrentId) { function setupIpc () {
dispatch('addTorrent', torrentId) electron.ipcRenderer.on('addTorrent', function (e, torrentId) {
}) dispatch('addTorrent', torrentId)
})
electron.ipcRenderer.on('seed', function (e, files) { electron.ipcRenderer.on('seed', function (e, files) {
dispatch('seed', files) dispatch('seed', files)
}) })
electron.ipcRenderer.on('fullscreenChanged', function (e, isFullScreen) { electron.ipcRenderer.on('fullscreenChanged', function (e, isFullScreen) {
state.isFullScreen = isFullScreen state.isFullScreen = isFullScreen
update() update()
}) })
electron.ipcRenderer.on('addFakeDevice', function (e, device) { electron.ipcRenderer.on('addFakeDevice', function (e, device) {
var player = new EventEmitter() var player = new EventEmitter()
player.play = (networkURL) => console.log(networkURL) player.play = (networkURL) => console.log(networkURL)
state.devices[device] = player state.devices[device] = player
update() update()
}) })
}
// Load state.saved from the JSON state file // Load state.saved from the JSON state file
function loadState () { function loadState (callback) {
cfg.read(function (err, data) { cfg.read(function (err, data) {
if (err) console.error(err) if (err) console.error(err)
electron.ipcRenderer.send('log', 'loaded state from ' + cfg.filePath) electron.ipcRenderer.send('log', 'loaded state from ' + cfg.filePath)
state.saved = data state.saved = data
if (!state.saved.torrents) state.saved.torrents = [] if (!state.saved.torrents) state.saved.torrents = []
state.saved.torrents.forEach((x) => startTorrenting(x.infoHash)) if (callback) callback()
}) })
} }
function resumeAllTorrents () {
state.saved.torrents.forEach((x) => startTorrenting(x.infoHash))
}
// Write state.saved to the JSON state file // Write state.saved to the JSON state file
function saveState () { function saveState () {
electron.ipcRenderer.send('log', 'saving state to ' + cfg.filePath) electron.ipcRenderer.send('log', 'saving state to ' + cfg.filePath)