In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "nodejs stream base class how to achieve", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "nodejs stream base class how to achieve" it!
Stream is an abstraction of data production and consumption. Today, let's analyze the implementation of stream base class.
Const EE = require ('events')
Const util = require ('util')
/ / Base class of the stream
Function Stream () {
EE.call (this)
}
/ / inherit the ability of event subscription distribution
Util.inherits (Stream, EE)
The base class of the stream provides only one function, pipe. Used to realize pipelization. This method has a lot of code, so let's talk about it separately.
1 handle data event function ondata (chunk) {
/ / the source stream has data arriving, and the destination stream is writable
If (dest.writable) {
/ / if the destination stream is overloaded and the source stream implements the pause method, pause the read operation of the readable stream and wait for the destination stream to trigger the drain event
If (false = dest.write (chunk) & & source.pause) {
Source.pause ()
}
}
}
/ / listen for the data event. When the readable stream has data, the data event will be triggered.
Source.on ('data', ondata)
Function ondrain () {
/ / the destination stream is writable and readable, switching to automatic read mode
If (source.readable & & source.resume) {
Source.resume ()
}
}
/ / listens to the drain event, which will be triggered when the destination stream can consume data
Dest.on ('drain', ondrain)
This is where flow control is implemented when pipelining, mainly using write return values and drain events.
Stream closes / ends processing / / destination stream is not standard output or standard error, and end is not equal to false
If (! dest._isStdio & & (! options | | options.end! = = false)) {
/ / the source stream has no data to read. Execute an end callback to tell the destination stream that there is no data to read.
Source.on ('end', onend)
/ / Source stream is closed, execute close callback
Source.on ('close', onclose)
}
/ / two functions will only be executed once and only one
Var didOnEnd = false
Function onend () {
If (didOnEnd) return
DidOnEnd = true
/ / execute the end function of the destination stream, indicating that the data has been written.
Dest.end ()
}
Function onclose () {
If (didOnEnd) return
DidOnEnd = true
/ / destroy the destination stream
If (typeof dest.destroy = 'function') dest.destroy ()
}
Here is the logic for notifying the destination flow after the source stream ends and closes.
Error handling and event cleanup / / remove all the event listeners that were added.
Function cleanup () {
Source.removeListener ('data', ondata)
Dest.removeListener ('drain', ondrain)
Source.removeListener ('end', onend)
Source.removeListener ('close', onclose)
Source.removeListener ('error', onerror)
Dest.removeListener ('error', onerror)
Source.removeListener ('end', cleanup)
Source.removeListener ('close', cleanup)
Dest.removeListener ('close', cleanup)
}
Function onerror (er) {
/ / error. Clear the registered events, including the onerror function that is being executed
Cleanup ()
/ / if the user does not listen to the error event of the stream, an error is thrown, so our business code needs to listen to the error event.
If (EE.listenerCount (this, 'error') = 0) {
Throw er; / / Unhandled stream error in pipe.
}
}
/ / listen to the error event of the stream
Source.on ('error', onerror)
Dest.on ('error', onerror)
/ / clear registered events when the source stream is closed or there is no data to read
Source.on ('end', cleanup)
Source.on ('close', cleanup)
/ / the destination stream closes and clears his registered events
Dest.on ('close', cleanup)
This mainly deals with the error event and the event that clears the subscription when the stream closes / ends / errors. This is all the logic of the stream base class.
Thank you for your reading, the above is the content of "how to achieve nodejs stream base class". After the study of this article, I believe you have a deeper understanding of how to achieve nodejs stream base class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.