-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
When a number is passed as concurrent parameter, execute the tasks in… #56
Changes from 2 commits
f2b0809
84c957b
e6b3874
e75968e
f5f64ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
'use strict'; | ||
const pMap = require('p-map'); | ||
const Task = require('./lib/task'); | ||
const TaskWrapper = require('./lib/task-wrapper'); | ||
const renderer = require('./lib/renderer'); | ||
|
@@ -79,17 +80,20 @@ class Listr { | |
|
||
this._checkAll(context); | ||
|
||
let tasks; | ||
if (this._options.concurrent === true) { | ||
tasks = Promise.all(this._tasks.map(task => runTask(task, context, errors))); | ||
let concurrency; | ||
if (!this._options.concurrent) { | ||
concurrency = 1; | ||
} else if (typeof this._options.concurrent === 'number') { | ||
concurrency = this._options.concurrent; | ||
} else { | ||
tasks = this._tasks.reduce((promise, task) => promise.then(() => { | ||
this._checkAll(context); | ||
|
||
return runTask(task, context, errors); | ||
}), Promise.resolve()); | ||
concurrency = 'Infinity'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
const tasks = pMap(this._tasks, task => { | ||
this._checkAll(context); | ||
return runTask(task, context, errors); | ||
}, {concurrency}); | ||
|
||
return tasks | ||
.then(() => { | ||
if (errors.length > 0) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {serial as test} from 'ava'; | ||
import delay from 'delay'; | ||
import Listr from '../'; | ||
import SimpleRenderer from './fixtures/simple-renderer'; | ||
import {testOutput} from './fixtures/utils'; | ||
|
||
const tasks = [ | ||
{ | ||
title: 'P1', | ||
task: () => delay(100) | ||
}, | ||
{ | ||
title: 'P2', | ||
task: () => delay(200) | ||
}, | ||
{ | ||
title: 'P3', | ||
task: () => delay(300) | ||
}, | ||
{ | ||
title: 'P4', | ||
task: () => delay(400) | ||
} | ||
]; | ||
|
||
test('run tasks sequentially if concurrency is undefined', async t => { | ||
const list = new Listr(tasks, { | ||
renderer: SimpleRenderer | ||
}); | ||
|
||
testOutput(t, [ | ||
'P1 [started]', | ||
'P1 [completed]', | ||
'P2 [started]', | ||
'P2 [completed]', | ||
'P3 [started]', | ||
'P3 [completed]', | ||
'P4 [started]', | ||
'P4 [completed]', | ||
'done' | ||
]); | ||
|
||
await list.run(); | ||
}); | ||
|
||
test('run tasks in parallel if concurrency is true', async t => { | ||
const list = new Listr(tasks, { | ||
renderer: SimpleRenderer, | ||
concurrent: true | ||
}); | ||
|
||
testOutput(t, [ | ||
'P1 [started]', | ||
'P2 [started]', | ||
'P3 [started]', | ||
'P4 [started]', | ||
'P1 [completed]', | ||
'P2 [completed]', | ||
'P3 [completed]', | ||
'P4 [completed]', | ||
'done' | ||
]); | ||
|
||
await list.run(); | ||
}); | ||
|
||
test('run tasks in sequential parallel chunks, if concurrency is a number', async t => { | ||
const list = new Listr(tasks, { | ||
renderer: SimpleRenderer, | ||
concurrent: 2 | ||
}); | ||
testOutput(t, [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new line |
||
'P1 [started]', | ||
'P2 [started]', | ||
'P1 [completed]', | ||
'P3 [started]', | ||
'P2 [completed]', | ||
'P4 [started]', | ||
'P3 [completed]', | ||
'P4 [completed]', | ||
'done' | ||
]); | ||
await list.run(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new line |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do it the other way around
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also wondering, wouldn't it be cleaner if we did all of this in the
constructor
instead? And thus setthis._options.concurrent
to the correct value forp-map
.