Send ipcReady after state is initialized

Moving it sooner caused more than one bug, including
https://github.com/feross/webtorrent-desktop/issues/606 most recently.

The app feels no slower to start (even though the command line shows a
250ms slower start).

Closes #606.
This commit is contained in:
Feross Aboukhadijeh
2016-06-01 00:27:26 -07:00
parent cc9ba385bf
commit f7acdffb2a

View File

@@ -3,18 +3,9 @@ console.time('init')
var crashReporter = require('../crash-reporter') var crashReporter = require('../crash-reporter')
crashReporter.init() crashReporter.init()
var electron = require('electron')
// 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,
// and this IPC channel receives from and sends messages to the main process
var ipcRenderer = electron.ipcRenderer
// Listen for messages from the main process
setupIpc()
var appConfig = require('application-config')('WebTorrent') var appConfig = require('application-config')('WebTorrent')
var dragDrop = require('drag-drop') var dragDrop = require('drag-drop')
var electron = require('electron')
var fs = require('fs-extra') var fs = require('fs-extra')
var mainLoop = require('main-loop') var mainLoop = require('main-loop')
var parallel = require('run-parallel') var parallel = require('run-parallel')
@@ -46,6 +37,11 @@ var vdomLoop
var state = State.getInitialState() var state = State.getInitialState()
state.location.go({ url: 'home' }) // Add first page to location history state.location.go({ url: 'home' }) // Add first page to location history
// 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,
// and this IPC channel receives from and sends messages to the main process
var ipcRenderer = electron.ipcRenderer
// 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.
loadState(init) loadState(init)
@@ -90,6 +86,9 @@ function init () {
}) })
document.body.appendChild(vdomLoop.target) document.body.appendChild(vdomLoop.target)
// Listen for messages from the main process
setupIpc()
// Calling update() updates the UI given the current state // Calling update() updates the UI given the current state
// Do this at least once a second to give every file in every torrentSummary // Do this at least once a second to give every file in every torrentSummary
// a progress bar and to keep the cursor in sync when playing a video // a progress bar and to keep the cursor in sync when playing a video
@@ -106,9 +105,9 @@ function init () {
window.addEventListener('focus', onFocus) window.addEventListener('focus', onFocus)
window.addEventListener('blur', onBlur) window.addEventListener('blur', onBlur)
// Done! Ideally we want to get here <100ms after the user clicks the app
sound.play('STARTUP') sound.play('STARTUP')
// Done! Ideally we want to get here < 500ms after the user clicks the app
console.timeEnd('init') console.timeEnd('init')
} }
@@ -139,7 +138,7 @@ function render (state) {
// Calls render() to go from state -> UI, then applies to vdom to the real DOM. // Calls render() to go from state -> UI, then applies to vdom to the real DOM.
function update () { function update () {
showOrHidePlayerControls() showOrHidePlayerControls()
if (vdomLoop) vdomLoop.update(state) vdomLoop.update(state)
updateElectron() updateElectron()
} }
@@ -458,8 +457,6 @@ function isCasting () {
} }
function setupIpc () { function setupIpc () {
ipcRenderer.send('ipcReady')
ipcRenderer.on('log', (e, ...args) => console.log(...args)) ipcRenderer.on('log', (e, ...args) => console.log(...args))
ipcRenderer.on('error', (e, ...args) => console.error(...args)) ipcRenderer.on('error', (e, ...args) => console.error(...args))
@@ -486,13 +483,15 @@ function setupIpc () {
ipcRenderer.on('wt-poster', (e, ...args) => torrentPosterSaved(...args)) ipcRenderer.on('wt-poster', (e, ...args) => torrentPosterSaved(...args))
ipcRenderer.on('wt-audio-metadata', (e, ...args) => torrentAudioMetadata(...args)) ipcRenderer.on('wt-audio-metadata', (e, ...args) => torrentAudioMetadata(...args))
ipcRenderer.on('wt-server-running', (e, ...args) => torrentServerRunning(...args)) ipcRenderer.on('wt-server-running', (e, ...args) => torrentServerRunning(...args))
ipcRenderer.send('ipcReady')
} }
// Starts all torrents that aren't paused on program startup // Starts all torrents that aren't paused on program startup
function resumeTorrents () { function resumeTorrents () {
state.saved.torrents state.saved.torrents
.filter((x) => x.status !== 'paused') .filter((torrentSummary) => torrentSummary.status !== 'paused')
.forEach((x) => startTorrentingSummary(x)) .forEach((torrentSummary) => startTorrentingSummary(torrentSummary))
} }
// Updates a single property in the UNSAVED prefs // Updates a single property in the UNSAVED prefs