[#27: Implement Bitbucket backup download location] [Prefer 'ipcRenderer.once()']

This commit is contained in:
Scott E. Graves
2019-07-05 19:21:54 -05:00
parent e8b743ae0b
commit 7c54747a7a
3 changed files with 43 additions and 37 deletions

View File

@@ -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);
};
};