Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: amccollum/line
base: 3cdfdf9913
...
head fork: amccollum/line
compare: b3a4704358
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
36 README.md
@@ -5,25 +5,25 @@ Line is designed to work with CoffeeScript.
Here's an example:
```CoffeeScript
- line ->
- fs.readdir 'my_dir', 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 ->
+ fs.readdir 'my_dir', 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)
-
- # 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 (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 (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)
+
+ # 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 the tasks completed without errors.')
+line.run ->
+ console.log('All the tasks completed without errors.')
```
View
44 lib/line.js
@@ -12,8 +12,7 @@ line = (function() {
this.parent = parent;
this.blocks = [];
this.results = {};
- this.successCallback = null;
- this.errorCallback = null;
+ this.error = null;
this.stopped = false;
this.waiting = 0;
}
@@ -25,7 +24,7 @@ line = (function() {
line.prototype.wait = function(name, fn) {
var _this = this;
this.waiting++;
- if (!name || typeof name === 'function') {
+ if (name === void 0 || typeof name === 'function') {
fn = name;
name = ++this.id;
} else if (name === true) {
@@ -38,7 +37,7 @@ line = (function() {
if (!_this.stopped && args[0]) _this.stop.apply(_this, args);
args.shift();
if (!_this.stopped) {
- _this.results[name] = args;
+ if (name !== null) _this.results[name] = args;
if (fn) fn.apply(_this, args);
if (!--_this.waiting) return _this.next();
}
@@ -46,46 +45,41 @@ line = (function() {
};
line.prototype.stop = function() {
- var args, errorCallback, parent;
- this.stopped = true;
+ var args, error, parent;
args = Array.prototype.slice.call(arguments);
- errorCallback = this.errorCallback;
+ this.stopped = true;
+ error = this.error;
parent = this.parent;
- while (!errorCallback && parent) {
- errorCallback = parent.errorCallback;
+ while (!error && parent) {
+ error = parent.error;
parent = parent.parent;
}
- if (errorCallback) return errorCallback.apply(this, args);
+ if (error) return error.apply(this, args);
};
- line.prototype.next = function() {
- var args, result, waitCallback;
+ line.prototype.next = function(fn) {
+ var args, block, done, result;
args = this.results[this.resultId || this.id];
this.resultId = null;
if (this.blocks.length) {
- waitCallback = this.wait();
+ block = this.blocks.shift();
+ done = this.wait(null, fn);
try {
_running = this;
- result = this.blocks.shift().apply(this, args);
+ result = block.apply(this, args);
_running = null;
- waitCallback(null, result);
+ done(null, result);
} catch (e) {
- waitCallback(e);
+ done(e);
}
- } else if (this.successCallback) {
- this.successCallback.apply(this, args);
}
};
line.prototype.run = function(fn) {
- var waitCallback;
var _this = this;
_context = null;
- if (this.parent) waitCallback = this.parent.wait();
- this.successCallback = function() {
- if (this.parent) waitCallback();
- if (fn) return fn.apply(this, arguments);
- };
+ if (fn) this.add(fn);
+ if (this.parent) this.add(this.parent.wait(null));
process.nextTick(function() {
return _this.next();
});
@@ -102,7 +96,7 @@ line.add = function(fn) {
line.error = function(fn) {
if (!_context) _context = new line(_running);
- _context.errorCallback = fn;
+ _context.error = fn;
};
line.run = function(fn) {
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "line",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "Control flow like a boss",
"keywords": ["ender", "flow", "control", "async"],
"author": "Andrew McCollum <amccollum@gmail.com>",
View
47 src/line/line.coffee
@@ -10,8 +10,7 @@ class line
@parent = parent
@blocks = []
@results = {}
- @successCallback = null
- @errorCallback = null
+ @error = null
@stopped = false
@waiting = 0
@@ -23,7 +22,7 @@ class line
wait: (name, fn) ->
@waiting++
- if not name or typeof name == 'function'
+ if name == undefined or typeof name == 'function'
fn = name
name = ++@id
@@ -41,58 +40,52 @@ class line
args.shift()
if not @stopped
- @results[name] = args
+ if name != null
+ @results[name] = args
+
fn.apply(this, args) if fn
@next() if not --@waiting
stop: ->
- @stopped = true
args = Array.prototype.slice.call(arguments)
+ @stopped = true
- errorCallback = @errorCallback
+ error = @error
parent = @parent
- while not errorCallback and parent
- errorCallback = parent.errorCallback
+ while not error and parent
+ error = parent.error
parent = parent.parent
- if errorCallback
- errorCallback.apply(this, args)
+ if error
+ error.apply(this, args)
- next: ->
+ next: (fn) ->
args = @results[@resultId or @id]
@resultId = null
if @blocks.length
- waitCallback = @wait()
+ block = @blocks.shift()
+ done = @wait(null, fn)
try
_running = this
- result = @blocks.shift().apply(this, args)
+ result = block.apply(this, args)
_running = null
- waitCallback(null, result)
+ done(null, result)
catch e
- waitCallback(e)
-
- else if @successCallback
- @successCallback.apply(this, args)
+ done(e)
return
run: (fn) ->
_context = null
- if @parent
- waitCallback = @parent.wait()
+ @add(fn) if fn
+ @add(@parent.wait(null)) if @parent
- @successCallback = ->
- if @parent
- waitCallback()
-
- fn.apply(this, arguments) if fn
-
process.nextTick => @next()
return
@@ -103,7 +96,7 @@ line.add = (fn) ->
line.error = (fn) ->
_context = new line(_running) if not _context
- _context.errorCallback = fn
+ _context.error = fn
return
line.run = (fn) ->
View
6 src/test/line-test.coffee
@@ -110,10 +110,10 @@ vows.add 'line'
'should be added correctly': (results) ->
equal results['a'], 1
- equal results[2], 2
- equal results[3], 3
+ equal results[1], 2
+ equal results[2], 3
equal results['b'], 4
equal results['c'], 5
- equal results[4], 6
+ equal results[3], 6
equal results['test'], 3

No commit comments for this range

Something went wrong with that request. Please try again.