You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.6 KiB

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