• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const Readable = require('stream').Readable;
5
6const readable = new Readable({
7  read: () => {}
8});
9
10// Initialized to false.
11assert.strictEqual(readable._readableState.emittedReadable, false);
12
13const expected = [Buffer.from('foobar'), Buffer.from('quo'), null];
14readable.on('readable', common.mustCall(() => {
15  // emittedReadable should be true when the readable event is emitted
16  assert.strictEqual(readable._readableState.emittedReadable, true);
17  assert.deepStrictEqual(readable.read(), expected.shift());
18  // emittedReadable is reset to false during read()
19  assert.strictEqual(readable._readableState.emittedReadable, false);
20}, 3));
21
22// When the first readable listener is just attached,
23// emittedReadable should be false
24assert.strictEqual(readable._readableState.emittedReadable, false);
25
26// These trigger a single 'readable', as things are batched up
27process.nextTick(common.mustCall(() => {
28  readable.push('foo');
29}));
30process.nextTick(common.mustCall(() => {
31  readable.push('bar');
32}));
33
34// These triggers two readable events
35setImmediate(common.mustCall(() => {
36  readable.push('quo');
37  process.nextTick(common.mustCall(() => {
38    readable.push(null);
39  }));
40}));
41
42const noRead = new Readable({
43  read: () => {}
44});
45
46noRead.on('readable', common.mustCall(() => {
47  // emittedReadable should be true when the readable event is emitted
48  assert.strictEqual(noRead._readableState.emittedReadable, true);
49  noRead.read(0);
50  // emittedReadable is not reset during read(0)
51  assert.strictEqual(noRead._readableState.emittedReadable, true);
52}));
53
54noRead.push('foo');
55noRead.push(null);
56
57const flowing = new Readable({
58  read: () => {}
59});
60
61flowing.on('data', common.mustCall(() => {
62  // When in flowing mode, emittedReadable is always false.
63  assert.strictEqual(flowing._readableState.emittedReadable, false);
64  flowing.read();
65  assert.strictEqual(flowing._readableState.emittedReadable, false);
66}, 3));
67
68flowing.push('foooo');
69flowing.push('bar');
70flowing.push('quo');
71process.nextTick(common.mustCall(() => {
72  flowing.push(null);
73}));
74