Perf: lazy-load more require() calls in main process

Went from 36 unique require calls, to 31 calls after this commit.
This commit is contained in:
Feross Aboukhadijeh
2016-09-30 18:20:30 -07:00
parent 21de048738
commit d5820063a1
4 changed files with 40 additions and 18 deletions

View File

@@ -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.

View File

@@ -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) {

View File

@@ -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 () {

View File

@@ -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)
}
}