三种状态
¥Three states
Readable
流的 "两种模式" 操作是对 Readable
流实现中发生的更复杂的内部状态管理的简化抽象。
¥The "two modes" of operation for a Readable
stream are a simplified
abstraction for the more complicated internal state management that is happening
within the Readable
stream implementation.
具体来说,在任何给定的时间点,每个 Readable
都处于三种可能的状态之一:
¥Specifically, at any given point in time, every Readable
is in one of three
possible states:
-
readable.readableFlowing === null
-
readable.readableFlowing === false
-
readable.readableFlowing === true
当 readable.readableFlowing
为 null
时,则不提供消费流数据的机制。因此,流不会生成数据。在此状态下,为 'data'
事件绑定监听器、调用 readable.pipe()
方法、或调用 readable.resume()
方法会将 readable.readableFlowing
切换到 true
,从而使 Readable
在生成数据时开始主动触发事件。
¥When readable.readableFlowing
is null
, no mechanism for consuming the
stream's data is provided. Therefore, the stream will not generate data.
While in this state, attaching a listener for the 'data'
event, calling the
readable.pipe()
method, or calling the readable.resume()
method will switch
readable.readableFlowing
to true
, causing the Readable
to begin actively
emitting events as data is generated.
调用 readable.pause()
、readable.unpipe()
或接收背压将导致 readable.readableFlowing
设置为 false
,暂时停止事件的流动但不会停止数据的生成。在此状态下,为 'data'
事件绑定监听器不会将 readable.readableFlowing
切换到 true
。
¥Calling readable.pause()
, readable.unpipe()
, or receiving backpressure
will cause the readable.readableFlowing
to be set as false
,
temporarily halting the flowing of events but not halting the generation of
data. While in this state, attaching a listener for the 'data'
event
will not switch readable.readableFlowing
to true
.
const { PassThrough, Writable } = require('node:stream');
const pass = new PassThrough();
const writable = new Writable();
pass.pipe(writable);
pass.unpipe(writable);
// readableFlowing is now false.
pass.on('data', (chunk) => { console.log(chunk.toString()); });
// readableFlowing is still false.
pass.write('ok'); // Will not emit 'data'.
pass.resume(); // Must be called to make stream emit 'data'.
// readableFlowing is now true.
虽然 readable.readableFlowing
是 false
,但数据可能会在流的内部缓冲区中累积。
¥While readable.readableFlowing
is false
, data may be accumulating
within the stream's internal buffer.