node.js - Node.js 中的 Streams3 是什么,它与 Streams2 有何不同?

标签 node.js stream

我经常听说 Streams2 和 old-streams,但 Streams3 是什么? It get mentioned in this talk by Thorsten Lorenz .

我在哪里可以了解它,Streams2 和 Streams3 之间有什么区别。

在 Google 上搜索,我还看到 Changelog of Node 0.11.5 中提到了它,

stream: Simplify flowing, passive data listening (streams3) (isaacs)

最佳答案

我打算试一试,但我可能弄错了。从来没有写过 Streams1(旧流)或 Streams2,我可能不是 self 回答这个问题的合适人选,但它就是这样。似乎 Streams1 API 在某种程度上仍然存在。在 Streams2 中,流有两种模式流动(遗留)和非流动。简而言之,支持流动模式的垫片正在消失。这是 message that lead to the patch now called called Streams3 ,

Same API as streams2, but remove the confusing modality of flowing/old mode switch.

  1. Every time read() is called, and returns some data, a data event fires.
  2. resume() will make it call read() repeatedly. Otherwise, no change.
  3. pause() will make it stop calling read() repeatedly.
  4. pipe(dest) and on('data', fn) will automatically call resume().
  5. No switches into old-mode. There's only flowing, and paused. Streams start out paused.

不幸的是,要理解任何很好地定义 Streams3 的描述,您需要首先了解 Streams1 和遗留流

背景故事

首先,让我们看一下 Node v0.10.25 文档对这两种模式的说法,

Readable streams have two "modes": a flowing mode and a non-flowing mode. When in flowing mode, data is read from the underlying system and provided to your program as fast as possible. In non-flowing mode, you must explicitly call stream.read() to get chunks of data out.Node v0.10.25 Docs

Isaac Z. Schlueter said in November slides I dug up :

streams2

  • "suck streams"
  • Instead of 'data' events spewing, call read() to pull data from source
  • Solves all problems (that we know of)

看起来好像在streams1 中,您会创建一个对象并调用.on('data', cb) 到该对象。这会将事件设置为触发,然后您将受到流的摆布。在 Streams2 内部,流具有缓冲区,您可以显式地从这些流中请求数据(使用 `.read)。 Isaac 继续指定 Streams2 中的向后兼容如何工作以保持 Streams1(旧流)模块正常运行

old-mode streams1 shim

  • New streams can switch into old-mode, where they spew 'data'
  • If you add a 'data' event handler, or call pause() or resume(), then switch
  • Making minimal changes to existing tests to keep us honest

所以在 Streams2 中,对 .pause().resume() 的调用会触发 shim。而且,它应该,对吧?在 Streams2 中,您可以控制何时执行 .read(),并且您不会捕捉到被扔给您的东西。这触发了独立于 Streams2 的传统模式。

让我们以 Isaac 的幻灯片为例,

createServer(function(q,s) {
  // ADVISORY only!
  q.pause()
  session(q, function(ses) {
    q.on('data', handler)
    q.resume()
  })
})
  • 在 Streams1 中,q 会立即开始读取和发送(可能会丢失数据),直到对 q.pause 的调用建议 q停止拉入数据,但不会发出事件以清除已读取的内容。
  • 在 Streams2 中,q 开始暂停,直到调用 .pause() 表示模拟旧模式。
  • 在 Streams3 中,q 以暂停状态开始,从未从文件句柄中读取,从而使 q.pause() 成为 noop,然后调用 q.on('data', cb) 将调用 q.resume 直到缓冲区中没有更多数据。然后再次调用 q.resume 做同样的事情。

关于node.js - Node.js 中的 Streams3 是什么,它与 Streams2 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21538812/

相关文章:

node.js - 从 salesforce.com 访问本地 nodejs 服务器

flash - 在Flash SWF中播放YouTube视频

c# - XmlSerializer,生成 XML 文档时出错

javascript - Swig 模板,测试条件

node.js - nock 库 - 如何匹配任何 url

node.js - 在运行 node.js 脚本的上下文中使用 upstart 脚本或永远脚本的优点和缺点是什么?

node.js - gulp 中的 es.merge 用法 : Object #<Stream> has no method 'end'

delphi - 逐行读取流

java - 如何正确缓冲 Java 输入/输出/文件流?

c# - 使用 Stream.BeginRead 的顺序异步读取