Use callback during setState()

This commit is contained in:
Scott E. Graves
2018-10-15 17:15:25 -05:00
parent e4ca58e081
commit d8c5c769fd
3 changed files with 123 additions and 118 deletions

View File

@@ -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) => {