Skip to content

Commit

Permalink
change download_started to completed, failed and canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
wildan-m committed May 17, 2024
1 parent f610aa5 commit c792724
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 3 additions & 1 deletion desktop/ELECTRON_EVENTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const ELECTRON_EVENTS = {
START_UPDATE: 'start-update',
UPDATE_DOWNLOADED: 'update-downloaded',
DOWNLOAD: 'download',
DOWNLOAD_STARTED: 'download-started',
DOWNLOAD_COMPLETED: 'download-completed',
DOWNLOAD_FAILED: 'download-started',
DOWNLOAD_CANCELED: 'download-canceled',
SILENT_UPDATE: 'silent-update',
} as const;

Expand Down
4 changes: 3 additions & 1 deletion desktop/contextBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const WHITELIST_CHANNELS_MAIN_TO_RENDERER = [
ELECTRON_EVENTS.UPDATE_DOWNLOADED,
ELECTRON_EVENTS.FOCUS,
ELECTRON_EVENTS.BLUR,
ELECTRON_EVENTS.DOWNLOAD_STARTED,
ELECTRON_EVENTS.DOWNLOAD_COMPLETED,
ELECTRON_EVENTS.DOWNLOAD_FAILED,
ELECTRON_EVENTS.DOWNLOAD_CANCELED,
] as const;

const getErrorMessage = (channel: string): string => `Electron context bridge cannot be used with channel '${channel}'`;
Expand Down
7 changes: 4 additions & 3 deletions desktop/createDownloadQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const createDownloadQueue = () => {
let downloadTimeout: NodeJS.Timeout;
let downloadListener: (event: Electron.Event, electronDownloadItem: Electron.DownloadItem) => void;

// Define the timeout function
const timeoutFunction = () => {
item.win.webContents.session.removeListener('will-download', downloadListener);
resolve();
Expand Down Expand Up @@ -74,6 +73,7 @@ const createDownloadQueue = () => {
return;
}

item.win.webContents.send(ELECTRON_EVENTS.DOWNLOAD_CANCELED, {url: item.url});
cleanup();
reject(new Error(errorMessage));
electronDownloadItem.cancel();
Expand All @@ -82,19 +82,20 @@ const createDownloadQueue = () => {
electronDownloadItem.on('done', (_, state) => {
cleanup();
if (state === 'cancelled') {
item.win.webContents.send(ELECTRON_EVENTS.DOWNLOAD_CANCELED, {url: item.url});
resolve();
} else if (state === 'interrupted') {
item.win.webContents.send(ELECTRON_EVENTS.DOWNLOAD_FAILED, {url: item.url});
reject(new Error(errorMessage));
} else if (state === 'completed') {
if (process.platform === 'darwin') {
const savePath = electronDownloadItem.getSavePath();
app.dock.downloadFinished(savePath);
}
item.win.webContents.send(ELECTRON_EVENTS.DOWNLOAD_COMPLETED, {url: item.url});
resolve();
}
});

item.win.webContents.send(ELECTRON_EVENTS.DOWNLOAD_STARTED, {url: item.url});
};

downloadTimeout = setTimeout(timeoutFunction, CONST.DOWNLOADS_TIMEOUT);
Expand Down
10 changes: 6 additions & 4 deletions src/libs/fileDownload/index.desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ const fileDownload: FileDownload = (url, fileName) => {
resolve();
}, CONST.DOWNLOADS_TIMEOUT);

window.electron.on(ELECTRON_EVENTS.DOWNLOAD_STARTED, (...args: unknown[]) => {
const handleDownloadStatus = (...args: unknown[]) => {
const arg = Array.isArray(args) ? args[0] : null;
const eventUrl = arg && typeof arg === 'object' && 'url' in arg ? arg.url : null;

// This event is triggered for all active download instances. We intentionally keep other promises waiting.
// Early resolution or rejection of other promises could prematurely stop the loading spinner or prevent the promise from being resolved.
if (eventUrl === url) {
clearTimeout(downloadTimeout);
resolve();
}
});
};

window.electron.on(ELECTRON_EVENTS.DOWNLOAD_COMPLETED, handleDownloadStatus);
window.electron.on(ELECTRON_EVENTS.DOWNLOAD_FAILED, handleDownloadStatus);
window.electron.on(ELECTRON_EVENTS.DOWNLOAD_CANCELED, handleDownloadStatus);
});
};

Expand Down

0 comments on commit c792724

Please sign in to comment.