[6a3a178] | 1 | #through
|
---|
| 2 |
|
---|
| 3 | [![build status](https://secure.travis-ci.org/dominictarr/through.png)](http://travis-ci.org/dominictarr/through)
|
---|
| 4 | [![testling badge](https://ci.testling.com/dominictarr/through.png)](https://ci.testling.com/dominictarr/through)
|
---|
| 5 |
|
---|
| 6 | Easy way to create a `Stream` that is both `readable` and `writable`.
|
---|
| 7 |
|
---|
| 8 | * Pass in optional `write` and `end` methods.
|
---|
| 9 | * `through` takes care of pause/resume logic if you use `this.queue(data)` instead of `this.emit('data', data)`.
|
---|
| 10 | * Use `this.pause()` and `this.resume()` to manage flow.
|
---|
| 11 | * Check `this.paused` to see current flow state. (`write` always returns `!this.paused`).
|
---|
| 12 |
|
---|
| 13 | This function is the basis for most of the synchronous streams in
|
---|
| 14 | [event-stream](http://github.com/dominictarr/event-stream).
|
---|
| 15 |
|
---|
| 16 | ``` js
|
---|
| 17 | var through = require('through')
|
---|
| 18 |
|
---|
| 19 | through(function write(data) {
|
---|
| 20 | this.queue(data) //data *must* not be null
|
---|
| 21 | },
|
---|
| 22 | function end () { //optional
|
---|
| 23 | this.queue(null)
|
---|
| 24 | })
|
---|
| 25 | ```
|
---|
| 26 |
|
---|
| 27 | Or, can also be used _without_ buffering on pause, use `this.emit('data', data)`,
|
---|
| 28 | and this.emit('end')
|
---|
| 29 |
|
---|
| 30 | ``` js
|
---|
| 31 | var through = require('through')
|
---|
| 32 |
|
---|
| 33 | through(function write(data) {
|
---|
| 34 | this.emit('data', data)
|
---|
| 35 | //this.pause()
|
---|
| 36 | },
|
---|
| 37 | function end () { //optional
|
---|
| 38 | this.emit('end')
|
---|
| 39 | })
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 | ## Extended Options
|
---|
| 43 |
|
---|
| 44 | You will probably not need these 99% of the time.
|
---|
| 45 |
|
---|
| 46 | ### autoDestroy=false
|
---|
| 47 |
|
---|
| 48 | By default, `through` emits close when the writable
|
---|
| 49 | and readable side of the stream has ended.
|
---|
| 50 | If that is not desired, set `autoDestroy=false`.
|
---|
| 51 |
|
---|
| 52 | ``` js
|
---|
| 53 | var through = require('through')
|
---|
| 54 |
|
---|
| 55 | //like this
|
---|
| 56 | var ts = through(write, end, {autoDestroy: false})
|
---|
| 57 | //or like this
|
---|
| 58 | var ts = through(write, end)
|
---|
| 59 | ts.autoDestroy = false
|
---|
| 60 | ```
|
---|
| 61 |
|
---|
| 62 | ## License
|
---|
| 63 |
|
---|
| 64 | MIT / Apache2
|
---|