Comparing version 0.2.0 to 0.3.0
@@ -84,4 +84,6 @@ var slice = [].slice | ||
Channel.prototype.end = function* () { | ||
var values = [] | ||
while (this.length) | ||
yield* this.shift() | ||
values.push(yield* this.shift()) | ||
return values | ||
} |
{ | ||
"name": "archan", | ||
"description": "Array-like generator-based channels", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jonathan Ong", |
@@ -25,2 +25,4 @@ # Archan [![Build Status](https://travis-ci.org/cojs/archan.png)](https://travis-ci.org/cojs/archan) | ||
} | ||
console.log('all done!') | ||
process.exit() | ||
@@ -38,3 +40,3 @@ })() | ||
Returns a new callback you can pass to asynchronous functions. | ||
Returns a new callback you pass to asynchronous functions. | ||
@@ -48,2 +50,17 @@ ```js | ||
Note that each callback returned from `ch.push()` is single use only. | ||
If you use a callback more or less than once, | ||
things are going to go badly with your channel. | ||
For example, for emitters, you would want to do something like this: | ||
```js | ||
var ch = archan() | ||
var stream = fs.createWriteStream('file.txt') | ||
var cb = ch.push() | ||
stream.once('error', cb) | ||
stream.once('finish', cb) | ||
yield* ch.shift() | ||
``` | ||
### ch.push(val...) | ||
@@ -53,2 +70,3 @@ | ||
If you push any values, a callback will not be returned. | ||
Multiple arguments will be combined into a single array. | ||
@@ -115,11 +133,12 @@ ```js | ||
### yield* ch.end() | ||
### var values = yield* ch.end() | ||
Wait for all the values to be `yield`ed. | ||
Useful if you don't need the resulting values and are using `archan` just for control flow. | ||
Basically does this: | ||
Return all the pending values at once. | ||
At this point, the channel will be empty and you will be able to reuse it. | ||
This is a shortcut for: | ||
```js | ||
var values = [] | ||
while (ch.length) { | ||
yield* ch.shift() | ||
values.push(yield* ch.shift()) | ||
} | ||
@@ -126,0 +145,0 @@ ``` |
6947
74
164