Comparing version 0.0.3 to 0.1.0
@@ -11,5 +11,6 @@ var line, _context, _running; | ||
if (this.constructor !== line) return line.add.apply(this, arguments); | ||
this.id = 0; | ||
this.parent = parent; | ||
this.blocks = []; | ||
this.results = []; | ||
this.results = {}; | ||
this.successCallback = null; | ||
@@ -25,5 +26,12 @@ this.errorCallback = null; | ||
line.prototype.wait = function(fn) { | ||
line.prototype.wait = function(name, fn) { | ||
var _this = this; | ||
this.waiting++; | ||
if (!name || typeof name === 'function') { | ||
fn = name; | ||
name = ++this.id; | ||
} else if (name === true) { | ||
name = ++this.id; | ||
this.resultId = name; | ||
} | ||
return function() { | ||
@@ -35,5 +43,5 @@ var args; | ||
if (!_this.stopped) { | ||
_this.results.push(args); | ||
_this.results[name] = args; | ||
if (fn) fn.apply(_this, args); | ||
if (!--_this.waiting) return _this.next(args); | ||
if (!--_this.waiting) return _this.next(); | ||
} | ||
@@ -56,9 +64,12 @@ }; | ||
line.prototype.next = function(args) { | ||
var result, waitCallback; | ||
line.prototype.next = function() { | ||
var args, result, waitCallback; | ||
args = this.results[this.resultId || this.id]; | ||
this.resultId = null; | ||
if (this.blocks.length) { | ||
_running = this; | ||
waitCallback = this.wait(); | ||
try { | ||
_running = this; | ||
result = this.blocks.shift().apply(this, args); | ||
_running = null; | ||
waitCallback(null, result); | ||
@@ -91,5 +102,5 @@ } catch (e) { | ||
line.add = function(block) { | ||
line.add = function(fn) { | ||
if (!_context) _context = new line(_running); | ||
_context.add(block); | ||
_context.add(fn); | ||
}; | ||
@@ -96,0 +107,0 @@ |
{ | ||
"name": "line", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Control flow like a boss", | ||
@@ -5,0 +5,0 @@ "keywords": ["ender", "flow", "control", "async"], |
@@ -7,19 +7,24 @@ line | ||
```js | ||
```CoffeeScript | ||
line -> | ||
taskWithCallback line.wait() | ||
fs.readdir 'my_dir', line.wait() | ||
line (firstTaskResult) -> | ||
anotherTask firstTaskResult, line.wait() | ||
line (files) -> | ||
fs.readFile "my_dir/#{files[0]}", 'utf8', line.wait('f1') | ||
fs.readFile "my_dir/#{files[1]}", 'utf8', line.wait('f2') | ||
fs.stat "my_dir/#{files[2]}", line.wait() | ||
line -> | ||
finalTask line.wait() | ||
line (stats) -> | ||
console.log('Contents of the first file:', @results.f1) | ||
console.log('Contents of the second file:', @results.f2) | ||
console.log('Result of fs.stat for the third file:', stats) | ||
line.catch (err) -> | ||
console.log('Oh no! One of the tasks had an error!') | ||
throw err | ||
# If there is no line.wait() call, the callback will complete immediately | ||
line.error (err) -> | ||
console.log('Oh no! One of the callbacks showed an error!') | ||
line.run -> | ||
console.log('All three tasks should be completed') | ||
console.log('All the tasks completed without errors.') | ||
``` |
@@ -1,2 +0,2 @@ | ||
var assert, equal, line, timeout, vows; | ||
var assert, echo, equal, line, timeout, vows; | ||
@@ -21,2 +21,8 @@ assert = require('assert'); | ||
echo = function(val, fn) { | ||
return process.nextTick(function() { | ||
return fn(null, val); | ||
}); | ||
}; | ||
vows.add('line', { | ||
@@ -126,3 +132,32 @@ 'simple test': { | ||
} | ||
}, | ||
'arguments to wait()': { | ||
topic: function() { | ||
var success; | ||
success = this.success; | ||
line(function() { | ||
echo(1, line.wait('a')); | ||
echo(2, line.wait()); | ||
echo(3, line.wait(true)); | ||
echo(4, line.wait('b')); | ||
echo(5, line.wait('c')); | ||
return echo(6, line.wait()); | ||
}); | ||
line(function(result) { | ||
return this.results['test'] = result; | ||
}); | ||
return line.run(function() { | ||
return success(this.results); | ||
}); | ||
}, | ||
'should be added correctly': function(results) { | ||
equal(results['a'], 1); | ||
equal(results[2], 2); | ||
equal(results[3], 3); | ||
equal(results['b'], 4); | ||
equal(results['c'], 5); | ||
equal(results[4], 6); | ||
return equal(results['test'], 3); | ||
} | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18970
341
30