Skip to content

Commit

Permalink
Improve startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Jan 5, 2022
1 parent f8771c4 commit efaa065
Showing 1 changed file with 141 additions and 147 deletions.
288 changes: 141 additions & 147 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const logger = require("@levminer/lib/logger/main")
const { autoUpdater } = require("electron-updater")
const { number, date } = require("./build.json")
const remote = require("@electron/remote/main")
const debug = require("electron-debug")
const axios = require("axios").default
const path = require("path")
const fs = require("fs")
Expand Down Expand Up @@ -61,18 +60,13 @@ let menu = null
let dev = false

if (app.isPackaged === false) {
const debug = require("electron-debug")

debug({
showDevTools: false,
})

dev = true
} else {
if (process.platform === "darwin") {
debug({
isEnabled: true,
showDevTools: false,
})
}
}

/**
Expand Down Expand Up @@ -906,6 +900,145 @@ const createWindows = () => {
}
}

/**
* Start Authme when the app is ready
*/
app.whenReady()
.then(() => {
logger.log("Starting app")

process.on("uncaughtException", (error) => {
logger.error("Error occurred while starting", error.stack)

dialog
.showMessageBox({
title: "Authme",
buttons: ["Report", "Close", "Exit"],
defaultId: 0,
cancelId: 1,
noLink: true,
type: "error",
message: `Error occurred while starting Authme! \n\n${error.stack}`,
})
.then((result) => {
if (result.response === 0) {
shell.openExternal("https://github.com/Levminer/authme/issues/")
} else if (result.response === 2) {
app.exit()
}
})
})

// Set application Id
if (dev === false) {
app.setAppUserModelId("Authme")
}

// Create windows
createWindows()

/**
* Create tray and menu
*/
const icon_path = path.join(__dirname, "img/tray.png")
tray = new Tray(icon_path)

tray.on("click", () => {
showAppFromTray()
createTray()
createMenu()
})

createTray()
createMenu()
quickShortcuts()

/**
* App controller
*/
if (settings.security.require_password === null) {
window_landing.on("ready-to-show", () => {
if (authenticated === false) {
if (landing_shown === false) {
setTimeout(() => {
window_landing.maximize()
window_landing.show()
}, 100)

landing_shown = true
}

logger.warn("First start")

if (dev === false) {
authme_launcher.enable()
}
}
})
}

if (settings.security.require_password === true) {
window_confirm.on("ready-to-show", () => {
if (authenticated === false) {
settings = JSON.parse(fs.readFileSync(path.join(folder_path, "settings", "settings.json"), "utf-8"))

setTimeout(() => {
window_confirm.maximize()
window_confirm.show()

setTimeout(() => {
window_landing.destroy()
}, 100)
}, 100)
}
})
}

if (settings.security.require_password === false) {
window_application.on("ready-to-show", () => {
if (authenticated === false) {
settings = JSON.parse(fs.readFileSync(path.join(folder_path, "settings", "settings.json"), "utf-8"))

setTimeout(() => {
window_application.maximize()
window_application.show()

setTimeout(() => {
window_confirm.destroy()
window_landing.destroy()
}, 100)
}, 100)

authenticated = true

createTray()
createMenu()
}
})
}
})
.catch((error) => {
logger.error("Error occurred while starting", error.stack)

dialog
.showMessageBox({
title: "Authme",
buttons: ["Report", "Close", "Exit"],
defaultId: 0,
cancelId: 1,
noLink: true,
type: "error",
message: `Error occurred while starting Authme! \n\n${error.stack}`,
})
.then((result) => {
if (result.response === 0) {
shell.openExternal("https://github.com/Levminer/authme/issues/")
} else if (result.response === 2) {
app.exit()
}
})
})

/**
* Auto launch Authme on system start
*/
Expand Down Expand Up @@ -1510,145 +1643,6 @@ power.on("lock-screen", () => {
}
})

/**
* Start Authme when the app is ready
*/
app.whenReady()
.then(() => {
logger.log("Starting app")

process.on("uncaughtException", (error) => {
logger.error("Error occurred while starting", error.stack)

dialog
.showMessageBox({
title: "Authme",
buttons: ["Report", "Close", "Exit"],
defaultId: 0,
cancelId: 1,
noLink: true,
type: "error",
message: `Error occurred while starting Authme! \n\n${error.stack}`,
})
.then((result) => {
if (result.response === 0) {
shell.openExternal("https://github.com/Levminer/authme/issues/")
} else if (result.response === 2) {
app.exit()
}
})
})

// Set application Id
if (dev === false) {
app.setAppUserModelId("Authme")
}

// Create windows
createWindows()

/**
* Create tray and menu
*/
const icon_path = path.join(__dirname, "img/tray.png")
tray = new Tray(icon_path)

tray.on("click", () => {
showAppFromTray()
createTray()
createMenu()
})

createTray()
createMenu()
quickShortcuts()

/**
* App controller
*/
if (settings.security.require_password === null) {
window_landing.on("ready-to-show", () => {
if (authenticated === false) {
if (landing_shown === false) {
setTimeout(() => {
window_landing.maximize()
window_landing.show()
}, 100)

landing_shown = true
}

logger.warn("First start")

if (dev === false) {
authme_launcher.enable()
}
}
})
}

if (settings.security.require_password === true) {
window_confirm.on("ready-to-show", () => {
if (authenticated === false) {
settings = JSON.parse(fs.readFileSync(path.join(folder_path, "settings", "settings.json"), "utf-8"))

setTimeout(() => {
window_confirm.maximize()
window_confirm.show()

setTimeout(() => {
window_landing.destroy()
}, 100)
}, 100)
}
})
}

if (settings.security.require_password === false) {
window_application.on("ready-to-show", () => {
if (authenticated === false) {
settings = JSON.parse(fs.readFileSync(path.join(folder_path, "settings", "settings.json"), "utf-8"))

setTimeout(() => {
window_application.maximize()
window_application.show()

setTimeout(() => {
window_confirm.destroy()
window_landing.destroy()
}, 100)
}, 100)

authenticated = true

createTray()
createMenu()
}
})
}
})
.catch((error) => {
logger.error("Error occurred while starting", error.stack)

dialog
.showMessageBox({
title: "Authme",
buttons: ["Report", "Close", "Exit"],
defaultId: 0,
cancelId: 1,
noLink: true,
type: "error",
message: `Error occurred while starting Authme! \n\n${error.stack}`,
})
.then((result) => {
if (result.response === 0) {
shell.openExternal("https://github.com/Levminer/authme/issues/")
} else if (result.response === 2) {
app.exit()
}
})
})

/**
* Create tray menu
*/
Expand Down

0 comments on commit efaa065

Please sign in to comment.