Simple to understand node stream

Almost all Node.js applications, no matter how simple, use streams in some manner. 

Opening the first scare yourself. Figure painting, analysis of code to deepen their understanding.

Simple to understand node stream

  • The basic concept of stream
  • Writable - stream data writable
  • Readable - stream data can be read
  • Application of other modules in the node of stream
  • to sum up

 1.stream basic concepts

1.1 What is a stream

1. When writing code, we should have some connection methods like water, like connection program together - when we need to get some data, you can go to achieve their goals through other part of the "twist." It should be IO is the way it should. . Doug McIlroy October 11, 1964

In conjunction with the node

stream like an abstract model (a bit like a pipe), an orderly transfer of data (a bit like water), it is needed to take points by unscrewing tube can also be controlled size.

Node.js There are four basic types stream:
  • Writable - stream (e.g. fs.createWriteStream ()) data can be written.
  • Readable - can read the data stream (e.g. fs.createReadStream ()).
  • Duplex - a readable and writeable stream (e.g. net.Socket).
  • Transform - in reading and writing process can be modified or converted data stream Duplex (e.g. zlib.createDeflate ()).

 1.Readable- stream data can be read

1.1 Readable brief description of the stream data can be read

Readable stream is an abstraction of data sources. Like Water for water as we passed consumer use.

There are flow-readable two modes : a flow mode (Flowing) or pause mode (paused)

  • Flowing flow mode, data is automatically read from the underlying system, and is provided to the application by the '' data 'event EventEmitter interface as soon as possible.
  • Pause mode paused, the data must be read display data by calling stream.read ().

For example 1.2Readable data stream can be read

for example:

Readable the require {} = const ( 'Stream' ); 
class myReadable the extends Readable { 
  constructor (Options, Sources) { 
    Super (Options) 
    the this .sources = Sources
     the this .POS = 0 
  } 
  // inherited class must implement Readable _read () private method, the method is invoked inside Readable class 
  // when the _read () is called, if the resource is read from the data, it is necessary to start using this.push (dataChunk) to push data into the read queue. 
  // the _read () should continue to read data from and push data resources until readable.push () returns false 
// (temporarily understand readable.push (), push (null) returns false)
the _read () { IF ( the this .POS < the this .sources.length) { the this .push ( the this .sources [the this .POS]) the this .POS ++ } the else { the this .push ( null ) } } } the let rs = new new myReadable ({}, "I am a Luo cloth, I was somewhere to water resources" ) the let waterCup = '' // when binding to the listeners 'data' event, converted into a flow stream mode. // When the stream of data block transfer to the consumer triggers. rs.on ( 'Data', (the chunk) => { the console.log (the chunk); // the chunk is a Buffer waterCup + = the chunk }) rs.on ( 'End', () => { the console.log ( 'read complete consumption' ); Console. })

Open from debugging code above:

Probably a bit of paint flow chart patterns flowing :( this figure really does not look good, it is recommended to see the back of that)

Once you start listening data method, internal Readable will call the read method to trigger a read stream operation,

the _read () function which is a synchronous operation will first push data storage, and then removed from the variable this.buffer this.buffer variable, emit ( 'data', chunk) consumed

the _read () function which is asynchronous push, once an asynchronous operation is called push method, and the data, this time directly emit ( 'data', chunk) consumed.

Simplification of the pattern:

 

Guess you like

Origin www.cnblogs.com/luoxiaoer/p/11846386.html