Permalink
Browse files

2.0.5

  • Loading branch information...
1 parent 2214737 commit 2f83660a9a97553cc4cd8d725500da46c6ff59c3 Chris Scribner committed Mar 5, 2012
Showing with 70 additions and 15 deletions.
  1. +4 −0 changelog
  2. +1 −0 docs/api.md
  3. +21 −13 lib/flow.js
  4. +4 −1 lib/queue.js
  5. +1 −1 package.json
  6. +39 −0 tests/functionality.js
View
4 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
View
1 docs/api.md
@@ -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
View
34 lib/flow.js
@@ -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);
@@ -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);
@@ -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){
@@ -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 {
@@ -180,7 +188,8 @@ var parseAddArgs = Flow.prototype._parseAddArgs = function(key, responseFormat){
timeout: timeout,
timeoutIsError: timeoutIsError,
dontWait: dontWait,
- ignoreError: ignoreError
+ ignoreError: ignoreError,
+ firstArgIsError: firstArgIsError
};
};
@@ -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();
@@ -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;
@@ -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;
View
5 lib/queue.js
@@ -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){
@@ -28,6 +29,7 @@ var parseQueueArgs = function(key, responseFormat, toExecute){
timeout = obj.timeout;
timeoutIsError = obj.timeoutIsError;
ignoreError = obj.ignoreError;
+ firstArgIsError = obj.firstArgIsError;
}
return {
@@ -36,7 +38,8 @@ var parseQueueArgs = function(key, responseFormat, toExecute){
toExecute: toExecute,
timeout: timeout,
timeoutIsError: timeoutIsError,
- ignoreError: ignoreError
+ ignoreError: ignoreError,
+ firstArgIsError: firstArgIsError
};
};
View
2 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" ],
View
39 tests/functionality.js
@@ -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(){
@@ -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');
+ }
}
});

0 comments on commit 2f83660

Please sign in to comment.