-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Feature - add priority race #1472
Closed
sandinmyjoints
wants to merge
3
commits into
caolan:master
from
sandinmyjoints:feature-priority-race
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import noop from 'lodash/noop'; | ||
import isArrayLike from 'lodash/isArrayLike'; | ||
import slice from './slice'; | ||
import wrapAsync from './wrapAsync'; | ||
|
||
export default function _priorityRace(eachfn, tasks, callback) { | ||
callback = callback || noop; | ||
var results = isArrayLike(tasks) ? [] : {}; | ||
var outerCallback = callback; | ||
var numPrioritized = tasks.filter(t => t.isPrioritized).length; | ||
var numDone = 0; | ||
|
||
eachfn(tasks, function (task, key, callback) { | ||
wrapAsync(task)(function (err, result) { | ||
if (arguments.length > 2) { | ||
result = slice(arguments, 1); | ||
} | ||
results[key] = result; | ||
if(task.isPrioritized && ++numDone === numPrioritized) { | ||
return outerCallback(err, results) | ||
} | ||
callback(err); | ||
}); | ||
}, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import isArray from 'lodash/isArray'; | ||
import noop from 'lodash/noop'; | ||
import once from './internal/once'; | ||
import eachOf from './eachOf'; | ||
import _priorityRace from './internal/priorityRace'; | ||
|
||
/** | ||
* @name priorityRace | ||
* @static | ||
* @memberOf module:ControlFlow | ||
* @method | ||
* @category Control Flow | ||
* @param {Array|Iterable|Object} tasks - A collection of | ||
* [async functions]{@link AsyncFunction} to run. | ||
* Each async function can complete with any number of optional `result` values. | ||
* Each task is non-prioritized by default but can be prioritized by setting | ||
* `isPrioritized = true`. | ||
* @param {Function} [callback] - An optional callback to run once all the prioritized | ||
* functions have completed successfully. This function gets a results array | ||
* (or object) containing all the result arguments passed to the task callbacks. | ||
* Invoked with (err, results). | ||
* @returns undefined | ||
* | ||
* @example | ||
* var tasks = [ | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'one'); | ||
* }, 100); | ||
* }, | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'two'); | ||
* }, 200); | ||
* }, | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'three'); | ||
* }, 300); | ||
* } | ||
* ] | ||
* | ||
* tasks[1].isPrioritized = true; | ||
* | ||
* async.priorityRace(tasks, | ||
* function(err, results) { | ||
* // the results array will equal ['one','two'] because the race ended as soon | ||
* // as all the prioritzed tasks finished. | ||
* }); | ||
* | ||
*/ | ||
export default function priorityRace(tasks, callback) { | ||
callback = once(callback || noop); | ||
if (!isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions')); | ||
if (!tasks.length) return callback(); | ||
_priorityRace(eachOf, tasks, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
var async = require('../lib'); | ||
var expect = require('chai').expect; | ||
var assert = require('assert'); | ||
|
||
describe('priorityRace', function () { | ||
it('should call each function in parallel and finish when one prioritized task is done', function priorityRaceTest10(done) { | ||
var finished = 0; | ||
var tasks = []; | ||
function eachTest(i) { | ||
var index = i; | ||
return function (next) { | ||
finished++; | ||
next(null, index); | ||
}; | ||
} | ||
for (var i = 0; i < 10; i++) { | ||
tasks[i] = eachTest(i); | ||
} | ||
tasks[4].isPrioritized = true | ||
async.priorityRace(tasks, function (err, result) { | ||
assert.ifError(err); | ||
//race ended when 5 finished first | ||
expect(result).to.eql([0, 1, 2, 3, 4]); | ||
assert.strictEqual(finished, 5); | ||
done(); | ||
}); | ||
}); | ||
it('should call each function in parallel and finish when all prioritized tasks are done', function priorityRaceTest20(done) { | ||
var finished = 0; | ||
var tasks = []; | ||
function eachTest(i) { | ||
var index = i; | ||
return function (next) { | ||
finished++; | ||
next(null, index); | ||
}; | ||
} | ||
for (var i = 0; i < 10; i++) { | ||
tasks[i] = eachTest(i); | ||
} | ||
tasks[0].isPrioritized = true; | ||
tasks[7].isPrioritized = true; | ||
async.priorityRace(tasks, function (err, result) { | ||
assert.ifError(err); | ||
//race ended when both 0 and 7 finished | ||
expect(result).to.eql([0, 1, 2, 3, 4, 5, 6, 7]); | ||
assert.strictEqual(finished, 8); | ||
done(); | ||
}); | ||
}); | ||
it('should callback with the first error', function priorityRaceTest30(done) { | ||
var tasks = []; | ||
function eachTest(i) { | ||
var index = i; | ||
return function (next) { | ||
setTimeout(function () { | ||
next(new Error('ERR' + index)); | ||
}, 50 - index * 2); | ||
}; | ||
} | ||
for (var i = 0; i <= 5; i++) { | ||
tasks[i] = eachTest(i); | ||
tasks[0].priority = true; | ||
} | ||
async.priorityRace(tasks, function (err, result) { | ||
assert.ok(err); | ||
assert.ok(err instanceof Error); | ||
assert.strictEqual(typeof result, 'undefined'); | ||
assert.strictEqual(err.message, 'ERR5'); | ||
done(); | ||
}); | ||
}); | ||
it('should callback when task is empty', function priorityRaceTest40(done) { | ||
async.priorityRace([], function (err, result) { | ||
assert.ifError(err); | ||
assert.strictEqual(typeof result, 'undefined'); | ||
done(); | ||
}); | ||
}); | ||
it('should callback in error when the task arg is not an Array', function priorityRaceTest50() { | ||
var errors = []; | ||
async.priorityRace(null, function (err) { | ||
errors.push(err); | ||
}); | ||
async.priorityRace({}, function (err) { | ||
errors.push(err); | ||
}); | ||
assert.strictEqual(errors.length, 2); | ||
assert.ok(errors[0] instanceof TypeError); | ||
assert.ok(errors[1] instanceof TypeError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is this getting set for?
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.
To show that when more than one task has priority, the async operation will finish if/when all of them finish, not just one of them.
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.
Sorry I meant that this should be
isPrioritized
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.
You're absolutely right, this was left over from an earlier implementation. Fixed.