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

When a number is passed as concurrent parameter, execute the tasks in… #56

Merged
merged 5 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
20 changes: 12 additions & 8 deletions index.js
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');
Expand Down Expand Up @@ -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) {
Copy link
Owner

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

if (this._options.concurrent === true) {
    concurrency = Infinity;
} else if (...) {

} else {
    concurrency = 1;
}

Copy link
Owner

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 set this._options.concurrent to the correct value for p-map.

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';
Copy link
Owner

Choose a reason for hiding this comment

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

Infinity, not 'Infinity' :)

}

const tasks = pMap(this._tasks, task => {
this._checkAll(context);
return runTask(task, context, errors);
}, {concurrency});

return tasks
.then(() => {
if (errors.length > 0) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"log-symbols": "^1.0.2",
"log-update": "^1.0.2",
"ora": "^0.2.3",
"p-map": "^1.1.1",
"rxjs": "^5.0.0-beta.11",
"stream-to-observable": "^0.1.0",
"strip-ansi": "^3.0.1"
Expand Down
84 changes: 84 additions & 0 deletions test/concurrent.js
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, [
Copy link
Owner

Choose a reason for hiding this comment

The 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();
Copy link
Owner

Choose a reason for hiding this comment

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

Add new line

});