From a8a861260e7aa0e21be897507ab3c0ddf9ee1cf8 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 22 Sep 2016 12:00:40 +0200 Subject: [PATCH] main: Start loading state before app is ready (#952) As mentioned in https://github.com/feross/webtorrent-desktop/pull/827#discussion_r799219 59 We should load the state outside the app.on('ready') handler so there's a chance it's ready by the time 'ready' fires. This improves startup time by roughly 50ms on my Macbook 12". --- src/main/index.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index c82d8846..cadbb766 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -4,6 +4,8 @@ const electron = require('electron') const app = electron.app const ipcMain = electron.ipcMain +const parallel = require('run-parallel') + const announcement = require('./announcement') const config = require('../config') const crashReporter = require('../crash-reporter') @@ -60,23 +62,17 @@ function init () { app.ipcReady = false // main window has finished loading and IPC is ready app.isQuitting = false - // Open handlers must be added as early as possible - app.on('open-file', onOpen) - app.on('open-url', onOpen) + parallel({ + appReady: (cb) => app.on('ready', () => cb(null)), + state: (cb) => State.load(cb) + }, onReady) - ipc.init() + function onReady (err, results) { + if (err) throw err - app.once('will-finish-launching', function () { - crashReporter.init() - }) - - app.on('ready', function () { isReady = true - State.load(function (err, state) { - if (err) throw err - windows.main.init(state, {hidden: hidden}) - }) + windows.main.init(results.state, {hidden: hidden}) windows.webtorrent.init() menu.init() @@ -89,6 +85,15 @@ function init () { const error = {message: err.message, stack: err.stack} windows.main.dispatch('uncaughtError', 'main', error) }) + } + + app.on('open-file', onOpen) + app.on('open-url', onOpen) + + ipc.init() + + app.once('will-finish-launching', function () { + crashReporter.init() }) app.once('ipcReady', function () {