Skip to content

Commit

Permalink
fix: memory and add some try/catch (#11)
Browse files Browse the repository at this point in the history
* fix: memory fixs and add some try catchs

* error message
  • Loading branch information
kuruk-mm authored Oct 28, 2021
1 parent ad77a53 commit 9116353
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
4 changes: 2 additions & 2 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ if (getOSName() === 'windows') {
executablePath = executablePath.replace(/\//gi, '\\')
}

registerUpdaterEvents(baseUrl, rendererPath, versionPath, executablePath, artifactUrl, remoteVersionUrl, config)

const createWindow = async (): Promise<BrowserWindow> => {
const win = new BrowserWindow({
title: 'Decentraland',
Expand All @@ -68,6 +66,8 @@ const createWindow = async (): Promise<BrowserWindow> => {
}
})

registerUpdaterEvents(win, baseUrl, rendererPath, versionPath, executablePath, artifactUrl, remoteVersionUrl, config)

win.setMenuBarVisibility(false)

if (isDev) {
Expand Down
76 changes: 46 additions & 30 deletions electron/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const registerVersionEvent = (rendererPath: string, versionPath: string, baseUrl

if (!validVersion) {
// error
event.sender.send('downloadState', { type: 'ERROR' })
event.sender.send('downloadState', { type: 'ERROR', message: 'Invalid remote version' })
} else if (version === remoteVersion) {
// ready
event.sender.send('downloadState', { type: 'READY' })
Expand All @@ -55,36 +55,47 @@ const getBranchName = () => {

const registerExecuteProcessEvent = (rendererPath: string, executablePath: string, config: any) => {
ipcMain.once('executeProcess', (event) => {
const onExecute = (err: any, data: any) => {
if (err) {
console.error(err)
return
try {
const onExecute = (err: any, data: any) => {
if (err) {
console.error('Execute error: ', err)
event.sender.send('downloadState', { type: 'ERROR', message: err })
return
}

console.log(data.toString())
}

console.log(data.toString())
}

let path = rendererPath + getBranchName() + executablePath
let path = rendererPath + getBranchName() + executablePath

let extraParams = ' --browser false'
let extraParams = ' --browser false'

console.log('Execute path: ', path + extraParams)
console.log('Execute path: ', path + extraParams)

if (getOSName() === 'mac') {
const { exec } = require('child_process')
exec('open "' + path + '" --args' + extraParams, onExecute)
} else {
const { exec } = require('child_process')
exec(path + extraParams, onExecute)
if (getOSName() === 'mac') {
const { exec } = require('child_process')
exec('open "' + path + '" --args' + extraParams, onExecute)
} else {
const { exec } = require('child_process')
exec(path + extraParams, onExecute)
}
} catch (e) {
console.error('Execute error: ', e)
event.sender.send('downloadState', { type: 'ERROR', message: e })
}
})
}
const registerDownloadEvent = (rendererPath: string, versionPath: string, baseUrl: string, artifactUrl: string) => {
const registerDownloadEvent = (
win: BrowserWindow,
rendererPath: string,
versionPath: string,
baseUrl: string,
artifactUrl: string
) => {
//electronDl();
ipcMain.on('download', async (event) => {
const branchPath = rendererPath + getBranchName()
fs.rmdirSync(branchPath, { recursive: true })
const win = BrowserWindow.getFocusedWindow() as BrowserWindow
const url = baseUrl + globalConfig.desktopBranch + artifactUrl
console.log('artifactUrl: ', url)
const res = await electronDl.download(win, url, {
Expand Down Expand Up @@ -119,6 +130,7 @@ const registerDownloadEvent = (rendererPath: string, versionPath: string, baseUr
}

export const registerUpdaterEvents = (
win: BrowserWindow,
baseUrl: string,
rendererPath: string,
versionPath: string,
Expand All @@ -127,21 +139,25 @@ export const registerUpdaterEvents = (
remoteVersionUrl: string,
config: any
) => {
globalConfig.desktopBranch = config.desktopBranch
try {
globalConfig.desktopBranch = config.desktopBranch

// Get version
registerVersionEvent(rendererPath, versionPath, baseUrl, remoteVersionUrl)
// Get version
registerVersionEvent(rendererPath, versionPath, baseUrl, remoteVersionUrl)

// Register event to execute process
registerExecuteProcessEvent(rendererPath, executablePath + getOSExtension(), config)
// Register event to execute process
registerExecuteProcessEvent(rendererPath, executablePath + getOSExtension(), config)

// Register event to download
registerDownloadEvent(rendererPath, versionPath, baseUrl, artifactUrl)
// Register event to download
registerDownloadEvent(win, rendererPath, versionPath, baseUrl, artifactUrl)

// Register clear cache
ipcMain.on('clearCache', async (event) => {
fs.rmdirSync(rendererPath, { recursive: true })
})
// Register clear cache
ipcMain.on('clearCache', async (event) => {
fs.rmdirSync(rendererPath, { recursive: true })
})
} catch (e) {
console.error('registerUpdaterEvents error: ', e)
}
}

export const getOSName = (): string | null => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dcl/explorer-desktop-launcher",
"version": "0.1.6",
"version": "0.1.7",
"author": "decentraland",
"description": "Decentraland Desktop Launcher",
"homepage": ".",
Expand Down

0 comments on commit 9116353

Please sign in to comment.