From d16d417c99faf2410fd53c26ca4df789c4241977 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 29 Jan 2019 17:13:59 -0600 Subject: [PATCH] Refactoring --- electron.js | 36 +++- src/App.js | 186 +++++++----------- src/containers/Configuration/Configuration.js | 86 ++++---- src/containers/IPCContainer/IPCContainer.js | 51 +++++ src/containers/MountItems/MountItems.js | 39 ++-- 5 files changed, 209 insertions(+), 189 deletions(-) create mode 100644 src/containers/IPCContainer/IPCContainer.js diff --git a/electron.js b/electron.js index 6730883..ce482a2 100644 --- a/electron.js +++ b/electron.js @@ -259,7 +259,7 @@ const standardIPCReply = (event, channel, data, error) => { } }; -ipcMain.on(Constants.IPC_Browse_Directory, (event, data) => { +ipcMain.on(Constants.IPC_Browse_Directory + '_sync', (event, data) => { dialog.showOpenDialog(mainWindow, { defaultPath: data.Location, properties: ['openDirectory'], @@ -286,6 +286,23 @@ ipcMain.on(Constants.IPC_Check_Dependency_Installed, (event, data) => { } }); +ipcMain.on(Constants.IPC_Check_Dependency_Installed + '_sync', (event, data) => { + try { + const exists = fs.lstatSync(data.File).isFile(); + event.returnValue = { + data: { + Exists: exists + }, + }; + } catch (e) { + event.returnValue = { + data: { + Exists: false + }, + }; + } +}); + ipcMain.on(Constants.IPC_Check_Installed, (event, data) => { const dataDirectory = helpers.resolvePath(data.Directory); const destination = path.join(dataDirectory, data.Version); @@ -310,7 +327,7 @@ ipcMain.on(Constants.IPC_Check_Installed, (event, data) => { }); }); -ipcMain.on(Constants.IPC_Check_Mount_Location, (event, data) => { +ipcMain.on(Constants.IPC_Check_Mount_Location + '_sync', (event, data) => { let response = { Success: true, Error: '' @@ -342,6 +359,21 @@ ipcMain.on(Constants.IPC_Delete_File, (event, data) => { } }); +ipcMain.on(Constants.IPC_Delete_File + '_sync', (event, data) => { + try { + if (fs.existsSync(data.FilePath)) { + fs.unlinkSync(data.FilePath); + } + event.returnValue = { + data: true, + }; + } catch (e) { + event.returnValue = { + data: false, + }; + } +}); + ipcMain.on(Constants.IPC_Detect_Mounts, (event, data) => { let driveLetters = {}; for (const provider of Constants.PROVIDER_LIST) { diff --git a/src/App.js b/src/App.js index 816c872..bc9983a 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, {Component} from 'react'; +import React from 'react'; import axios from 'axios'; import styles from './App.css'; import Box from './components/UI/Box/Box'; @@ -15,16 +15,12 @@ import ReleaseVersionDisplay from './components/ReleaseVersionDisplay/ReleaseVer import Text from './components/UI/Text/Text'; import UpgradeIcon from './components/UpgradeIcon/UpgradeIcon'; import UpgradeUI from './components/UpgradeUI/UpgradeUI'; +import IPCContainer from './containers/IPCContainer/IPCContainer'; const Constants = require('./constants'); const Scheduler = require('node-schedule'); -let ipcRenderer = null; -if (!process.versions.hasOwnProperty('electron')) { - ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null); -} - -class App extends Component { +class App extends IPCContainer { constructor(props) { super(props); @@ -36,20 +32,18 @@ class App extends Component { } } - if (ipcRenderer) { - ipcRenderer.on(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply); - ipcRenderer.on(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete); - ipcRenderer.on(Constants.IPC_Download_File_Progress, this.onDownloadFileProgress); - ipcRenderer.on(Constants.IPC_Extract_Release_Complete, this.onExtractReleaseComplete); - ipcRenderer.on(Constants.IPC_Get_State_Reply, this.onGetStateReply); - ipcRenderer.on(Constants.IPC_Grab_Releases_Reply, this.onGrabReleasesReply); - ipcRenderer.on(Constants.IPC_Grab_UI_Releases_Reply, this.onGrabUiReleasesReply); - ipcRenderer.on(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply); - ipcRenderer.on(Constants.IPC_Install_Upgrade_Reply, this.onInstallUpgradeReply); + this.setRequestHandler(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply); + this.setRequestHandler(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete); + this.setRequestHandler(Constants.IPC_Download_File_Progress, this.onDownloadFileProgress); + this.setRequestHandler(Constants.IPC_Extract_Release_Complete, this.onExtractReleaseComplete); + this.setRequestHandler(Constants.IPC_Get_State_Reply, this.onGetStateReply); + this.setRequestHandler(Constants.IPC_Grab_Releases_Reply, this.onGrabReleasesReply); + this.setRequestHandler(Constants.IPC_Grab_UI_Releases_Reply, this.onGrabUiReleasesReply); + this.setRequestHandler(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply); + this.setRequestHandler(Constants.IPC_Install_Upgrade_Reply, this.onInstallUpgradeReply); - ipcRenderer.send(Constants.IPC_Get_State, Constants.DATA_LOCATIONS[this.props.platform]); - Scheduler.scheduleJob('23 11 * * *', this.updateCheckScheduledJob); - } + this.sendRequest(Constants.IPC_Get_State, Constants.DATA_LOCATIONS[this.props.platform]); + Scheduler.scheduleJob('23 11 * * *', this.updateCheckScheduledJob); } state = { @@ -106,18 +100,16 @@ class App extends Component { }, ()=> { const selectedVersion = this.getSelectedVersion(); if (selectedVersion !== 'unavailable') { - if (ipcRenderer) { - let dependencies = []; - if (this.state.LocationsLookup[selectedVersion] && this.state.LocationsLookup[selectedVersion].dependencies) { - dependencies = this.state.LocationsLookup[selectedVersion].dependencies; - } - - ipcRenderer.send(Constants.IPC_Check_Installed, { - Dependencies: dependencies, - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Version: selectedVersion, - }); + let dependencies = []; + if (this.state.LocationsLookup[selectedVersion] && this.state.LocationsLookup[selectedVersion].dependencies) { + dependencies = this.state.LocationsLookup[selectedVersion].dependencies; } + + this.sendRequest(Constants.IPC_Check_Installed, { + Dependencies: dependencies, + Directory: Constants.DATA_LOCATIONS[this.props.platform], + Version: selectedVersion, + }); } }); }; @@ -128,9 +120,7 @@ class App extends Component { } if (this.state.ErrorCritical) { - if (ipcRenderer) { - ipcRenderer.send(Constants.IPC_Shutdown); - } + this.sendRequest(Constants.IPC_Shutdown); } else { this.setState({ DisplayError: false, @@ -139,20 +129,6 @@ class App extends Component { } }; - componentWillUnmount = () => { - if (ipcRenderer) { - ipcRenderer.removeListener(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply); - ipcRenderer.removeListener(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete); - ipcRenderer.removeListener(Constants.IPC_Download_File_Progress, this.onDownloadFileProgress); - ipcRenderer.removeListener(Constants.IPC_Extract_Release_Complete, this.onExtractReleaseComplete); - ipcRenderer.removeListener(Constants.IPC_Get_State_Reply, this.onGetStateReply); - ipcRenderer.removeListener(Constants.IPC_Grab_Releases_Reply, this.onGrabReleasesReply); - ipcRenderer.removeListener(Constants.IPC_Grab_UI_Releases_Reply, this.onGrabUiReleasesReply); - ipcRenderer.removeListener(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply); - ipcRenderer.removeListener(Constants.IPC_Install_Upgrade_Reply, this.onInstallUpgradeReply); - } - }; - extractFileNameFromURL = url => { const parts = url.split('/'); return parts[parts.length - 1]; @@ -161,7 +137,7 @@ class App extends Component { extractRelease = (data) => { if (data.Success) { const selectedVersion = this.getSelectedVersion(); - ipcRenderer.send(Constants.IPC_Extract_Release, { + this.sendRequest(Constants.IPC_Extract_Release, { Directory: Constants.DATA_LOCATIONS[this.props.platform], Source: data.Destination, Version: selectedVersion, @@ -183,10 +159,8 @@ class App extends Component { grabReleases = () => { if (this.props.platform !== 'unknown') { - if (ipcRenderer) { - ipcRenderer.send(Constants.IPC_Grab_Releases); - ipcRenderer.send(Constants.IPC_Grab_UI_Releases); - } + this.sendRequest(Constants.IPC_Grab_Releases); + this.sendRequest(Constants.IPC_Grab_UI_Releases); } }; @@ -227,19 +201,17 @@ class App extends Component { }; handleDependencyDownload = (url) => { - if (ipcRenderer) { - this.setState({ - DownloadActive: true, - DownloadingDependency: true, - DownloadName: this.extractFileNameFromURL(url), - }, ()=> { - ipcRenderer.send(Constants.IPC_Download_File, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Filename: this.state.DownloadName, - URL: url, - }); + this.setState({ + DownloadActive: true, + DownloadingDependency: true, + DownloadName: this.extractFileNameFromURL(url), + }, ()=> { + this.sendRequest(Constants.IPC_Download_File, { + Directory: Constants.DATA_LOCATIONS[this.props.platform], + Filename: this.state.DownloadName, + URL: url, }); - } + }); }; handleMountLocationChanged = (storageType, location) => { @@ -269,38 +241,32 @@ class App extends Component { handleReleaseDownload = () => { const selectedVersion = this.getSelectedVersion(); const fileName = selectedVersion + '.zip'; - if (ipcRenderer) { - this.setState({ - DownloadActive: true, - DownloadingRelease: true, - DownloadName: fileName, - }, () => { - ipcRenderer.send(Constants.IPC_Download_File, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Filename: this.state.DownloadName, - URL: this.state.LocationsLookup[selectedVersion].urls[0], - }); + this.setState({ + DownloadActive: true, + DownloadingRelease: true, + DownloadName: fileName, + }, () => { + this.sendRequest(Constants.IPC_Download_File, { + Directory: Constants.DATA_LOCATIONS[this.props.platform], + Filename: this.state.DownloadName, + URL: this.state.LocationsLookup[selectedVersion].urls[0], }); - } + }); }; handleUIDownload = () => { - if (ipcRenderer) { - this.setState({ - DownloadActive: true, - DownloadingUpgrade: true, - DownloadName: 'UI Upgrade', - }, ()=> { - const url = this.state.UpgradeData.urls[0]; - ipcRenderer.send(Constants.IPC_Download_File, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Filename: this.props.platform === 'win32' ? 'upgrade.exe' : this.extractFileNameFromURL(url), - URL: url, - }); + this.setState({ + DownloadActive: true, + DownloadingUpgrade: true, + DownloadName: 'UI Upgrade', + }, ()=> { + const url = this.state.UpgradeData.urls[0]; + this.sendRequest(Constants.IPC_Download_File, { + Directory: Constants.DATA_LOCATIONS[this.props.platform], + Filename: this.props.platform === 'win32' ? 'upgrade.exe' : this.extractFileNameFromURL(url), + URL: url, }); - } else { - this.setState({UpgradeDismissed: true}); - } + }); }; handleVersionChanged = (e) => { @@ -314,7 +280,7 @@ class App extends Component { installDependency = data => { if (data.Success) { - ipcRenderer.send(Constants.IPC_Install_Dependency, { + this.sendRequest(Constants.IPC_Install_Dependency, { Source: data.Destination, URL: data.URL, }); @@ -330,7 +296,7 @@ class App extends Component { installUpgrade = data => { if (data.Success) { - ipcRenderer.send(Constants.IPC_Install_Upgrade, { + this.sendRequest(Constants.IPC_Install_Upgrade, { Source: data.Destination, }); } else { @@ -404,7 +370,7 @@ class App extends Component { }; onExtractReleaseComplete = (event, arg) => { - ipcRenderer.send(Constants.IPC_Delete_File, { + this.sendRequest(Constants.IPC_Delete_File, { FilePath: arg.data.Source, }); @@ -521,7 +487,7 @@ class App extends Component { if (arg.data.Success && arg.data.Source.toLowerCase().endsWith('.dmg')) { this.waitForDependencyInstall(arg.data.URL); } else { - ipcRenderer.send(Constants.IPC_Delete_File, { + this.sendRequest(Constants.IPC_Delete_File, { FilePath: arg.data.Source, }); this.checkVersionInstalled(); @@ -529,7 +495,7 @@ class App extends Component { }; onInstallUpgradeReply = (event, arg) => { - ipcRenderer.sendSync(Constants.IPC_Delete_File, { + this.sendSyncRequest(Constants.IPC_Delete_File, { FilePath: arg.data.Source, }); @@ -541,20 +507,18 @@ class App extends Component { }; saveState = version => { - if (ipcRenderer) { - let state = { - Release: this.state.Release, - Version: version || this.state.Version, - }; - for (const provider of Constants.PROVIDER_LIST) { - state[provider] = this.state[provider]; - } - - ipcRenderer.send(Constants.IPC_Save_State, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - State: state - }); + let state = { + Release: this.state.Release, + Version: version || this.state.Version, + }; + for (const provider of Constants.PROVIDER_LIST) { + state[provider] = this.state[provider]; } + + this.sendRequest(Constants.IPC_Save_State, { + Directory: Constants.DATA_LOCATIONS[this.props.platform], + State: state + }); }; setErrorState = (error, action, critical) => { @@ -578,7 +542,7 @@ class App extends Component { }); const i = setInterval(()=> { - const ret = ipcRenderer.sendSync(Constants.IPC_Check_Dependency_Installed, { + const ret = this.sendSyncRequest(Constants.IPC_Check_Dependency_Installed, { File: dep.file, }); if (ret.data.Exists || !ret.data.Success) { diff --git a/src/containers/Configuration/Configuration.js b/src/containers/Configuration/Configuration.js index e783df7..ce89457 100644 --- a/src/containers/Configuration/Configuration.js +++ b/src/containers/Configuration/Configuration.js @@ -5,28 +5,22 @@ import Button from '../../components/UI/Button/Button'; import ConfigurationItem from '../../components/ConfigurationItem/ConfigurationItem'; import CSSModules from 'react-css-modules'; import Modal from '../../components/UI/Modal/Modal'; +import IPCContainer from '../IPCContainer/IPCContainer'; const Constants = require('../../constants'); -let ipcRenderer = null; -if (!process.versions.hasOwnProperty('electron')) { - ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null); -} - -class Configuration extends Component { +class Configuration extends IPCContainer { constructor(props) { super(props); - if (ipcRenderer) { - ipcRenderer.on(Constants.IPC_Get_Config_Template_Reply, this.onGetConfigTemplateReply); - ipcRenderer.on(Constants.IPC_Get_Config_Reply, this.onGetConfigReply); - ipcRenderer.on(Constants.IPC_Set_Config_Values_Reply, this.onSetConfigValuesReply); + this.setRequestHandler(Constants.IPC_Get_Config_Template_Reply, this.onGetConfigTemplateReply); + this.setRequestHandler(Constants.IPC_Get_Config_Reply, this.onGetConfigReply); + this.setRequestHandler(Constants.IPC_Set_Config_Values_Reply, this.onSetConfigValuesReply); - ipcRenderer.send(Constants.IPC_Get_Config_Template, { - Directory: this.props.directory, - StorageType: this.props.storageType, - Version: this.props.version, - }); - } + this.sendRequest(Constants.IPC_Get_Config_Template, { + Directory: this.props.directory, + StorageType: this.props.storageType, + Version: this.props.version, + }); } state = { @@ -78,14 +72,6 @@ class Configuration extends Component { } }; - componentWillUnmount = () => { - if (ipcRenderer) { - ipcRenderer.removeListener(Constants.IPC_Get_Config_Reply, this.onGetConfigReply); - ipcRenderer.removeListener(Constants.IPC_Get_Config_Template_Reply, this.onGetConfigTemplateReply); - ipcRenderer.removeListener(Constants.IPC_Set_Config_Values_Reply, this.onSetConfigValuesReply); - } - }; - createItemList = (config, template) => { const objectList = []; const itemList = Object @@ -165,7 +151,7 @@ class Configuration extends Component { this.setState({ Template: arg.data.Template, }, ()=> { - ipcRenderer.send(Constants.IPC_Get_Config, { + this.sendRequest(Constants.IPC_Get_Config, { Directory: this.props.directory, StorageType: this.props.storageType, Version: this.props.version, @@ -183,37 +169,35 @@ class Configuration extends Component { }; saveAndClose = () => { - if (ipcRenderer) { - this.setState({ - Saving: true, - }, ()=> { - const changedItems = []; - for (const item of this.state.ChangedItems) { - changedItems.push({ - Name: item.label, - Value: item.value, - }); - } + this.setState({ + Saving: true, + }, ()=> { + const changedItems = []; + for (const item of this.state.ChangedItems) { + changedItems.push({ + Name: item.label, + Value: item.value, + }); + } - if (this.state.ChangedObjectLookup) { - for (const key of Object.keys(this.state.ChangedObjectLookup)) { - for (const item of this.state.ChangedObjectLookup[key]) { - changedItems.push({ - Name: key + '.' + item.label, - Value: item.value, - }); - } + if (this.state.ChangedObjectLookup) { + for (const key of Object.keys(this.state.ChangedObjectLookup)) { + for (const item of this.state.ChangedObjectLookup[key]) { + changedItems.push({ + Name: key + '.' + item.label, + Value: item.value, + }); } } + } - ipcRenderer.send(Constants.IPC_Set_Config_Values, { - Directory: this.props.directory, - Items: changedItems, - StorageType: this.props.storageType, - Version: this.props.version, - }); + this.sendRequest(Constants.IPC_Set_Config_Values, { + Directory: this.props.directory, + Items: changedItems, + StorageType: this.props.storageType, + Version: this.props.version, }); - } + }); }; render() { diff --git a/src/containers/IPCContainer/IPCContainer.js b/src/containers/IPCContainer/IPCContainer.js new file mode 100644 index 0000000..9fde958 --- /dev/null +++ b/src/containers/IPCContainer/IPCContainer.js @@ -0,0 +1,51 @@ +import {Component} from 'react'; + +export default class extends Component { + constructor(props) { + super(props); + + if (!process.versions.hasOwnProperty('electron')) { + this.ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null); + } + } + + handlerList = {}; + ipcRenderer; + + componentWillUnmount() { + if (this.ipcRenderer) { + for (let name in this.handlerList) { + if (this.handlerList.hasOwnProperty(name)) { + this.ipcRenderer.removeListener(name, this.handlerList[name]); + } + } + } + + this.handlerList = {}; + }; + + sendRequest = (name, data) => { + if (this.ipcRenderer) { + this.ipcRenderer.send(name, data); + } + }; + + sendSyncRequest = (name, data) => { + if (this.ipcRenderer) { + return this.ipcRenderer.sendSync(name + '_sync', data); + } else { + return { + Success: false, + Error: 'IPC not available. Running in browser?', + }; + } + }; + + setRequestHandler = (name, callback) => { + if (this.ipcRenderer) { + this.handlerList[name] = callback; + this.ipcRenderer.on(name, callback); + } + }; + +}; \ No newline at end of file diff --git a/src/containers/MountItems/MountItems.js b/src/containers/MountItems/MountItems.js index 03dd635..2da4b9e 100644 --- a/src/containers/MountItems/MountItems.js +++ b/src/containers/MountItems/MountItems.js @@ -1,20 +1,15 @@ import React from 'react'; -import {Component} from 'react'; import Box from '../../components/UI/Box/Box'; import Button from '../../components/UI/Button/Button'; import CSSModules from 'react-css-modules'; import styles from './MountItems.css'; import Modal from '../../components/UI/Modal/Modal'; import MountItem from '../../components/MountItem/MountItem'; +import IPCContainer from '../IPCContainer/IPCContainer'; const Constants = require('../../constants'); -let ipcRenderer = null; -if (!process.versions.hasOwnProperty('electron')) { - ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null); -} - -class MountItems extends Component { +class MountItems extends IPCContainer { constructor(props) { super(props); @@ -26,13 +21,11 @@ class MountItems extends Component { }; } - if (ipcRenderer) { - ipcRenderer.on(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply); - ipcRenderer.on(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply); - ipcRenderer.on(Constants.IPC_Unmount_Drive_Reply, this.onUnmountDriveReply); + this.setRequestHandler(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply); + this.setRequestHandler(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply); + this.setRequestHandler(Constants.IPC_Unmount_Drive_Reply, this.onUnmountDriveReply); - this.detectMounts(); - } + this.detectMounts(); } retryIntervals = {}; @@ -58,24 +51,20 @@ class MountItems extends Component { } }; - componentWillUnmount = () => { + componentWillUnmount() { for (const storageType in this.state.RetryItems) { if (this.state.RetryItems.hasOwnProperty(storageType)) { this.cancelRetryMount(storageType); } } - if (ipcRenderer) { - ipcRenderer.removeListener(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply); - ipcRenderer.removeListener(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply); - ipcRenderer.removeListener(Constants.IPC_Unmount_Drive_Reply, this.onUnmountDriveReply); - } + super.componentWillUnmount(); }; detectMounts = ()=> { if (!this.state.DisplayRetry) { this.props.mountsBusy(true); - ipcRenderer.send(Constants.IPC_Detect_Mounts, { + this.sendRequest(Constants.IPC_Detect_Mounts, { Directory: this.props.directory, Version: this.props.version, }); @@ -83,7 +72,7 @@ class MountItems extends Component { }; handleBrowseLocation = (storageType, location) => { - location = ipcRenderer.sendSync(Constants.IPC_Browse_Directory, { + location = this.sendSyncRequest(Constants.IPC_Browse_Directory, { Title: storageType + ' Mount Location', Location: location, }); @@ -103,10 +92,10 @@ class MountItems extends Component { handleMountUnMount = (storageType, mount, location) => { if (!location || (location.trim().length === 0)) { this.props.errorHandler('Mount location is not set'); - } else if (ipcRenderer) { + } else { let allowAction = true; if (mount && (this.props.platform !== 'win32')) { - const result = ipcRenderer.sendSync(Constants.IPC_Check_Mount_Location, { + const result = this.sendSyncRequest(Constants.IPC_Check_Mount_Location, { Location: location, }); if (!result.Success) { @@ -127,7 +116,7 @@ class MountItems extends Component { [storageType]: storageState, }, () => { if (mount) { - ipcRenderer.send(Constants.IPC_Mount_Drive, { + this.sendRequest(Constants.IPC_Mount_Drive, { Directory: this.props.directory, Location: location, NoConsoleSupported: this.props.noConsoleSupported, @@ -135,7 +124,7 @@ class MountItems extends Component { Version: this.props.version, }); } else { - ipcRenderer.send(Constants.IPC_Unmount_Drive, { + this.sendRequest(Constants.IPC_Unmount_Drive, { Directory: this.props.directory, Location: location, StorageType: storageType,