Comparing version 0.0.0 to 0.1.0
114
chan.js
'use strict'; | ||
var call | ||
, arg | ||
, make; | ||
var make | ||
, Channel; | ||
call = function(value, cb) { | ||
if (value instanceof Error) { | ||
cb(value); | ||
/** | ||
* Make a channel. | ||
* | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
make = function make() { | ||
var chan = new Channel(); | ||
return function(a, b) { | ||
// yielded | ||
if (typeof a === 'function') { | ||
chan.get(a); | ||
return; | ||
} | ||
// (err, res) | ||
if (a === null && typeof b !== 'undefined') { | ||
a = b; | ||
} | ||
// value | ||
chan.add(a); | ||
}; | ||
}; | ||
/** | ||
* Initialize a `Channel`. | ||
* | ||
* @api priate | ||
*/ | ||
Channel = function Channel() { | ||
this.queue = []; | ||
this.items = []; | ||
}; | ||
/** | ||
* Get an item with `cb`. | ||
* | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
Channel.prototype.get = function(cb){ | ||
if (this.items.length > 0) { | ||
this.call(cb, this.items.shift()); | ||
} else { | ||
cb(null, value); | ||
this.queue.push(cb); | ||
} | ||
}; | ||
arg = function(args) { | ||
var i = 0; | ||
if (args[0] === null && args.length > 1) { | ||
i = 1; | ||
/** | ||
* Add `val` to the channel. | ||
* | ||
* @param {Mixed} val | ||
* @api private | ||
*/ | ||
Channel.prototype.add = function(val){ | ||
if (this.queue.length > 0) { | ||
this.call(this.queue.shift(), val); | ||
} else { | ||
this.items.push(val); | ||
} | ||
return args[i]; | ||
}; | ||
make = function() { | ||
var items = [] | ||
, queue = []; | ||
return function(cb) { | ||
if (typeof cb !== 'function') { | ||
if (queue.length > 0) { | ||
call(arg(arguments), queue.shift()); | ||
} else { | ||
items.push(arg(arguments)); | ||
} | ||
} else { | ||
if (items.length > 0) { | ||
call(items.shift(), cb); | ||
} else { | ||
queue.push(cb); | ||
} | ||
} | ||
}; | ||
/** | ||
* Invoke `cb` with `val` facilitate both | ||
* `chan(value)` and the `chan(error, value)` | ||
* use-cases. | ||
* | ||
* @param {Function} cb | ||
* @param {Mixed} val | ||
* @api private | ||
*/ | ||
Channel.prototype.call = function(cb, val){ | ||
if (val instanceof Error) { | ||
cb(val); | ||
} else { | ||
cb(null, val); | ||
} | ||
}; | ||
/** | ||
* Expose `make()`. | ||
*/ | ||
module.exports = make; | ||
{ | ||
"name": "chan", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "A go style channel implementation that works nicely with co", | ||
@@ -21,2 +21,11 @@ "main": "chan.js", | ||
"author": "Brent Burgoyne", | ||
"contributors": [ | ||
{ | ||
"name": "Brent Burgoyne" | ||
}, | ||
{ | ||
"name": "TJ Holowaychuk", | ||
"email": "tj@vision-media.ca" | ||
} | ||
], | ||
"license": "MIT", | ||
@@ -29,4 +38,7 @@ "bugs": { | ||
"mocha": "~1.14.0", | ||
"expect.js": "~0.2.0" | ||
"expect.js": "~0.2.0", | ||
"co": "~2.3.0", | ||
"superagent": "~0.15.7", | ||
"co-wait": "0.0.0" | ||
} | ||
} |
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
12653
12
234
5
2
1