Skip to content

Commit

Permalink
downloader: move worker into own function
Browse files Browse the repository at this point in the history
  • Loading branch information
shagu committed Jan 30, 2024
1 parent 54b48c6 commit 405f3dd
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/main/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ class Downloader {
})
}

// TODO same url? same path? return existing promise
queue (url, path, notify, force, queueName) {
async worker (process) {
while (process.tasks[0]) {
const current = process.tasks.shift()
await this.fetch(current.url, current.path, current.notify, current.force, current.queue)
}

process.promise = null
}

async queue (url, path, notify, force, queueName) {
// initialize empty process list by name
if (!this.processes[queueName]) {
this.processes[queueName] = { count: 0, tasks: [] }
Expand All @@ -108,17 +116,9 @@ class Downloader {
})
}

// runner
// worker
if (!process.promise) {
process.promise = new Promise(async (resolve, reject) => {
while (process.tasks[0]) {
const current = process.tasks.shift()
await this.fetch(current.url, current.path, current.notify, current.force, current.queue)
}

process.promise = null
resolve(true)
})
process.promise = this.worker(process)
}

return process.promise
Expand Down

0 comments on commit 405f3dd

Please sign in to comment.