Skip to content

Commit

Permalink
fix: handle the close of the renderer correctly (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuruk-mm authored Apr 14, 2022
1 parent 4a9b9e4 commit 83a045e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
3 changes: 2 additions & 1 deletion electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const defaultConfig: LauncherConfig = {
class MainApp {
defaultConfig: LauncherConfig = defaultConfig

isDefaultWeb = false
isRendererOpen = false
isExitAllowed = false
tray: Tray | null = null
Expand Down Expand Up @@ -120,7 +121,7 @@ const startApp = async (): Promise<void> => {

if (reloadWebsite) {
// (#1457) we should reload the url
loadDecentralandWeb(win)
await loadDecentralandWeb(win)
}

showWindowAndHideTray(win)
Expand Down
37 changes: 21 additions & 16 deletions electron/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,28 @@ const getPlayerLog = (): string => {
}

const reportFatalError = async (sender: WebContents, message: string) => {
await sender.executeJavaScript(
`
ReportFatalError(new Error('${message}'), 'renderer#errorHandler')
`
)
try {
const code = `ReportFatalError(new Error(${JSON.stringify(message)}), 'renderer#errorHandler')`
console.log(code)
await sender.executeJavaScript(code)
} catch (e) {
console.error(`Report fatal error, error: ${e}`)
}
}

const reportCrash = async (sender: WebContents, message: string) => {
const reportCrash = async (sender: WebContents) => {

const path = JSON.stringify({ playerlogpath: getPlayerLogPath() })
const data = JSON.stringify(`Player log:\n${getPlayerLog()}`)
const code = `window.Rollbar.error(${data}, ${path})`
console.log(`reportCrash path: ${path}`)

try {
const path = JSON.stringify({ playerlogpath: getPlayerLogPath() })
const data = JSON.stringify(`Player log:\n${getPlayerLog()}`)
await sender.executeJavaScript(code)

console.log(`reportCrash path: ${path}`)
await sender.executeJavaScript(`window.Rollbar.error(${data}, ${path})`)
await reportFatalError(sender, message)
await reportFatalError(sender, 'Renderer unexpected exit')
} catch (e) {
console.error(`Report crash error: ${e}`)
console.error(`Report crash, error: ${e}`)
}
}

Expand All @@ -120,13 +125,13 @@ const registerExecuteProcessEvent = (rendererPath: string, executablePath: strin
const onProcessFinish = async (err: any, data: any) => {
if (err) {
console.error('Execute error: ', err)
await reportCrash(event.sender, err.message)
ipcMain.emit('process-terminated', false)
await reportCrash(event.sender)
ipcMain.emit('process-terminated', event, false)
return
}

console.log('Process terminated - ' + data.toString())
ipcMain.emit('process-terminated', true)
ipcMain.emit('process-terminated', event, true)
}

let path = '"' + rendererPath + getBranchName() + executablePath + '"'
Expand All @@ -143,7 +148,7 @@ const registerExecuteProcessEvent = (rendererPath: string, executablePath: strin
exec(path + extraParams, onProcessFinish)
}

ipcMain.emit('on-open-renderer')
ipcMain.emit('on-open-renderer', event)
} catch (e) {
console.error('Execute error: ', e)
await reportFatalError(event.sender, JSON.stringify(e))
Expand Down
31 changes: 21 additions & 10 deletions electron/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ export const createWindow = async (title: string, launcherPaths: LauncherPaths):

win.setMenuBarVisibility(false)

await loadWindow(win, isDev)
await loadDefaultWeb(win, isDev)

checkDeveloperConsole(win)

return Promise.resolve(win)
}

export const loadWindow = async (win: BrowserWindow, isDev: boolean) => {
export const loadDefaultWeb = async (win: BrowserWindow, isDev: boolean) => {
main.isDefaultWeb = true
if (isDev) {
await win.loadURL(`http://localhost:9000/index.html`)
} else {
Expand Down Expand Up @@ -98,17 +99,26 @@ export const showWindowAndHideTray = (win: BrowserWindow) => {
}
}

const showLoading = (win: BrowserWindow) => {
win.webContents.executeJavaScript(`document.getElementById("loading").removeAttribute("hidden")`)
const showLoading = async (win: BrowserWindow) => {
try {
await win.webContents.executeJavaScript(`document.getElementById("loading").removeAttribute("hidden")`)
} catch (e) {
console.error(`Show loading error ${e}`)
}
}

const hideLoading = (win: BrowserWindow) => {
win.webContents.executeJavaScript(`document.getElementById("loading")?.setAttribute("hidden", "")`)
const hideLoading = async (win: BrowserWindow) => {
try {
await win.webContents.executeJavaScript(`document.getElementById("loading")?.setAttribute("hidden", "")`)
} catch (e) {
console.error(`Hide loading error ${e}`)
}
}

export const loadDecentralandWeb = async (win: BrowserWindow) => {
try {
showLoading(win)
if (main.isDefaultWeb)
await showLoading(win)

const stage = main.config.developerMode ? 'zone' : 'org'
const url = new URL(main.config.customUrl || `http://play.decentraland.${stage}/?renderer-version=loading`)
Expand All @@ -125,8 +135,9 @@ export const loadDecentralandWeb = async (win: BrowserWindow) => {

console.log(`Opening: ${url.toString()}`)

let promise = win.loadURL(url.toString())
promise.finally(() => hideLoading(win))
main.isDefaultWeb = false
await win.loadURL(url.toString())
await hideLoading(win)
} catch (err) {
console.error('err:', err)
}
Expand Down Expand Up @@ -185,7 +196,7 @@ export const onOpenUrl = (data: string, win?: BrowserWindow) => {
if (!isDev && !main.config.developerMode && !main.config.previewMode) {
loadDecentralandWeb(win)
} else {
loadWindow(win, isDev)
loadDefaultWeb(win, isDev)
}

checkDeveloperConsole(win)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
},
"scripts": {
"postinstall": "electron-builder install-app-deps",
"start": "concurrently \"cross-env http-server public -p 9000\" \"wait-on http://localhost:9000 && tsc -p electron -w\" \"wait-on http://localhost:9000 && electron .\"",
"start:local": "concurrently \"cross-env http-server public -p 9000\" \"wait-on http://localhost:9000 && tsc -p electron -w\" \"wait-on http://localhost:9000 && electron . --custom-url http://localhost:3000/?\"",
"start": "concurrently \"cross-env http-server public -p 9000\" \"wait-on http://localhost:9000 && tsc -p electron && electron .\"",
"start:local": "concurrently \"cross-env http-server public -p 9000\" \"wait-on http://localhost:9000 && tsc -p electron && electron . --custom-url http://localhost:3000/?\"",
"build": "tsc -p electron && electron-builder --publish never",
"publish": "tsc -p electron && electron-builder --publish always"
},
Expand Down Expand Up @@ -115,4 +115,4 @@
"last 1 safari version"
]
}
}
}
18 changes: 10 additions & 8 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@
<p>You're entering a local Decentraland preview hosted at:</p>
<p style="color: #f05; text-decoration: underline;" id="preview-mode-url">empty</p>
<p><small>Only follow links to trusted domains</small></p>

<p><input onclick="submitForm()" type="button" style="background-color: #ff2d55; border-radius: 6px; border: 0; height: 44px; color: #fff; font-weight: bold; font-size: 13px; width: 128px; font-family: -apple-system,system-ui,segoe ui,Roboto,helvetica neue,Arial,sans-serif;" value="CONTINUE" /></p>


<p><input onclick="submitForm()" type="button"
style="background-color: #ff2d55; border-radius: 6px; border: 0; height: 44px; color: #fff; font-weight: bold; font-size: 13px; width: 128px; font-family: -apple-system,system-ui,segoe ui,Roboto,helvetica neue,Arial,sans-serif;"
value="CONTINUE" /></p>

<label>Add custom URL parameters:</label>
<input onchange="checkChange(`preview-custom-param`)" type="checkbox" id="preview-check-custom-param" /></br>
<input oninput="updateUrl()" type="text" id="preview-custom-param" value="EXAMPLE=VALUE&" disabled='' /></br></br>
Expand Down Expand Up @@ -141,12 +143,12 @@

if (!hasCustomUrl) {
const newUrl = new URL(
hasExplorerWebsiteBranch ? `https://explorer-artifacts.decentraland.org/@dcl/explorer-website/branch/${explorerWebsiteBranch}/?`
: baseUrl
hasExplorerWebsiteBranch ? `https://explorer-artifacts.decentraland.org/@dcl/explorer-website/branch/${explorerWebsiteBranch}/?`
: baseUrl
)

if (hasKernelBranch) {
newUrl.searchParams.append('kernel-branch', )
newUrl.searchParams.append('kernel-branch', '')
}

if (hasCustomParam) {
Expand All @@ -160,7 +162,7 @@
return newUrl.toString()
} else if (!previewPanelHidden) {
const newUrl = new URL(baseUrl)

if (hasPreviewCustomParam) {
const customParamObj = new URLSearchParams(previewCustomParam)
for (const [paramKey, paramValue] of customParamObj) {
Expand Down

0 comments on commit 83a045e

Please sign in to comment.