Comparing version 0.1.2 to 0.1.3
309
lib/line.js
@@ -1,148 +0,203 @@ | ||
var line, _context, _running; | ||
// Generated by CoffeeScript 1.3.3 | ||
var __hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
_context = null; | ||
!(function(name, definition) { | ||
if (typeof module === 'object' && module.exports) { | ||
module.exports = definition(); | ||
} else if (typeof define === 'function' && define.amd) { | ||
define(name, definition); | ||
} else { | ||
this[name] = definition(); | ||
} | ||
})('line', function() { | ||
var events, line, _context; | ||
events = require('events'); | ||
line = {}; | ||
line.active = null; | ||
line.Line = (function(_super) { | ||
_running = null; | ||
__extends(Line, _super); | ||
line = (function() { | ||
function Line(parent, listeners) { | ||
var blocks, done, event, listener; | ||
blocks = Array.prototype.slice.call(arguments); | ||
if (typeof parent === null || parent instanceof line.Line) { | ||
blocks.shift(); | ||
} else { | ||
listeners = parent; | ||
parent = null; | ||
} | ||
if (typeof listeners === 'object') { | ||
blocks.shift(); | ||
} else { | ||
listeners = {}; | ||
} | ||
if (blocks.length && typeof blocks[0] !== 'function') { | ||
blocks = blocks[0]; | ||
} | ||
this.parent = parent; | ||
this.blocks = []; | ||
this.waiting = 0; | ||
this.stopped = false; | ||
this.results = {}; | ||
this.id = 0; | ||
for (event in listeners) { | ||
listener = listeners[event]; | ||
this.on(event, listener); | ||
} | ||
this.add(blocks); | ||
if (this.parent) { | ||
done = this.parent.wait(); | ||
this.once('end', function() { | ||
var args; | ||
args = Array.prototype.slice.call(arguments); | ||
args.unshift(null); | ||
return done.apply(this, args); | ||
}); | ||
} | ||
} | ||
function line(parent) { | ||
if (this.constructor !== line) return line.add.apply(this, arguments); | ||
this.id = 0; | ||
this.parent = parent; | ||
this.blocks = []; | ||
this.results = {}; | ||
this.error = null; | ||
this.stopped = false; | ||
this.waiting = 0; | ||
} | ||
Line.prototype.add = function(blocks) { | ||
var block, _i, _len, | ||
_this = this; | ||
if (typeof blocks === 'function') { | ||
blocks = Array.prototype.slice.call(arguments); | ||
} | ||
for (_i = 0, _len = blocks.length; _i < _len; _i++) { | ||
block = blocks[_i]; | ||
this.blocks.push(block); | ||
if (this.blocks.length === 1) { | ||
process.nextTick(function() { | ||
return _this.next(); | ||
}); | ||
} | ||
} | ||
return this; | ||
}; | ||
line.prototype.add = function(block) { | ||
this.blocks.push(block); | ||
}; | ||
Line.prototype.wait = function(name, fn) { | ||
var _this = this; | ||
this.waiting++; | ||
if (name === void 0 || typeof name === 'function') { | ||
fn = name; | ||
name = ++this.id; | ||
} else if (name === true) { | ||
name = ++this.id; | ||
this.resultId = name; | ||
} | ||
return function() { | ||
var args; | ||
_this.waiting--; | ||
if (_this.stopped) { | ||
return; | ||
} | ||
args = Array.prototype.slice.call(arguments); | ||
if (args[0]) { | ||
return _this._bubble('error', args); | ||
} else { | ||
args.shift(); | ||
_this._bubble('result', args); | ||
if (name !== null) { | ||
_this.results[name] = args; | ||
} | ||
try { | ||
line.active = _this; | ||
if (fn) { | ||
fn.apply(_this, args); | ||
} | ||
line.active = null; | ||
} catch (e) { | ||
_this._bubble('error', [e]); | ||
} | ||
if (!_this.waiting && !_this.stopped) { | ||
return _this.next(); | ||
} | ||
} | ||
}; | ||
}; | ||
line.prototype.wait = function(name, fn) { | ||
var _this = this; | ||
this.waiting++; | ||
if (name === void 0 || typeof name === 'function') { | ||
fn = name; | ||
name = ++this.id; | ||
} else if (name === true) { | ||
name = ++this.id; | ||
this.resultId = name; | ||
} | ||
return function() { | ||
var args; | ||
_this.waiting--; | ||
if (_this.stopped) return; | ||
args = Array.prototype.slice.call(arguments); | ||
if (args[0]) { | ||
return _this.fail.apply(_this, args); | ||
Line.prototype.next = function() { | ||
var args, block, done, result; | ||
this.stopped = false; | ||
args = this.results[this.resultId || this.id]; | ||
this.resultId = null; | ||
if (!this.blocks.length) { | ||
this.emit.apply(this, ['end'].concat(args)); | ||
} else { | ||
args.shift(); | ||
if (name !== null) _this.results[name] = args; | ||
block = this.blocks.shift(); | ||
done = this.wait(); | ||
try { | ||
_running = _this; | ||
if (fn) fn.apply(_this, args); | ||
_running = null; | ||
line.active = this; | ||
result = block.apply(this, args); | ||
line.active = null; | ||
done(null, result); | ||
} catch (e) { | ||
_this.fail(e); | ||
done(e); | ||
} | ||
if (!_this.waiting && !_this.stopped) return _this.next(); | ||
} | ||
}; | ||
}; | ||
line.prototype.fail = function(err) { | ||
var args, error, parent; | ||
args = Array.prototype.slice.call(arguments); | ||
this.stopped = true; | ||
if (err instanceof Error) { | ||
console.log(err.stack); | ||
} else { | ||
console.log(args); | ||
} | ||
error = this.error; | ||
parent = this.parent; | ||
while (!error && parent) { | ||
error = parent.error; | ||
parent = parent.parent; | ||
} | ||
if (error) return error.apply(this, args); | ||
}; | ||
Line.prototype.fail = function() { | ||
var args; | ||
args = Array.prototype.slice.call(arguments); | ||
this._bubble('error', args); | ||
}; | ||
line.prototype.end = function(fn) { | ||
this.stopped = true; | ||
this.blocks = this.blocks.slice(-1); | ||
this.next(fn); | ||
}; | ||
Line.prototype.stop = function() { | ||
var args; | ||
this.stopped = true; | ||
args = Array.prototype.slice.call(arguments); | ||
this._bubble('stop', args); | ||
}; | ||
line.prototype.next = function(fn) { | ||
var args, block, done, result; | ||
args = this.results[this.resultId || this.id]; | ||
this.resultId = null; | ||
if (this.blocks.length) { | ||
block = this.blocks.shift(); | ||
done = this.wait(fn); | ||
try { | ||
_running = this; | ||
result = block.apply(this, args); | ||
_running = null; | ||
done(null, result); | ||
} catch (e) { | ||
done(e); | ||
Line.prototype._bubble = function(event, args) { | ||
var cur; | ||
args = [event].concat(args); | ||
if (!this._events || !this._events[event]) { | ||
cur = this; | ||
while ((cur = cur.parent)) { | ||
if (cur._events && cur._events[event]) { | ||
if (cur.emit.apply(cur, args) !== true) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
return this.emit.apply(this, args); | ||
}; | ||
return Line; | ||
})(events.EventEmitter); | ||
_context = null; | ||
line.add = function() { | ||
if (!_context) { | ||
_context = new line.Line(line.active); | ||
} | ||
_context.add.apply(_context, arguments); | ||
}; | ||
line.prototype.run = function(fn) { | ||
var done; | ||
var _this = this; | ||
_context = null; | ||
if (fn) this.add(fn); | ||
if (this.parent) { | ||
done = this.parent.wait(); | ||
this.add(function() { | ||
var args; | ||
args = Array.prototype.slice.call(arguments); | ||
args.unshift(null); | ||
return done.apply(_this, args); | ||
}); | ||
line.error = function(listener) { | ||
if (!_context) { | ||
_context = new line.Line(line.active); | ||
} | ||
process.nextTick(function() { | ||
return _this.next(); | ||
}); | ||
_context.on('error', listener); | ||
}; | ||
line.run = function() { | ||
if (!_context) { | ||
_context = new line.Line(line.active); | ||
} | ||
_context.add.apply(_context, arguments); | ||
_context = null; | ||
}; | ||
line.wait = function(fn) { | ||
return line.active.wait(fn); | ||
}; | ||
line.fail = function() { | ||
return line.active.fail.apply(line.active, arguments); | ||
}; | ||
line.stop = function() { | ||
return line.active.stop.apply(line.active, arguments); | ||
}; | ||
return line; | ||
})(); | ||
line.add = function(fn) { | ||
if (!_context) _context = new line(_running); | ||
_context.add(fn); | ||
}; | ||
line.error = function(fn) { | ||
if (!_context) _context = new line(_running); | ||
_context.error = fn; | ||
}; | ||
line.run = function(fn) { | ||
if (!_context) _context = new line(_running); | ||
_context.run(fn); | ||
}; | ||
line.wait = function(fn) { | ||
return _running.wait(fn); | ||
}; | ||
line.end = function(fn) { | ||
return _running.end(fn); | ||
}; | ||
line.fail = function() { | ||
return _running.fail.apply(_running, arguments); | ||
}; | ||
module.exports = line; | ||
}); |
{ | ||
"name": "line", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Control flow like a boss", | ||
@@ -5,0 +5,0 @@ "keywords": ["ender", "flow", "control", "async"], |
line | ||
=== | ||
Line is designed to work with CoffeeScript. | ||
Line is flow control designed to work with CoffeeScript. | ||
@@ -8,23 +8,27 @@ Here's an example: | ||
```CoffeeScript | ||
line -> | ||
fs.readdir 'my_dir', line.wait() | ||
Line = require('line').Line | ||
data2 = null | ||
l = new Line | ||
error: (err) -> console.log('Oh no! One of the callbacks had an error:', err) | ||
-> fs.readdir 'my_dir', @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() | ||
(files) -> | ||
# Callback results can be named, or observed | ||
fs.readFile "my_dir/#{files[0]}", 'utf8', @wait('data1') | ||
fs.readFile "my_dir/#{files[1]}", 'utf8', @wait (data) -> data2 = data | ||
fs.stat "my_dir/#{files[2]}", @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) | ||
# By default, the result of the last wait() call will be passed on | ||
(stats) -> | ||
console.log('Contents of the first file:', @results.data1) | ||
console.log('Contents of the second file:', data2) | ||
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!') | ||
# If there is no wait() call, the callback will complete immediately | ||
line.run -> | ||
console.log('All the tasks completed without errors.') | ||
# Blocks can also be added later | ||
l.add -> console.log('All the tasks completed without errors.') | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23802
383
34
1