diff --git a/src/config.js b/src/config.js index 8f4f0e9f..f0be0664 100644 --- a/src/config.js +++ b/src/config.js @@ -1,5 +1,4 @@ const appConfig = require('application-config')('WebTorrent') -const fs = require('fs') const path = require('path') const electron = require('electron') const arch = require('arch') @@ -146,6 +145,8 @@ function isPortable () { return false } + const fs = require('fs') + try { // This line throws if the "Portable Settings" folder does not exist, and does // nothing otherwise. diff --git a/src/main/index.js b/src/main/index.js index 17c30b71..9271ce49 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -124,15 +124,18 @@ function init () { function delayedInit () { const announcement = require('./announcement') const dock = require('./dock') - const tray = require('./tray') const updater = require('./updater') const userTasks = require('./user-tasks') announcement.init() dock.init() - tray.init() updater.init() userTasks.init() + + if (process.platform !== 'darwin') { + const tray = require('./tray') + tray.init() + } } function onOpen (e, torrentId) { diff --git a/src/main/windows/main.js b/src/main/windows/main.js index c0c9dd8e..3c8d26f4 100644 --- a/src/main/windows/main.js +++ b/src/main/windows/main.js @@ -84,11 +84,14 @@ function init (state, options) { }) win.on('close', function (e) { - const tray = require('../tray') - - if (process.platform !== 'darwin' && !tray.hasTray()) { - app.quit() - } else if (!app.isQuitting) { + if (process.platform !== 'darwin') { + const tray = require('../tray') + if (!tray.hasTray()) { + app.quit() + return + } + } + if (!app.isQuitting) { e.preventDefault() hide() } @@ -226,17 +229,21 @@ function toggleFullScreen (flag) { } function onWindowBlur () { - const tray = require('../tray') - menu.setWindowFocus(false) - tray.setWindowFocus(false) + + if (process.platform !== 'darwin') { + const tray = require('../tray') + tray.setWindowFocus(false) + } } function onWindowFocus () { - const tray = require('../tray') - menu.setWindowFocus(true) - tray.setWindowFocus(true) + + if (process.platform !== 'darwin') { + const tray = require('../tray') + tray.setWindowFocus(true) + } } function getIconPath () { diff --git a/src/renderer/lib/state.js b/src/renderer/lib/state.js index dddc4279..11825308 100644 --- a/src/renderer/lib/state.js +++ b/src/renderer/lib/state.js @@ -1,10 +1,8 @@ const appConfig = require('application-config')('WebTorrent') -const debounce = require('debounce') const path = require('path') const {EventEmitter} = require('events') const config = require('../../config') -const migrations = require('./migrations') const SAVE_DEBOUNCE_INTERVAL = 1000 @@ -12,7 +10,14 @@ const State = module.exports = Object.assign(new EventEmitter(), { getDefaultPlayState, load, // state.save() calls are rate-limited. Use state.saveImmediate() to skip limit. - save: debounce(saveImmediate, SAVE_DEBOUNCE_INTERVAL), + save: function () { + // Perf optimization: Lazy-require debounce (and it's dependencies) + const debounce = require('debounce') + // After first State.save() invokation, future calls go straight to the + // debounced function + State.save = debounce(saveImmediate, SAVE_DEBOUNCE_INTERVAL) + State.save() + }, saveImmediate }) @@ -205,7 +210,13 @@ function load (cb) { if (err) return cb(err) const state = getDefaultState() state.saved = saved - migrations.run(state) + + if (process.type === 'renderer') { + // Perf optimization: Save require() calls in the main process + const migrations = require('./migrations') + migrations.run(state) + } + cb(null, state) } }