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:
@@ -1,5 +1,4 @@
|
|||||||
const appConfig = require('application-config')('WebTorrent')
|
const appConfig = require('application-config')('WebTorrent')
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
const arch = require('arch')
|
const arch = require('arch')
|
||||||
@@ -146,6 +145,8 @@ function isPortable () {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// This line throws if the "Portable Settings" folder does not exist, and does
|
// This line throws if the "Portable Settings" folder does not exist, and does
|
||||||
// nothing otherwise.
|
// nothing otherwise.
|
||||||
|
|||||||
@@ -124,15 +124,18 @@ function init () {
|
|||||||
function delayedInit () {
|
function delayedInit () {
|
||||||
const announcement = require('./announcement')
|
const announcement = require('./announcement')
|
||||||
const dock = require('./dock')
|
const dock = require('./dock')
|
||||||
const tray = require('./tray')
|
|
||||||
const updater = require('./updater')
|
const updater = require('./updater')
|
||||||
const userTasks = require('./user-tasks')
|
const userTasks = require('./user-tasks')
|
||||||
|
|
||||||
announcement.init()
|
announcement.init()
|
||||||
dock.init()
|
dock.init()
|
||||||
tray.init()
|
|
||||||
updater.init()
|
updater.init()
|
||||||
userTasks.init()
|
userTasks.init()
|
||||||
|
|
||||||
|
if (process.platform !== 'darwin') {
|
||||||
|
const tray = require('./tray')
|
||||||
|
tray.init()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onOpen (e, torrentId) {
|
function onOpen (e, torrentId) {
|
||||||
|
|||||||
@@ -84,11 +84,14 @@ function init (state, options) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
win.on('close', function (e) {
|
win.on('close', function (e) {
|
||||||
const tray = require('../tray')
|
if (process.platform !== 'darwin') {
|
||||||
|
const tray = require('../tray')
|
||||||
if (process.platform !== 'darwin' && !tray.hasTray()) {
|
if (!tray.hasTray()) {
|
||||||
app.quit()
|
app.quit()
|
||||||
} else if (!app.isQuitting) {
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!app.isQuitting) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
hide()
|
hide()
|
||||||
}
|
}
|
||||||
@@ -226,17 +229,21 @@ function toggleFullScreen (flag) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onWindowBlur () {
|
function onWindowBlur () {
|
||||||
const tray = require('../tray')
|
|
||||||
|
|
||||||
menu.setWindowFocus(false)
|
menu.setWindowFocus(false)
|
||||||
tray.setWindowFocus(false)
|
|
||||||
|
if (process.platform !== 'darwin') {
|
||||||
|
const tray = require('../tray')
|
||||||
|
tray.setWindowFocus(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onWindowFocus () {
|
function onWindowFocus () {
|
||||||
const tray = require('../tray')
|
|
||||||
|
|
||||||
menu.setWindowFocus(true)
|
menu.setWindowFocus(true)
|
||||||
tray.setWindowFocus(true)
|
|
||||||
|
if (process.platform !== 'darwin') {
|
||||||
|
const tray = require('../tray')
|
||||||
|
tray.setWindowFocus(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIconPath () {
|
function getIconPath () {
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
const appConfig = require('application-config')('WebTorrent')
|
const appConfig = require('application-config')('WebTorrent')
|
||||||
const debounce = require('debounce')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const {EventEmitter} = require('events')
|
const {EventEmitter} = require('events')
|
||||||
|
|
||||||
const config = require('../../config')
|
const config = require('../../config')
|
||||||
const migrations = require('./migrations')
|
|
||||||
|
|
||||||
const SAVE_DEBOUNCE_INTERVAL = 1000
|
const SAVE_DEBOUNCE_INTERVAL = 1000
|
||||||
|
|
||||||
@@ -12,7 +10,14 @@ const State = module.exports = Object.assign(new EventEmitter(), {
|
|||||||
getDefaultPlayState,
|
getDefaultPlayState,
|
||||||
load,
|
load,
|
||||||
// state.save() calls are rate-limited. Use state.saveImmediate() to skip limit.
|
// 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
|
saveImmediate
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -205,7 +210,13 @@ function load (cb) {
|
|||||||
if (err) return cb(err)
|
if (err) return cb(err)
|
||||||
const state = getDefaultState()
|
const state = getDefaultState()
|
||||||
state.saved = saved
|
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)
|
cb(null, state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user