我必须在可读事件处理程序中重复调用read.read()吗? [英] Must I repeatedly call readable.read() within a readable event handler?

查看:76
本文介绍了我必须在可读事件处理程序中重复调用read.read()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我创建了一个名为Parser的转换流,可以像普通流一样将其写入,但可以将其作为对象流读取.我正在为使用此转换流的代码使用readable事件:

Suppose I have created a transform stream called Parser which can be written to like a normal stream but is read from as an object stream. I am using the readable event for the code that uses this transform stream:

var parser = new Parser();
parser.on('readable', function () {
    var data = parser.read();
    console.log(data);
});

在此事件处理程序中,我是否必须重复调用parser.read()?或者,对于从我的转换流中推送的每个对象,readable都会自行触发吗?

In this event handler, must I repeatedly call parser.read()? Or, will readable fire on its own for every single object being pushed from my transform stream?

推荐答案

根据节点文档,一旦内部缓冲区耗尽,当更多数据可用时,可读事件将再次触发." 因此,如果您仅调用一次read()并且仍然有更多数据要读取,您将必须记得稍后再read().

According to the node docs, "Once the internal buffer is drained, a readable event will fire again when more data is available," so if you call read() just once and there's still more data to be read, you'll have to remember to read() some more later on.

您可以在while循环中(在可读"事件处理程序内部)调用read(),直到它返回null,然后等待下一个可读"事件.

You could call read() in a while loop (inside your 'readable' event handler) until it returns null, then wait for the next 'readable' event.

这篇关于我必须在可读事件处理程序中重复调用read.read()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆