diff --git a/src/update.js b/src/update.js index 4a87655..2524d8a 100644 --- a/src/update.js +++ b/src/update.js @@ -57,12 +57,31 @@ function downloadFile(url, filePath) { }); } -function processDownloadQueue(baseDirectory, fileEntries) { +function processDownloadQueue(baseDirectory, baseURL, fileEntries) { return new Promise(function (resolve, reject) { let runningDownloads = 0; + let queue = [...fileEntries]; + + let finishCallback = function () { + if (queue.length > 0) { + + } + }; for (let d = 0; d < module.exports.concurrentDownloads; d++) { + if (queue.length < 1) break; + let currentEntry = queue.shift(); + + downloadFile(baseURL + currentEntry.hash, path.join(baseDirectory, currentEntry.name)).then(finishCallback).catch((function (error) { + if (!this.retries) { + this.retries = 0; + } + if (this.retries > 3) return reject(error); + + this.retries++; + queue.push(this); + }).bind(currentEntry)); } }); } @@ -86,9 +105,9 @@ module.exports = { }); }); }, - "downloadFiles": function (baseDirectory, fileEntries) { + "downloadFiles": function (baseDirectory, baseURL, fileEntries) { return new Promise(function (resolve, reject) { - processDownloadQueue(baseDirectory, fileEntries.filter(function (entry) { + processDownloadQueue(baseDirectory, baseURL, fileEntries.filter(function (entry) { return !entry.ok; })).then(function () { resolve(true);