Skip to content

Commit

Permalink
Add onCompleted and onTotalProgress options (#130)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
paviro and sindresorhus committed Feb 26, 2021
1 parent 9332c5c commit bd55e77
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
21 changes: 21 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ declare namespace electronDl {
transferredBytes: number;
totalBytes: number;
}

interface File {
filename: string,
path: string,
fileSize: number,
mimeType: string,
url: string
}

interface Options {
/**
Expand Down Expand Up @@ -60,11 +68,24 @@ declare namespace electronDl {
Optional callback that receives an object containing information about the progress of the current download item.
*/
readonly onProgress?: (progress: Progress) => void;

/**
Optional callback that receives an object containing information about the combined progress of all download items done within any registered window.
Each time a new download is started, the next callback will include it. The progress percentage could therefore become smaller again.
This callback provides the same data that is used for the progress bar on the app icon.
*/
readonly onTotalProgress?: (file: File) => void;

/**
Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item) for which the download has been cancelled.
*/
readonly onCancel?: (item: DownloadItem) => void;

/**
Optional callback that receives an object with information about an item that has been completed. It is called for each completed item.
*/
readonly onCompleted?: (completed: Completed) => void;

/**
Reveal the downloaded file in the system file manager, and if possible, select the file.
Expand Down
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ function registerListener(session, options, callback = () => {}) {
totalBytes: itemTotalBytes
});
}

if (typeof options.onTotalProgress === 'function') {
options.onTotalProgress({
percent: progressDownloadItems(),
transferredBytes: receivedBytes,
totalBytes
});
}
});

item.on('done', (event, state) => {
Expand Down Expand Up @@ -150,6 +158,16 @@ function registerListener(session, options, callback = () => {}) {
shell.showItemInFolder(filePath);
}

if (typeof options.onCompleted === 'function') {
options.onCompleted({
fileName: item.getFilename(),
path: item.getSavePath(),
fileSize: item.getReceivedBytes(),
mimeType: item.getMimeType(),
url: item.getURL()
});
}

callback(null, item);
}
});
Expand Down
32 changes: 32 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,44 @@ Optional callback that receives an object containing information about the progr
}
```

#### onTotalProgress

Type: `Function`

Optional callback that receives an object containing information about the combined progress of all download items done within any registered window.

Each time a new download is started, the next callback will include it. The progress percentage could therefore become smaller again.
This callback provides the same data that is used for the progress bar on the app icon.

```js
{
percent: 0.1,
transferredBytes: 100,
totalBytes: 1000
}
```

#### onCancel

Type: `Function`

Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item) for which the download has been cancelled.

#### onCompleted

Type: `Function`

Optional callback that receives an object with information about an item that has been completed. It is called for each completed item.

```js
{
filename: 'file.zip',
path: '/path/file.zip',
fileSize: 503320,
mimeType: 'application/zip',
url: 'https://example.com/file.zip'
}

#### openFolderWhenDone

Type: `boolean`\
Expand Down

0 comments on commit bd55e77

Please sign in to comment.