From 4270e7f0f7169852c42adf84a8f4c0bc6f90634e Mon Sep 17 00:00:00 2001 From: Tom Gallacher Date: Fri, 30 Oct 2015 06:59:21 -0400 Subject: [PATCH] doc: Updated streams simplified constructor API The examples for implementing the simplified constructor API was missing some details on its correct usages. PR-URL: https://github.com/nodejs/node/pull/3602 Reviewed-By: James M Snell Reviewed-By: Chris Dickinson --- doc/api/stream.markdown | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/doc/api/stream.markdown b/doc/api/stream.markdown index c4d3f79ce1d88d..90289b7cf7400c 100644 --- a/doc/api/stream.markdown +++ b/doc/api/stream.markdown @@ -1368,9 +1368,16 @@ Examples: var duplex = new stream.Duplex({ read: function(n) { // sets this._read under the hood + + // push data onto the read queue, passing null + // will signal the end of the stream (EOF) + this.push(chunk); }, write: function(chunk, encoding, next) { // sets this._write under the hood + + // An optional error can be passed as the first argument + next() } }); @@ -1379,9 +1386,16 @@ var duplex = new stream.Duplex({ var duplex = new stream.Duplex({ read: function(n) { // sets this._read under the hood + + // push data onto the read queue, passing null + // will signal the end of the stream (EOF) + this.push(chunk); }, writev: function(chunks, next) { // sets this._writev under the hood + + // An optional error can be passed as the first argument + next() } }); ``` @@ -1391,6 +1405,10 @@ var duplex = new stream.Duplex({ var readable = new stream.Readable({ read: function(n) { // sets this._read under the hood + + // push data onto the read queue, passing null + // will signal the end of the stream (EOF) + this.push(chunk); } }); ``` @@ -1400,9 +1418,20 @@ var readable = new stream.Readable({ var transform = new stream.Transform({ transform: function(chunk, encoding, next) { // sets this._transform under the hood + + // generate output as many times as needed + // this.push(chunk); + + // call when the current chunk is consumed + next(); }, flush: function(done) { // sets this._flush under the hood + + // generate output as many times as needed + // this.push(chunk); + + done(); } }); ``` @@ -1412,6 +1441,9 @@ var transform = new stream.Transform({ var writable = new stream.Writable({ write: function(chunk, encoding, next) { // sets this._write under the hood + + // An optional error can be passed as the first argument + next() } }); @@ -1420,6 +1452,9 @@ var writable = new stream.Writable({ var writable = new stream.Writable({ writev: function(chunks, next) { // sets this._writev under the hood + + // An optional error can be passed as the first argument + next() } }); ```