|  | 
|  | 1 | +'use strict'; | 
|  | 2 | +const common = require('../common'); | 
|  | 3 | +const assert = require('assert'); | 
|  | 4 | +const Readable = require('stream').Readable; | 
|  | 5 | + | 
|  | 6 | +const readable = new Readable({ | 
|  | 7 | +  read: () => {} | 
|  | 8 | +}); | 
|  | 9 | + | 
|  | 10 | +// Initialized to false. | 
|  | 11 | +assert.strictEqual(readable._readableState.needReadable, false); | 
|  | 12 | + | 
|  | 13 | +readable.on('readable', common.mustCall(() => { | 
|  | 14 | +  // When the readable event fires, needReadable is reset. | 
|  | 15 | +  assert.strictEqual(readable._readableState.needReadable, false); | 
|  | 16 | +  readable.read(); | 
|  | 17 | +})); | 
|  | 18 | + | 
|  | 19 | +// If a readable listener is attached, then a readable event is needed. | 
|  | 20 | +assert.strictEqual(readable._readableState.needReadable, true); | 
|  | 21 | + | 
|  | 22 | +readable.push('foo'); | 
|  | 23 | +readable.push(null); | 
|  | 24 | + | 
|  | 25 | +readable.on('end', common.mustCall(() => { | 
|  | 26 | +  // No need to emit readable anymore when the stream ends. | 
|  | 27 | +  assert.strictEqual(readable._readableState.needReadable, false); | 
|  | 28 | +})); | 
|  | 29 | + | 
|  | 30 | +const asyncReadable = new Readable({ | 
|  | 31 | +  read: () => {} | 
|  | 32 | +}); | 
|  | 33 | + | 
|  | 34 | +asyncReadable.on('readable', common.mustCall(() => { | 
|  | 35 | +  if (asyncReadable.read() !== null) { | 
|  | 36 | +    // After each read(), the buffer is empty. | 
|  | 37 | +    // If the stream doesn't end now, | 
|  | 38 | +    // then we need to notify the reader on future changes. | 
|  | 39 | +    assert.strictEqual(asyncReadable._readableState.needReadable, true); | 
|  | 40 | +  } | 
|  | 41 | +}, 3)); | 
|  | 42 | + | 
|  | 43 | +process.nextTick(common.mustCall(() => { | 
|  | 44 | +  asyncReadable.push('foooo'); | 
|  | 45 | +})); | 
|  | 46 | +process.nextTick(common.mustCall(() => { | 
|  | 47 | +  asyncReadable.push('bar'); | 
|  | 48 | +})); | 
|  | 49 | +process.nextTick(common.mustCall(() => { | 
|  | 50 | +  asyncReadable.push(null); | 
|  | 51 | +})); | 
|  | 52 | + | 
|  | 53 | +const flowing = new Readable({ | 
|  | 54 | +  read: () => {} | 
|  | 55 | +}); | 
|  | 56 | + | 
|  | 57 | +// Notice this must be above the on('data') call. | 
|  | 58 | +flowing.push('foooo'); | 
|  | 59 | +flowing.push('bar'); | 
|  | 60 | +flowing.push('quo'); | 
|  | 61 | +process.nextTick(common.mustCall(() => { | 
|  | 62 | +  flowing.push(null); | 
|  | 63 | +})); | 
|  | 64 | + | 
|  | 65 | +// When the buffer already has enough data, and the stream is | 
|  | 66 | +// in flowing mode, there is no need for the readable event. | 
|  | 67 | +flowing.on('data', common.mustCall(function(data) { | 
|  | 68 | +  assert.strictEqual(flowing._readableState.needReadable, false); | 
|  | 69 | +}, 3)); | 
|  | 70 | + | 
|  | 71 | +const slowProducer = new Readable({ | 
|  | 72 | +  read: () => {} | 
|  | 73 | +}); | 
|  | 74 | + | 
|  | 75 | +slowProducer.on('readable', common.mustCall(() => { | 
|  | 76 | +  if (slowProducer.read(8) === null) { | 
|  | 77 | +    // The buffer doesn't have enough data, and the stream is not ened, | 
|  | 78 | +    // we need to notify the reader when data arrives. | 
|  | 79 | +    assert.strictEqual(slowProducer._readableState.needReadable, true); | 
|  | 80 | +  } else { | 
|  | 81 | +    assert.strictEqual(slowProducer._readableState.needReadable, false); | 
|  | 82 | +  } | 
|  | 83 | +}, 4)); | 
|  | 84 | + | 
|  | 85 | +process.nextTick(common.mustCall(() => { | 
|  | 86 | +  slowProducer.push('foo'); | 
|  | 87 | +})); | 
|  | 88 | +process.nextTick(common.mustCall(() => { | 
|  | 89 | +  slowProducer.push('foo'); | 
|  | 90 | +})); | 
|  | 91 | +process.nextTick(common.mustCall(() => { | 
|  | 92 | +  slowProducer.push('foo'); | 
|  | 93 | +})); | 
|  | 94 | +process.nextTick(common.mustCall(() => { | 
|  | 95 | +  slowProducer.push(null); | 
|  | 96 | +})); | 
0 commit comments