diff options
author | Sam Newman <newmansam@outlook.com> | 2015-02-03 01:12:41 +0000 |
---|---|---|
committer | Chris Dickinson <christopher.s.dickinson@gmail.com> | 2015-02-04 20:19:39 -0800 |
commit | 50daee7243a3f987e1a28d93c43f913471d6885a (patch) | |
tree | 9bb02244f23a54732cf0e7ccdf2f5eb321a042d5 /doc | |
parent | e0730eeaa5231841a7eba080c8170e41278c3c52 (diff) | |
download | node-new-50daee7243a3f987e1a28d93c43f913471d6885a.tar.gz |
stream: simpler stream constructon
Adds simplified constructor pattern, allowing users
to provide "read", "write", "transform", "flush", and
"writev" functions as stream options in lieu of subclassing.
Semver: minor
PR-URL: https://github.com/iojs/io.js/pull/697
Fixes: https://github.com/iojs/readable-stream/issues/102
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/stream.markdown | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/doc/api/stream.markdown b/doc/api/stream.markdown index 943718660c..bbd2af0d7a 100644 --- a/doc/api/stream.markdown +++ b/doc/api/stream.markdown @@ -718,7 +718,7 @@ of stream class you are writing: <p>[Writable](#stream_class_stream_writable_1)</p> </td> <td> - <p><code>[_write][]</code></p> + <p><code>[_write][]</code>, <code>_writev</code></p> </td> </tr> <tr> @@ -729,7 +729,7 @@ of stream class you are writing: <p>[Duplex](#stream_class_stream_duplex_1)</p> </td> <td> - <p><code>[_read][]</code>, <code>[_write][]</code></p> + <p><code>[_read][]</code>, <code>[_write][]</code>, <code>_writev</code></p> </td> </tr> <tr> @@ -1315,6 +1315,77 @@ for examples and testing, but there are occasionally use cases where it can come in handy as a building block for novel sorts of streams. +## Simplified Constructor API + +<!--type=misc--> + +In simple cases there is now the added benefit of being able to construct a stream without inheritance. + +This can be done by passing the appropriate methods as constructor options: + +Examples: + +### Readable +```javascript +var readable = new stream.Readable({ + read: function(n) { + // sets this._read under the hood + } +}); +``` + +### Writable +```javascript +var writable = new stream.Writable({ + write: function(chunk, encoding, next) { + // sets this._write under the hood + } +}); + +// or + +var writable = new stream.Writable({ + writev: function(chunks, next) { + // sets this._writev under the hood + } +}); +``` + +### Duplex +```javascript +var duplex = new stream.Duplex({ + read: function(n) { + // sets this._read under the hood + }, + write: function(chunk, encoding, next) { + // sets this._write under the hood + } +}); + +// or + +var duplex = new stream.Duplex({ + read: function(n) { + // sets this._read under the hood + }, + writev: function(chunks, next) { + // sets this._writev under the hood + } +}); +``` + +### Transform +```javascript +var transform = new stream.Transform({ + transform: function(chunk, encoding, next) { + // sets this._transform under the hood + }, + flush: function(done) { + // sets this._flush under the hood + } +}); +``` + ## Streams: Under the Hood <!--type=misc--> |