node中的Readable - flowing/non-flowing mode
2020-12-13 05:49
                         标签:style   blog   http   color   使用   数据   2014   ar    大家都知道在node中 对于   而   可以看到当有数据时,只是将数据放入buffer中,而不是直接emit。 所以如果想控制stream的读取顺序,大小和时间,应该使用 而pipe时则会注册   link: http://villadora.me/2014/07/24/nodezhong-de-readable-flowingnon-flowing-mode/index.html node中的Readable - flowing/non-flowing mode,搜素材,soscw.com node中的Readable - flowing/non-flowing mode 标签:style   blog   http   color   使用   数据   2014   ar    原文地址:http://www.cnblogs.com/villadora/p/3885908.htmlReadable Stream有两种模式: flowing mode和non-flowing mode。flowing mode的Readable Stream, 我们是没法控制它何时去读数据读多少的,它会去尽快的去消耗data,并emit出来。1 // in lib/_stream_readable.js
2 if (state.flowing && state.length === 0 &&!state.sync) {  
3         stream.emit(‘data‘, chunk);
4         stream.read(0);
non-flowing mode的Readable Stream, 则不会主动的去读数据,需要自己显示的去调用read方法才能得到数据。1 // in lib/_stream_readable.js
2 // update the buffer info.
3 state.length += state.objectMode ? 1 : chunk.length;  
4 if (addToFront)  
5   state.buffer.unshift(chunk);
6 else  
7   state.buffer.push(chunk);
non-flowing mode,而当使用pipe有下游管道对数据进行处理时,这个时候数据的读取和处理应该交给下游管道处理,应该尽可能快的提供数据,所以要使用flowing mode。而这也是node会在attach ‘data‘事件之后,自动转变为flowing mode的原因,避免错误的使用或者忘记切换模式。data事件,所以使用了pipe也会转换为flowing mode。1 var src = this;  
2 ...
3 src.on(‘data‘, ondata);
文章标题:node中的Readable - flowing/non-flowing mode
文章链接:http://soscw.com/essay/31810.html