-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.desktop.ts
34 lines (30 loc) · 1.33 KB
/
index.desktop.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import ELECTRON_EVENTS from '@desktop/ELECTRON_EVENTS';
import type {Options} from '@desktop/electronDownloadManagerType';
import type {FileDownload} from './types';
/**
* The function downloads an attachment on desktop platforms.
*/
const fileDownload: FileDownload = (url, fileName) => {
const options: Options = {
filename: fileName,
saveAs: true,
};
window.electron.send(ELECTRON_EVENTS.DOWNLOAD, {url, options});
return new Promise((resolve) => {
// This sets a timeout that will resolve the promise after 5 seconds to prevent indefinite hanging
const timeout = setTimeout(() => {
resolve();
}, 5000);
window.electron.on(ELECTRON_EVENTS.DOWNLOAD_STARTED, (...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(timeout);
resolve();
}
});
});
};
export default fileDownload;