Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple download #1

Merged
merged 3 commits into from
Jul 2, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ const progressDownloadItems = function (item) {

function registerListener(session) {
const listener = (e, item, webContents) => {
const url = decodeURIComponent(_.first(item.getURLChain()));
const urlChains = item.getURLChain();
const originUrl = _.first(urlChains);
const key = decodeURIComponent(originUrl);
const defaultHanlder = {
options: {},
resolve: () => { },
reject: () => { }
};
const {options, resolve, reject} = handlerMap.get(url) || defaultHanlder;
const {options, resolve, reject} = handlerMap.get(key) || defaultHanlder;

downloadItems.add(item);
totalBytes += item.getTotalBytes();
Expand Down Expand Up @@ -120,8 +122,8 @@ function registerListener(session) {

resolve(item);
}
if (handlerMap.has(url)) {
handlerMap.delete(url);
if (handlerMap.has(key)) {
handlerMap.delete(key);
}
});
};
Expand All @@ -132,9 +134,16 @@ function registerListener(session) {
}
}

function unregisterListener (session) {
if (sessionListenerMap.has(session)) {
sessionListenerMap.delete(session);
}
}

module.exports = (options = {}) => {
app.on('session-created', session => {
registerListener(session, options);
app.on('close', () => unregisterListener(session));
});
};

Expand All @@ -143,6 +152,7 @@ module.exports.download = (win, url, options) => new Promise((resolve, reject) =

handlerMap.set(decodeURIComponent(url), {options, resolve, reject});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to also rename url to key? Do we actually use the url?

registerListener(win.webContents.session);
win.on('close', () => unregisterListener(win.webContents.session));

win.webContents.downloadURL(url);
});