From 7c54747a7adefdd86826b32be6f4bf63271e788d Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 5 Jul 2019 19:21:54 -0500 Subject: [PATCH] [#27: Implement Bitbucket backup download location] [Prefer 'ipcRenderer.once()'] --- src/App.js | 4 +- src/redux/actions/download_actions.js | 63 ++++++++++++++++----------- src/redux/actions/install_actions.js | 13 ++---- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/App.js b/src/App.js index 7b7139a..3e5b75f 100644 --- a/src/App.js +++ b/src/App.js @@ -109,7 +109,7 @@ class App extends IPCContainer { handleDownloadRelease = () => { const selectedVersion = this.getSelectedVersion(); const fileName = selectedVersion + '.zip'; - this.props.downloadItem(fileName, Constants.INSTALL_TYPES.Release, this.props.LocationsLookup[selectedVersion].urls[0], this.onDownloadFileComplete); + this.props.downloadItem(fileName, Constants.INSTALL_TYPES.Release, this.props.LocationsLookup[selectedVersion].urls, this.onDownloadFileComplete); }; handleDownloadUpgrade = () => { @@ -118,7 +118,7 @@ class App extends IPCContainer { (this.props.Platform === 'darwin') ? 'upgrade.dmg' : 'repertory-ui_' + this.props.UpgradeVersion + '_linux_x86_64.AppImage'; - this.props.downloadItem(name, Constants.INSTALL_TYPES.Upgrade, this.props.UpgradeData.urls[0], this.onDownloadFileComplete); + this.props.downloadItem(name, Constants.INSTALL_TYPES.Upgrade, this.props.UpgradeData.urls, this.onDownloadFileComplete); }; installDependency = result => { diff --git a/src/redux/actions/download_actions.js b/src/redux/actions/download_actions.js index 204b1d8..2649740 100644 --- a/src/redux/actions/download_actions.js +++ b/src/redux/actions/download_actions.js @@ -19,33 +19,44 @@ export const setDownloadBegin = (name, type, url) => { export const setDownloadEnd = createAction('download/setDownloadEnd'); export const setDownloadProgress = createAction('download/setDownloadProgress'); -export const downloadItem = (name, type, url, completedCallback) => { +export const downloadItem = (name, type, urls, completedCallback) => { return (dispatch, getState) => { - const state = getState(); - if (!state.download.DownloadActive && state.download.AllowDownload) { - const ipcRenderer = getIPCRenderer(); - if (ipcRenderer) { - dispatch(setDownloadBegin(name, type, url)); - - const downloadFileProgress = (_, arg) => { - dispatch(setDownloadProgress(arg.data.Progress)); - }; - - const downloadFileComplete = (_, arg) => { - ipcRenderer.removeListener(Constants.IPC_Download_File_Progress, downloadFileProgress); - ipcRenderer.removeListener(Constants.IPC_Download_File_Complete, downloadFileComplete); - completedCallback(name, type, url, arg.data); - dispatch(setDownloadEnd(arg.data)); - }; - - ipcRenderer.on(Constants.IPC_Download_File_Progress, downloadFileProgress); - ipcRenderer.on(Constants.IPC_Download_File_Complete, downloadFileComplete); - - ipcRenderer.send(Constants.IPC_Download_File, { - Filename: name, - URL: url, - }); - } + if (!Array.isArray(urls)) { + urls = [urls]; } + + const downloadAtIndex = index => { + const url = urls[index]; + const state = getState(); + if ((index > 0) || (!state.download.DownloadActive && state.download.AllowDownload)) { + const ipcRenderer = getIPCRenderer(); + if (ipcRenderer) { + dispatch(setDownloadBegin(name, type, url)); + + const downloadFileProgress = (_, arg) => { + dispatch(setDownloadProgress(arg.data.Progress)); + }; + + const downloadFileComplete = (_, arg) => { + ipcRenderer.removeListener(Constants.IPC_Download_File_Progress, downloadFileProgress); + if (!arg.data.Success && (++index < urls.length)) { + downloadAtIndex(index); + } else { + completedCallback(name, type, url, arg.data); + dispatch(setDownloadEnd(arg.data)); + } + }; + + ipcRenderer.on(Constants.IPC_Download_File_Progress, downloadFileProgress); + ipcRenderer.once(Constants.IPC_Download_File_Complete, downloadFileComplete); + + ipcRenderer.send(Constants.IPC_Download_File, { + Filename: name, + URL: url, + }); + } + } + }; + downloadAtIndex(0); }; }; \ No newline at end of file diff --git a/src/redux/actions/install_actions.js b/src/redux/actions/install_actions.js index 16c8384..ed3d53a 100644 --- a/src/redux/actions/install_actions.js +++ b/src/redux/actions/install_actions.js @@ -18,7 +18,6 @@ const ipcRenderer = getIPCRenderer(); export const checkInstalled = (dependencies, version) => { return (dispatch, getState) => { const checkInstalledComplete = (event, arg) => { - ipcRenderer.removeListener(Constants.IPC_Check_Installed_Reply, checkInstalledComplete); const result = arg.data; const updateState = () => { const installedVersion = result.Success && result.Exists ? result.Version : 'none'; @@ -48,7 +47,7 @@ export const checkInstalled = (dependencies, version) => { } }; - ipcRenderer.on(Constants.IPC_Check_Installed_Reply, checkInstalledComplete); + ipcRenderer.once(Constants.IPC_Check_Installed_Reply, checkInstalledComplete); ipcRenderer.send(Constants.IPC_Check_Installed, { Dependencies: dependencies, Version: version, @@ -62,8 +61,6 @@ export const installDependency = (source, url, completedCallback) => { dispatch(setInstallActive(Constants.INSTALL_TYPES.Dependency)); const installDependencyComplete = (event, arg) => { - ipcRenderer.removeListener(Constants.IPC_Install_Dependency_Reply, installDependencyComplete); - const result = arg.data; const handleCompleted = ()=> { ipcRenderer.send(Constants.IPC_Delete_File, { @@ -94,7 +91,7 @@ export const installDependency = (source, url, completedCallback) => { } }; - ipcRenderer.on(Constants.IPC_Install_Dependency_Reply, installDependencyComplete); + ipcRenderer.once(Constants.IPC_Install_Dependency_Reply, installDependencyComplete); ipcRenderer.send(Constants.IPC_Install_Dependency, { Source: source, URL: url, @@ -109,7 +106,6 @@ export const installRelease = (source, version, completedCallback) => { dispatch(setInstallActive(Constants.INSTALL_TYPES.Release)); const extractReleaseComplete = (event, arg) => { - ipcRenderer.removeListener(Constants.IPC_Extract_Release_Complete, extractReleaseComplete); ipcRenderer.send(Constants.IPC_Delete_File, { FilePath: source, }); @@ -118,7 +114,7 @@ export const installRelease = (source, version, completedCallback) => { completedCallback(source, version, arg.data); }; - ipcRenderer.on(Constants.IPC_Extract_Release_Complete, extractReleaseComplete); + ipcRenderer.once(Constants.IPC_Extract_Release_Complete, extractReleaseComplete); ipcRenderer.send(Constants.IPC_Extract_Release, { Source: source, Version: version, @@ -134,7 +130,6 @@ export const installUpgrade = (source, sha256, signature, skipVerification, comp dispatch(setApplicationReady(false)); const installUpgradeComplete = (event, arg) => { - ipcRenderer.removeListener(Constants.IPC_Install_Upgrade_Reply, installUpgradeComplete); if (arg.data.Success) { dispatch(shutdownApplication()); } else { @@ -144,7 +139,7 @@ export const installUpgrade = (source, sha256, signature, skipVerification, comp } }; - ipcRenderer.on(Constants.IPC_Install_Upgrade_Reply, installUpgradeComplete); + ipcRenderer.once(Constants.IPC_Install_Upgrade_Reply, installUpgradeComplete); ipcRenderer.send(Constants.IPC_Install_Upgrade, { Sha256: sha256, Signature: signature,