From 2dbb791a5f5f4d671cac583429323b762aafb2e7 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 15 Oct 2018 17:15:25 -0500 Subject: [PATCH] Use callback during setState() --- src/App.js | 103 +++++++++--------- src/containers/Configuration/Configuration.js | 55 +++++----- src/containers/MountItems/MountItems.js | 83 +++++++------- 3 files changed, 123 insertions(+), 118 deletions(-) diff --git a/src/App.js b/src/App.js index fe29cf7..06fded1 100644 --- a/src/App.js +++ b/src/App.js @@ -101,30 +101,26 @@ class App extends Component { } }; - checkVersionInstalled = (release, version, versionLookup) => { - if (!versionLookup) { - versionLookup = this.state.VersionLookup; - } - - const selectedVersion = versionLookup[this.state.ReleaseTypes[release]][version]; + checkVersionInstalled = () => { this.setState({ AllowDownload: false, - }); + }, ()=> { + const selectedVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; + if (selectedVersion !== 'unavailable') { + if (ipcRenderer) { + let dependencies = []; + if (this.state.LocationsLookup[selectedVersion] && this.state.LocationsLookup[selectedVersion].dependencies) { + dependencies = this.state.LocationsLookup[selectedVersion].dependencies; + } - 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, + }); } - - ipcRenderer.send(Constants.IPC_Check_Installed, { - Dependencies: dependencies, - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Version: selectedVersion, - }); } - } + }); }; closeErrorDisplay = () => { @@ -176,6 +172,8 @@ class App extends Component { ...this.state.Hyperspace }; + this.saveState(this.state.Release, this.state.Version, sia, hyperspace); + if (storageType === 'Hyperspace') { hyperspace.AutoMount = e.target.checked; this.setState({ @@ -188,7 +186,6 @@ class App extends Component { }); } - this.saveState(this.state.Release, this.state.Version, sia, hyperspace); }; handleConfigClicked = (storageType) => { @@ -212,12 +209,12 @@ class App extends Component { DownloadActive: true, DownloadingDependency: true, DownloadName: fileName, - }); - - ipcRenderer.send(Constants.IPC_Download_File, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Filename: fileName, - URL: url, + }, ()=> { + ipcRenderer.send(Constants.IPC_Download_File, { + Directory: Constants.DATA_LOCATIONS[this.props.platform], + Filename: this.state.DownloadName, + URL: url, + }); }); } }; @@ -227,9 +224,6 @@ class App extends Component { ...this.state[storageType], MountLocation: location, }; - this.setState({ - [storageType]: state, - }); const hyperspace = (storageType === 'Hyperspace') ? state : { ...this.state.Hyperspace, }; @@ -238,17 +232,21 @@ class App extends Component { }; this.saveState(this.state.Release, this.state.Version, sia, hyperspace); + this.setState({ + [storageType]: state, + }); }; handleReleaseChanged = (e) => { const val = parseInt(e.target.value, 10); const versionIndex = this.state.VersionLookup[this.state.ReleaseTypes[val]].length - 1; + this.saveState(val, versionIndex, this.state.Sia, this.state.Hyperspace); this.setState({ Release: val, Version: versionIndex + }, ()=> { + this.checkVersionInstalled( ); }); - this.saveState(val, versionIndex, this.state.Sia, this.state.Hyperspace); - this.checkVersionInstalled(val, versionIndex); }; handleReleaseDownload = () => { @@ -259,12 +257,12 @@ class App extends Component { DownloadActive: true, DownloadingRelease: true, DownloadName: fileName, - }); - - ipcRenderer.send(Constants.IPC_Download_File, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Filename: fileName, - URL: this.state.LocationsLookup[selectedVersion].urls[0], + }, () => { + ipcRenderer.send(Constants.IPC_Download_File, { + Directory: Constants.DATA_LOCATIONS[this.props.platform], + Filename: this.state.DownloadName, + URL: this.state.LocationsLookup[selectedVersion].urls[0], + }); }); } }; @@ -275,12 +273,12 @@ class App extends Component { DownloadActive: true, DownloadingUpgrade: true, DownloadName: 'UI Upgrade', - }); - - ipcRenderer.send(Constants.IPC_Download_File, { - Directory: Constants.DATA_LOCATIONS[this.props.platform], - Filename: this.props.platform === 'win32' ? 'upgrade.exe' : 'upgrade', - 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' : 'upgrade', + URL: this.state.UpgradeData.urls[0], + }); }); } else { this.setState({UpgradeDismissed: true}); @@ -289,11 +287,12 @@ class App extends Component { handleVersionChanged = (e) => { const val = parseInt(e.target.value, 10); + this.saveState(this.state.Release, val, this.state.Sia, this.state.Hyperspace); this.setState({ Version: val + }, ()=> { + this.checkVersionInstalled( ); }); - this.saveState(this.state.Release, val, this.state.Sia, this.state.Hyperspace); - this.checkVersionInstalled(this.state.Release, val); }; notifyAutoMountProcessed = () => { @@ -400,8 +399,9 @@ class App extends Component { this.setState({ ExtractActive: false, + }, ()=> { + this.checkVersionInstalled( ); }); - this.checkVersionInstalled(this.state.Release, this.state.Version); }; onGetStateReply = (event, arg) => { @@ -417,9 +417,12 @@ class App extends Component { Release: arg.data.Release, Sia: arg.data.Sia, Version: arg.data.Version, + }, ()=> { + this.grabReleases(); }); + } else { + this.grabReleases(); } - this.grabReleases(); }; onGrabReleasesReply = ()=> { @@ -437,9 +440,9 @@ class App extends Component { Version: version, VersionAvailable: version !== latestVersion, VersionLookup: versionLookup, + }, () => { + this.checkVersionInstalled( ); }); - - this.checkVersionInstalled(this.state.Release, version, versionLookup); }; axios.get(Constants.RELEASES_URL) @@ -500,7 +503,7 @@ class App extends Component { ipcRenderer.send(Constants.IPC_Delete_File, { FilePath: arg.data.Source, }); - this.checkVersionInstalled(this.state.Release, this.state.Version); + this.checkVersionInstalled( ); }; onInstallUpgradeReply = (event, arg) => { diff --git a/src/containers/Configuration/Configuration.js b/src/containers/Configuration/Configuration.js index 5e3e393..e783df7 100644 --- a/src/containers/Configuration/Configuration.js +++ b/src/containers/Configuration/Configuration.js @@ -164,11 +164,12 @@ class Configuration extends Component { if (arg.data.Success) { this.setState({ Template: arg.data.Template, - }); - ipcRenderer.send(Constants.IPC_Get_Config, { - Directory: this.props.directory, - StorageType: this.props.storageType, - Version: this.props.version, + }, ()=> { + ipcRenderer.send(Constants.IPC_Get_Config, { + Directory: this.props.directory, + StorageType: this.props.storageType, + Version: this.props.version, + }); }); } else { this.props.errorHandler(arg.data.Error, () => { @@ -185,32 +186,32 @@ class Configuration extends Component { if (ipcRenderer) { this.setState({ Saving: true, - }); + }, ()=> { + const changedItems = []; + for (const item of this.state.ChangedItems) { + changedItems.push({ + Name: item.label, + Value: item.value, + }); + } - 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, + ipcRenderer.send(Constants.IPC_Set_Config_Values, { + Directory: this.props.directory, + Items: changedItems, + StorageType: this.props.storageType, + Version: this.props.version, + }); }); } }; diff --git a/src/containers/MountItems/MountItems.js b/src/containers/MountItems/MountItems.js index c2c77ea..f9c08e9 100644 --- a/src/containers/MountItems/MountItems.js +++ b/src/containers/MountItems/MountItems.js @@ -69,28 +69,29 @@ class MountItems extends Component { ...this.state[storageType], AllowMount: false, }; - this.setState({ - [storageType]: state, - }); this.props.mountsBusy(true); - if (mount) { - ipcRenderer.send(Constants.IPC_Mount_Drive, { - Directory: this.props.directory, - Location: location, - StorageType: storageType, - Version: this.props.version, - }); - } else { - ipcRenderer.send(Constants.IPC_Unmount_Drive, { - Directory: this.props.directory, - Location: location, - PID: pid, - StorageType: storageType, - Version: this.props.version, - }); - } + this.setState({ + [storageType]: state, + }, ()=> { + if (mount) { + ipcRenderer.send(Constants.IPC_Mount_Drive, { + Directory: this.props.directory, + Location: location, + StorageType: storageType, + Version: this.props.version, + }); + } else { + ipcRenderer.send(Constants.IPC_Unmount_Drive, { + Directory: this.props.directory, + Location: location, + PID: pid, + StorageType: storageType, + Version: this.props.version, + }); + } + }); } }; @@ -111,30 +112,30 @@ class MountItems extends Component { PID: arg.data.PIDS.Hyperspace, }; + this.props.mountsBusy(hs.Mounted || sia.Mounted); + this.setState({ Hyperspace: hs, Sia: sia, + }, () => { + let hsLocation = arg.data.Locations.Hyperspace; + if ((hsLocation.length === 0) && (this.props.platform === 'win32')) { + hsLocation = this.props.hyperspace.MountLocation || arg.data.DriveLetters.Hyperspace[0]; + } + if (hsLocation !== this.props.hyperspace.MountLocation) { + this.props.changed('Hyperspace', hsLocation); + } + + let siaLocation = arg.data.Locations.Sia; + if ((siaLocation.length === 0) && (this.props.platform === 'win32')) { + siaLocation = this.props.sia.MountLocation || arg.data.DriveLetters.Sia[0]; + } + if (siaLocation !== this.props.sia.MountLocation) { + this.props.changed('Sia', siaLocation); + } + + this.performAutoMount(); }); - - this.props.mountsBusy(hs.Mounted || sia.Mounted); - - let hsLocation = arg.data.Locations.Hyperspace; - if ((hsLocation.length === 0) && (this.props.platform === 'win32')) { - hsLocation = this.props.hyperspace.MountLocation || arg.data.DriveLetters.Hyperspace[0]; - } - if (hsLocation !== this.props.hyperspace.MountLocation) { - this.props.changed('Hyperspace', hsLocation); - } - - let siaLocation = arg.data.Locations.Sia; - if ((siaLocation.length === 0) && (this.props.platform === 'win32')) { - siaLocation = this.props.sia.MountLocation || arg.data.DriveLetters.Sia[0]; - } - if (siaLocation !== this.props.sia.MountLocation) { - this.props.changed('Sia', siaLocation); - } - - this.performAutoMount(); } else { this.props.errorHandler(arg.data.Error); } @@ -148,9 +149,9 @@ class MountItems extends Component { }; this.setState({ [arg.data.StorageType]: state, + }, ()=> { + this.detectMounts(); }); - - this.detectMounts(); }; onUnmountDriveReply = (event, arg) => {