Abstract electron download path behind an opaque id (#292)

This commit is contained in:
Michael Telatynski 2021-12-21 15:34:59 +00:00 committed by GitHub
parent f18be113c3
commit 99ce48be8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -245,8 +245,13 @@ function onEditableContextMenu(ev: Event, params: ContextMenuParams) {
ev.preventDefault(); ev.preventDefault();
} }
ipcMain.on('userDownloadOpen', function(ev: IpcMainEvent, { path }) { let userDownloadIndex = 0;
shell.openPath(path); const userDownloadMap = new Map<number, string>(); // Map from id to path
ipcMain.on('userDownloadAction', function(ev: IpcMainEvent, { id, open = false }) {
if (open) {
shell.openPath(userDownloadMap.get(id));
}
userDownloadMap.delete(id);
}); });
export default (webContents: WebContents): void => { export default (webContents: WebContents): void => {
@ -270,8 +275,10 @@ export default (webContents: WebContents): void => {
item.once('done', (event, state) => { item.once('done', (event, state) => {
if (state === 'completed') { if (state === 'completed') {
const savePath = item.getSavePath(); const savePath = item.getSavePath();
const id = userDownloadIndex++;
userDownloadMap.set(id, savePath);
webContents.send('userDownloadCompleted', { webContents.send('userDownloadCompleted', {
path: savePath, id,
name: path.basename(savePath), name: path.basename(savePath),
}); });
} }