Refactoring main.js: simplify startup
This commit is contained in:
@@ -22,33 +22,28 @@ var sound = require('./lib/sound')
|
|||||||
var State = require('./lib/state')
|
var State = require('./lib/state')
|
||||||
var TorrentPlayer = require('./lib/torrent-player')
|
var TorrentPlayer = require('./lib/torrent-player')
|
||||||
var TorrentSummary = require('./lib/torrent-summary')
|
var TorrentSummary = require('./lib/torrent-summary')
|
||||||
var {setDispatch} = require('./lib/dispatcher')
|
|
||||||
|
// Yo-yo pattern: state object lives here and percolates down thru all the views.
|
||||||
|
// Events come back up from the views via dispatch(...)
|
||||||
|
require('./lib/dispatcher').setDispatch(dispatch)
|
||||||
|
|
||||||
|
// This dependency is the slowest-loading, so we lazy load it
|
||||||
|
var Cast = null
|
||||||
|
|
||||||
// Electron apps have two processes: a main process (node) runs first and starts
|
// Electron apps have two processes: a main process (node) runs first and starts
|
||||||
// a renderer process (essentially a Chrome window). We're in the renderer process,
|
// a renderer process (essentially a Chrome window). We're in the renderer process,
|
||||||
// and this IPC channel receives from and sends messages to the main process
|
// and this IPC channel receives from and sends messages to the main process
|
||||||
var ipcRenderer = electron.ipcRenderer
|
var ipcRenderer = electron.ipcRenderer
|
||||||
|
|
||||||
var state, vdomLoop
|
|
||||||
|
|
||||||
// This dependency is the slowest-loading, so we lazy load it
|
|
||||||
var Cast = null
|
|
||||||
|
|
||||||
init()
|
|
||||||
|
|
||||||
function init () {
|
|
||||||
// All state lives in state.js. `state.saved` is read from and written to a file.
|
// 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.
|
// All other state is ephemeral. First we load state.saved then initialize the app.
|
||||||
|
var state, vdomLoop
|
||||||
|
|
||||||
State.load(onState)
|
State.load(onState)
|
||||||
|
|
||||||
setDispatch(dispatch)
|
// Called once when the application loads. (Not once per window.)
|
||||||
}
|
// Connects to the torrent networks, sets up the UI and OS integrations like
|
||||||
|
// the dock icon and drag+drop.
|
||||||
/**
|
|
||||||
* Called once when the application loads. (Not once per window.)
|
|
||||||
* Connects to the torrent networks, sets up the UI and OS integrations like
|
|
||||||
* the dock icon and drag+drop.
|
|
||||||
*/
|
|
||||||
function onState (err, _state) {
|
function onState (err, _state) {
|
||||||
if (err) return onError(err)
|
if (err) return onError(err)
|
||||||
state = _state
|
state = _state
|
||||||
@@ -104,6 +99,7 @@ function onState (err, _state) {
|
|||||||
console.timeEnd('init')
|
console.timeEnd('init')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Runs a few seconds after the app loads, to avoid slowing down startup time
|
||||||
function delayedInit () {
|
function delayedInit () {
|
||||||
lazyLoadCast()
|
lazyLoadCast()
|
||||||
sound.preload()
|
sound.preload()
|
||||||
@@ -136,6 +132,8 @@ function update () {
|
|||||||
updateElectron()
|
updateElectron()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Some state changes can't be reflected in the DOM, instead we have to
|
||||||
|
// tell the main process to update the window or OS integrations
|
||||||
function updateElectron () {
|
function updateElectron () {
|
||||||
if (state.window.title !== state.prev.title) {
|
if (state.window.title !== state.prev.title) {
|
||||||
state.prev.title = state.window.title
|
state.prev.title = state.window.title
|
||||||
@@ -213,13 +211,7 @@ function dispatch (action, ...args) {
|
|||||||
backToList()
|
backToList()
|
||||||
}
|
}
|
||||||
if (action === 'escapeBack') {
|
if (action === 'escapeBack') {
|
||||||
if (state.modal) {
|
escapeBack()
|
||||||
dispatch('exitModal')
|
|
||||||
} else if (state.window.isFullScreen) {
|
|
||||||
dispatch('toggleFullScreen')
|
|
||||||
} else {
|
|
||||||
dispatch('back')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (action === 'back') {
|
if (action === 'back') {
|
||||||
state.location.back()
|
state.location.back()
|
||||||
@@ -455,6 +447,17 @@ function backToList () {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Quits modals, full screen, or goes back. Happens when the user hits ESC
|
||||||
|
function escapeBack() {
|
||||||
|
if (state.modal) {
|
||||||
|
dispatch('exitModal')
|
||||||
|
} else if (state.window.isFullScreen) {
|
||||||
|
dispatch('toggleFullScreen')
|
||||||
|
} else {
|
||||||
|
dispatch('back')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Checks whether we are connected and already casting
|
// Checks whether we are connected and already casting
|
||||||
// Returns false if we not casting (state.playing.location === 'local')
|
// Returns false if we not casting (state.playing.location === 'local')
|
||||||
// or if we're trying to connect but haven't yet ('chromecast-pending', etc)
|
// or if we're trying to connect but haven't yet ('chromecast-pending', etc)
|
||||||
|
|||||||
Reference in New Issue
Block a user