Skip to content

Commit

Permalink
2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Scribner committed Mar 5, 2012
1 parent 2214737 commit 2f83660
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
4 changes: 4 additions & 0 deletions changelog
@@ -1,3 +1,7 @@
2.0.5

* Adding firstArgIsError option to allow not treating the first argument passed to the callback as an error

2.0.4

* Correcting an issue with additional newlines getting added when a reference to a defer variable was the last in a block
Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Expand Up @@ -7,6 +7,7 @@
* timeout: Number of milliseconds to wait before aborting this task
* timeoutIsError: If true (the default), a timeout is considered in error and will abort the current flow. If false, a timeout will not be treated as an error.
* dontWait: If true, the result of this task will not be returned from flow.wait(). The result may only be obtained by passing a key to flow.wait.
* firstArgIsError: If false, the first argument passed to the callback is not treated as an error. Default is true. This may also be specified on the flow variable, in which case it applies to all added tasks.
* key: Same as if specified in options
* responseFormat: same as if specified in options

Expand Down
34 changes: 21 additions & 13 deletions lib/flow.js
Expand Up @@ -25,6 +25,8 @@ var Flow = function(fiber) {
this._isGenerator = false; //Tracks whether the current asyncblock was created as an enumerator or not

this.maxParallel = 0; // max number of parallel tasks. maxParallel <= 0 means no limit

this.firstArgIsError = true; //The first argument to the callback is treated as data instead of as an error
};

util.inherits(Flow, events.EventEmitter);
Expand Down Expand Up @@ -137,6 +139,10 @@ var addTask = function(self, task){
}
}

if(task.firstArgIsError == null){
task.firstArgIsError = self.firstArgIsError;
}

while (self.maxParallel > 0 && self.unfinishedCount >= self.maxParallel) {
// too many fibers running. Yield until the fiber count goes down.
yieldFiber(self);
Expand All @@ -158,6 +164,7 @@ var parseAddArgs = Flow.prototype._parseAddArgs = function(key, responseFormat){
var timeoutIsError;
var dontWait = false;
var ignoreError = false;
var firstArgIsError;

//Support single argument of responseFormat
if(key instanceof Array){
Expand All @@ -172,6 +179,7 @@ var parseAddArgs = Flow.prototype._parseAddArgs = function(key, responseFormat){
timeoutIsError = obj.timeoutIsError;
dontWait = obj.dontWait;
ignoreError = obj.ignoreError;
firstArgIsError = obj.firstArgIsError;
}

return {
Expand All @@ -180,7 +188,8 @@ var parseAddArgs = Flow.prototype._parseAddArgs = function(key, responseFormat){
timeout: timeout,
timeoutIsError: timeoutIsError,
dontWait: dontWait,
ignoreError: ignoreError
ignoreError: ignoreError,
firstArgIsError: firstArgIsError
};
};

Expand Down Expand Up @@ -234,7 +243,7 @@ var yieldFiber = function(self) {
};

var errorHandler = function(self, task){
if(task != null && task.result && task.result[0]){
if(task != null && task.result && task.result[0] && task.firstArgIsError){
if(!task.ignoreError) {
if(task.resultWasAsync) {
var err = new Error();
Expand Down Expand Up @@ -270,7 +279,7 @@ var errorHandler = function(self, task){
};

var errorParser = function(self, task) {
if(task.result && task.result[0]){
if(task.result && task.result[0] && task.firstArgIsError){
//Make sure we don't call the error callback more than once
var err;

Expand Down Expand Up @@ -300,23 +309,22 @@ var resultHandler = function(self, task){
}

if(task.responseFormat instanceof Array) {
return convertResult(task.result, task.responseFormat);
return convertResult(task);
} else {
return task.result[1];
return task.result[task.firstArgIsError ? 1 : 0];
}
};

var convertResult = function(ret, responseFormat){
var convertResult = function(task){
var formatted = {};
var ret = task.result;
var responseFormat = task.responseFormat;
var offset = task.firstArgIsError ? 1 : 0;

if(ret instanceof Array){
var min = Math.min(ret.length - 1, responseFormat.length);
var min = Math.min(ret.length - offset, responseFormat.length);

for(var i = 0; i < min; i++) {
formatted[responseFormat[i]] = ret[i + 1];
}
} else {
formatted[responseFormat[0]] = ret;
for(var i = 0; i < min; i++) {
formatted[responseFormat[i]] = ret[i + offset];
}

return formatted;
Expand Down
5 changes: 4 additions & 1 deletion lib/queue.js
Expand Up @@ -4,6 +4,7 @@ var parseQueueArgs = function(key, responseFormat, toExecute){
var timeout;
var timeoutIsError;
var ignoreError = false;
var firstArgIsError;

//Support single argument of responseFormat
if(key instanceof Array){
Expand All @@ -28,6 +29,7 @@ var parseQueueArgs = function(key, responseFormat, toExecute){
timeout = obj.timeout;
timeoutIsError = obj.timeoutIsError;
ignoreError = obj.ignoreError;
firstArgIsError = obj.firstArgIsError;
}

return {
Expand All @@ -36,7 +38,8 @@ var parseQueueArgs = function(key, responseFormat, toExecute){
toExecute: toExecute,
timeout: timeout,
timeoutIsError: timeoutIsError,
ignoreError: ignoreError
ignoreError: ignoreError,
firstArgIsError: firstArgIsError
};
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "asyncblock",
"version": "2.0.4",
"version": "2.0.5",
"description": "A simple and powerful abstraction of node-fibers",
"keywords": [
"fiber", "fibers", "coroutine", "stop", "go", "green", "red" ],
Expand Down
39 changes: 39 additions & 0 deletions tests/functionality.js
Expand Up @@ -23,6 +23,16 @@ var immedMultiple = function(callback) {
callback(null, 1, 2, 3);
};

var echoAsFirstArg = function(message, callback){
process.nextTick(function(){
callback(message);
});
};

var echoAsFirstArgSync = function(message, callback){
callback(message);
};

var delayed = function(callback){
process.nextTick(
function(){
Expand Down Expand Up @@ -975,6 +985,35 @@ suite.addBatch({
assert.equal(result.third, 'third');
assert.equal(result.fourth, 'fourth');
}
},

'When using firstArgIsError = false': {
topic: function(){
var self = this;

asyncblock(function(flow){
var result = {};

echoAsFirstArg('first', flow.add( { firstArgIsError: false } ));
result.first = flow.wait();

flow.firstArgIsError = false;

echoAsFirstArgSync('second', flow.add());
result.second = flow.wait();

echoAsFirstArg('third', flow.add( { responseFormat: ['result'] } ));
result.third = flow.wait();

self.callback(null, result);
});
},

'The results are as expected': function(result){
assert.equal(result.first, 'first');
assert.equal(result.second, 'second');
assert.equal(result.third.result, 'third');
}
}

});
Expand Down

0 comments on commit 2f83660

Please sign in to comment.