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) => { checkVersionInstalled = () => {
if (!versionLookup) {
versionLookup = this.state.VersionLookup;
}
const selectedVersion = versionLookup[this.state.ReleaseTypes[release]][version];
this.setState({ this.setState({
AllowDownload: false, 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') { ipcRenderer.send(Constants.IPC_Check_Installed, {
if (ipcRenderer) { Dependencies: dependencies,
let dependencies = []; Directory: Constants.DATA_LOCATIONS[this.props.platform],
if (this.state.LocationsLookup[selectedVersion] && this.state.LocationsLookup[selectedVersion].dependencies) { Version: selectedVersion,
dependencies = this.state.LocationsLookup[selectedVersion].dependencies; });
} }
ipcRenderer.send(Constants.IPC_Check_Installed, {
Dependencies: dependencies,
Directory: Constants.DATA_LOCATIONS[this.props.platform],
Version: selectedVersion,
});
} }
} });
}; };
closeErrorDisplay = () => { closeErrorDisplay = () => {
@@ -176,6 +172,8 @@ class App extends Component {
...this.state.Hyperspace ...this.state.Hyperspace
}; };
this.saveState(this.state.Release, this.state.Version, sia, hyperspace);
if (storageType === 'Hyperspace') { if (storageType === 'Hyperspace') {
hyperspace.AutoMount = e.target.checked; hyperspace.AutoMount = e.target.checked;
this.setState({ this.setState({
@@ -188,7 +186,6 @@ class App extends Component {
}); });
} }
this.saveState(this.state.Release, this.state.Version, sia, hyperspace);
}; };
handleConfigClicked = (storageType) => { handleConfigClicked = (storageType) => {
@@ -212,12 +209,12 @@ class App extends Component {
DownloadActive: true, DownloadActive: true,
DownloadingDependency: true, DownloadingDependency: true,
DownloadName: fileName, DownloadName: fileName,
}); }, ()=> {
ipcRenderer.send(Constants.IPC_Download_File, {
ipcRenderer.send(Constants.IPC_Download_File, { Directory: Constants.DATA_LOCATIONS[this.props.platform],
Directory: Constants.DATA_LOCATIONS[this.props.platform], Filename: this.state.DownloadName,
Filename: fileName, URL: url,
URL: url, });
}); });
} }
}; };
@@ -227,9 +224,6 @@ class App extends Component {
...this.state[storageType], ...this.state[storageType],
MountLocation: location, MountLocation: location,
}; };
this.setState({
[storageType]: state,
});
const hyperspace = (storageType === 'Hyperspace') ? state : { const hyperspace = (storageType === 'Hyperspace') ? state : {
...this.state.Hyperspace, ...this.state.Hyperspace,
}; };
@@ -238,17 +232,21 @@ class App extends Component {
}; };
this.saveState(this.state.Release, this.state.Version, sia, hyperspace); this.saveState(this.state.Release, this.state.Version, sia, hyperspace);
this.setState({
[storageType]: state,
});
}; };
handleReleaseChanged = (e) => { handleReleaseChanged = (e) => {
const val = parseInt(e.target.value, 10); const val = parseInt(e.target.value, 10);
const versionIndex = this.state.VersionLookup[this.state.ReleaseTypes[val]].length - 1; const versionIndex = this.state.VersionLookup[this.state.ReleaseTypes[val]].length - 1;
this.saveState(val, versionIndex, this.state.Sia, this.state.Hyperspace);
this.setState({ this.setState({
Release: val, Release: val,
Version: versionIndex Version: versionIndex
}, ()=> {
this.checkVersionInstalled( );
}); });
this.saveState(val, versionIndex, this.state.Sia, this.state.Hyperspace);
this.checkVersionInstalled(val, versionIndex);
}; };
handleReleaseDownload = () => { handleReleaseDownload = () => {
@@ -259,12 +257,12 @@ class App extends Component {
DownloadActive: true, DownloadActive: true,
DownloadingRelease: true, DownloadingRelease: true,
DownloadName: fileName, DownloadName: fileName,
}); }, () => {
ipcRenderer.send(Constants.IPC_Download_File, {
ipcRenderer.send(Constants.IPC_Download_File, { Directory: Constants.DATA_LOCATIONS[this.props.platform],
Directory: Constants.DATA_LOCATIONS[this.props.platform], Filename: this.state.DownloadName,
Filename: fileName, URL: this.state.LocationsLookup[selectedVersion].urls[0],
URL: this.state.LocationsLookup[selectedVersion].urls[0], });
}); });
} }
}; };
@@ -275,12 +273,12 @@ class App extends Component {
DownloadActive: true, DownloadActive: true,
DownloadingUpgrade: true, DownloadingUpgrade: true,
DownloadName: 'UI Upgrade', DownloadName: 'UI Upgrade',
}); }, ()=> {
ipcRenderer.send(Constants.IPC_Download_File, {
ipcRenderer.send(Constants.IPC_Download_File, { Directory: Constants.DATA_LOCATIONS[this.props.platform],
Directory: Constants.DATA_LOCATIONS[this.props.platform], Filename: this.props.platform === 'win32' ? 'upgrade.exe' : 'upgrade',
Filename: this.props.platform === 'win32' ? 'upgrade.exe' : 'upgrade', URL: this.state.UpgradeData.urls[0],
URL: this.state.UpgradeData.urls[0], });
}); });
} else { } else {
this.setState({UpgradeDismissed: true}); this.setState({UpgradeDismissed: true});
@@ -289,11 +287,12 @@ class App extends Component {
handleVersionChanged = (e) => { handleVersionChanged = (e) => {
const val = parseInt(e.target.value, 10); const val = parseInt(e.target.value, 10);
this.saveState(this.state.Release, val, this.state.Sia, this.state.Hyperspace);
this.setState({ this.setState({
Version: val Version: val
}, ()=> {
this.checkVersionInstalled( );
}); });
this.saveState(this.state.Release, val, this.state.Sia, this.state.Hyperspace);
this.checkVersionInstalled(this.state.Release, val);
}; };
notifyAutoMountProcessed = () => { notifyAutoMountProcessed = () => {
@@ -400,8 +399,9 @@ class App extends Component {
this.setState({ this.setState({
ExtractActive: false, ExtractActive: false,
}, ()=> {
this.checkVersionInstalled( );
}); });
this.checkVersionInstalled(this.state.Release, this.state.Version);
}; };
onGetStateReply = (event, arg) => { onGetStateReply = (event, arg) => {
@@ -417,9 +417,12 @@ class App extends Component {
Release: arg.data.Release, Release: arg.data.Release,
Sia: arg.data.Sia, Sia: arg.data.Sia,
Version: arg.data.Version, Version: arg.data.Version,
}, ()=> {
this.grabReleases();
}); });
} else {
this.grabReleases();
} }
this.grabReleases();
}; };
onGrabReleasesReply = ()=> { onGrabReleasesReply = ()=> {
@@ -437,9 +440,9 @@ class App extends Component {
Version: version, Version: version,
VersionAvailable: version !== latestVersion, VersionAvailable: version !== latestVersion,
VersionLookup: versionLookup, VersionLookup: versionLookup,
}, () => {
this.checkVersionInstalled( );
}); });
this.checkVersionInstalled(this.state.Release, version, versionLookup);
}; };
axios.get(Constants.RELEASES_URL) axios.get(Constants.RELEASES_URL)
@@ -500,7 +503,7 @@ class App extends Component {
ipcRenderer.send(Constants.IPC_Delete_File, { ipcRenderer.send(Constants.IPC_Delete_File, {
FilePath: arg.data.Source, FilePath: arg.data.Source,
}); });
this.checkVersionInstalled(this.state.Release, this.state.Version); this.checkVersionInstalled( );
}; };
onInstallUpgradeReply = (event, arg) => { onInstallUpgradeReply = (event, arg) => {

View File

@@ -164,11 +164,12 @@ class Configuration extends Component {
if (arg.data.Success) { if (arg.data.Success) {
this.setState({ this.setState({
Template: arg.data.Template, Template: arg.data.Template,
}); }, ()=> {
ipcRenderer.send(Constants.IPC_Get_Config, { ipcRenderer.send(Constants.IPC_Get_Config, {
Directory: this.props.directory, Directory: this.props.directory,
StorageType: this.props.storageType, StorageType: this.props.storageType,
Version: this.props.version, Version: this.props.version,
});
}); });
} else { } else {
this.props.errorHandler(arg.data.Error, () => { this.props.errorHandler(arg.data.Error, () => {
@@ -185,32 +186,32 @@ class Configuration extends Component {
if (ipcRenderer) { if (ipcRenderer) {
this.setState({ this.setState({
Saving: true, Saving: true,
}); }, ()=> {
const changedItems = [];
for (const item of this.state.ChangedItems) {
changedItems.push({
Name: item.label,
Value: item.value,
});
}
const changedItems = []; if (this.state.ChangedObjectLookup) {
for (const item of this.state.ChangedItems) { for (const key of Object.keys(this.state.ChangedObjectLookup)) {
changedItems.push({ for (const item of this.state.ChangedObjectLookup[key]) {
Name: item.label, changedItems.push({
Value: item.value, 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, { ipcRenderer.send(Constants.IPC_Set_Config_Values, {
Directory: this.props.directory, Directory: this.props.directory,
Items: changedItems, Items: changedItems,
StorageType: this.props.storageType, StorageType: this.props.storageType,
Version: this.props.version, Version: this.props.version,
});
}); });
} }
}; };

View File

@@ -69,28 +69,29 @@ class MountItems extends Component {
...this.state[storageType], ...this.state[storageType],
AllowMount: false, AllowMount: false,
}; };
this.setState({
[storageType]: state,
});
this.props.mountsBusy(true); this.props.mountsBusy(true);
if (mount) { this.setState({
ipcRenderer.send(Constants.IPC_Mount_Drive, { [storageType]: state,
Directory: this.props.directory, }, ()=> {
Location: location, if (mount) {
StorageType: storageType, ipcRenderer.send(Constants.IPC_Mount_Drive, {
Version: this.props.version, Directory: this.props.directory,
}); Location: location,
} else { StorageType: storageType,
ipcRenderer.send(Constants.IPC_Unmount_Drive, { Version: this.props.version,
Directory: this.props.directory, });
Location: location, } else {
PID: pid, ipcRenderer.send(Constants.IPC_Unmount_Drive, {
StorageType: storageType, Directory: this.props.directory,
Version: this.props.version, Location: location,
}); PID: pid,
} StorageType: storageType,
Version: this.props.version,
});
}
});
} }
}; };
@@ -111,30 +112,30 @@ class MountItems extends Component {
PID: arg.data.PIDS.Hyperspace, PID: arg.data.PIDS.Hyperspace,
}; };
this.props.mountsBusy(hs.Mounted || sia.Mounted);
this.setState({ this.setState({
Hyperspace: hs, Hyperspace: hs,
Sia: sia, 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 { } else {
this.props.errorHandler(arg.data.Error); this.props.errorHandler(arg.data.Error);
} }
@@ -148,9 +149,9 @@ class MountItems extends Component {
}; };
this.setState({ this.setState({
[arg.data.StorageType]: state, [arg.data.StorageType]: state,
}, ()=> {
this.detectMounts();
}); });
this.detectMounts();
}; };
onUnmountDriveReply = (event, arg) => { onUnmountDriveReply = (event, arg) => {