@@ -3,64 +3,166 @@ const common = require('../common');
3
3
const assert = require ( 'assert' ) ;
4
4
const Readable = require ( 'stream' ) . Readable ;
5
5
6
- const readable = new Readable ( {
7
- read ( size ) { }
8
- } ) ;
6
+ {
7
+ const readable = new Readable ( {
8
+ read ( size ) { }
9
+ } ) ;
9
10
10
- const state = readable . _readableState ;
11
+ const state = readable . _readableState ;
11
12
12
- // Starting off with false initially.
13
- assert . strictEqual ( state . reading , false ) ;
14
- assert . strictEqual ( state . readingMore , false ) ;
13
+ // Starting off with false initially.
14
+ assert . strictEqual ( state . reading , false ) ;
15
+ assert . strictEqual ( state . readingMore , false ) ;
15
16
16
- readable . on ( 'data' , common . mustCall ( ( data ) => {
17
- // while in a flowing state, should try to read more.
18
- if ( readable . readableFlowing )
17
+ readable . on ( 'data' , common . mustCall ( ( data ) => {
18
+ // while in a flowing state with a 'readable' listener
19
+ // we should not be reading more
20
+ if ( readable . readableFlowing )
21
+ assert . strictEqual ( state . readingMore , true ) ;
22
+
23
+ // reading as long as we've not ended
24
+ assert . strictEqual ( state . reading , ! state . ended ) ;
25
+ } , 2 ) ) ;
26
+
27
+ function onStreamEnd ( ) {
28
+ // End of stream; state.reading is false
29
+ // And so should be readingMore.
30
+ assert . strictEqual ( state . readingMore , false ) ;
31
+ assert . strictEqual ( state . reading , false ) ;
32
+ }
33
+
34
+ readable . on ( 'readable' , common . mustCall ( ( ) => {
35
+ // 'readable' always gets called before 'end'
36
+ // since 'end' hasn't been emitted, more data could be incoming
19
37
assert . strictEqual ( state . readingMore , true ) ;
20
38
21
- // reading as long as we've not ended
22
- assert . strictEqual ( state . reading , ! state . ended ) ;
23
- } , 2 ) ) ;
39
+ // if the stream has ended, we shouldn't be reading
40
+ assert . strictEqual ( state . ended , ! state . reading ) ;
41
+
42
+ const data = readable . read ( ) ;
43
+ if ( data === null ) // reached end of stream
44
+ process . nextTick ( common . mustCall ( onStreamEnd , 1 ) ) ;
45
+ } , 2 ) ) ;
46
+
47
+ readable . on ( 'end' , common . mustCall ( onStreamEnd ) ) ;
48
+ readable . push ( 'pushed' ) ;
49
+
50
+ readable . read ( 6 ) ;
51
+
52
+ // reading
53
+ assert . strictEqual ( state . reading , true ) ;
54
+ assert . strictEqual ( state . readingMore , true ) ;
55
+
56
+ // add chunk to front
57
+ readable . unshift ( 'unshifted' ) ;
24
58
25
- function onStreamEnd ( ) {
26
- // End of stream; state.reading is false
27
- // And so should be readingMore.
59
+ // end
60
+ readable . push ( null ) ;
61
+ }
62
+
63
+ {
64
+ const readable = new Readable ( {
65
+ read ( size ) { }
66
+ } ) ;
67
+
68
+ const state = readable . _readableState ;
69
+
70
+ // Starting off with false initially.
71
+ assert . strictEqual ( state . reading , false ) ;
28
72
assert . strictEqual ( state . readingMore , false ) ;
73
+
74
+ readable . on ( 'data' , common . mustCall ( ( data ) => {
75
+ // while in a flowing state without a 'readable' listener
76
+ // we should be reading more
77
+ if ( readable . readableFlowing )
78
+ assert . strictEqual ( state . readingMore , true ) ;
79
+
80
+ // reading as long as we've not ended
81
+ assert . strictEqual ( state . reading , ! state . ended ) ;
82
+ } , 2 ) ) ;
83
+
84
+ function onStreamEnd ( ) {
85
+ // End of stream; state.reading is false
86
+ // And so should be readingMore.
87
+ assert . strictEqual ( state . readingMore , false ) ;
88
+ assert . strictEqual ( state . reading , false ) ;
89
+ }
90
+
91
+ readable . on ( 'end' , common . mustCall ( onStreamEnd ) ) ;
92
+ readable . push ( 'pushed' ) ;
93
+
94
+ // stop emitting 'data' events
95
+ assert . strictEqual ( state . flowing , true ) ;
96
+ readable . pause ( ) ;
97
+
98
+ // paused
99
+ assert . strictEqual ( state . reading , false ) ;
100
+ assert . strictEqual ( state . flowing , false ) ;
101
+
102
+ readable . resume ( ) ;
29
103
assert . strictEqual ( state . reading , false ) ;
104
+ assert . strictEqual ( state . flowing , true ) ;
105
+
106
+ // add chunk to front
107
+ readable . unshift ( 'unshifted' ) ;
108
+
109
+ // end
110
+ readable . push ( null ) ;
30
111
}
31
112
32
- readable . on ( 'readable' , common . mustCall ( ( ) => {
33
- // ' readable' always gets called before 'end'
34
- // since 'end' hasn't been emitted, more data could be incoming
35
- assert . strictEqual ( state . readingMore , true ) ;
113
+ {
114
+ const readable = new Readable ( {
115
+ read ( size ) { }
116
+ } ) ;
36
117
37
- // if the stream has ended, we shouldn't be reading
38
- assert . strictEqual ( state . ended , ! state . reading ) ;
118
+ const state = readable . _readableState ;
39
119
40
- const data = readable . read ( ) ;
41
- if ( data === null ) // reached end of stream
42
- process . nextTick ( common . mustCall ( onStreamEnd , 1 ) ) ;
43
- } , 2 ) ) ;
120
+ // Starting off with false initially.
121
+ assert . strictEqual ( state . reading , false ) ;
122
+ assert . strictEqual ( state . readingMore , false ) ;
123
+
124
+ const onReadable = common . mustNotCall ;
125
+
126
+ readable . on ( 'readable' , onReadable ) ;
44
127
45
- readable . on ( 'end' , common . mustCall ( onStreamEnd ) ) ;
128
+ readable . on ( 'data' , common . mustCall ( ( data ) => {
129
+ // reading as long as we've not ended
130
+ assert . strictEqual ( state . reading , ! state . ended ) ;
131
+ } , 2 ) ) ;
46
132
47
- readable . push ( 'pushed' ) ;
133
+ readable . removeListener ( 'readable' , onReadable ) ;
48
134
49
- // stop emitting 'data' events
50
- readable . pause ( ) ;
135
+ function onStreamEnd ( ) {
136
+ // End of stream; state.reading is false
137
+ // And so should be readingMore.
138
+ assert . strictEqual ( state . readingMore , false ) ;
139
+ assert . strictEqual ( state . reading , false ) ;
140
+ }
51
141
52
- // read() should only be called while operating in paused mode
53
- readable . read ( 6 ) ;
142
+ readable . on ( 'end' , common . mustCall ( onStreamEnd ) ) ;
143
+ readable . push ( 'pushed' ) ;
54
144
55
- // reading
56
- assert . strictEqual ( state . reading , true ) ;
57
- assert . strictEqual ( state . readingMore , true ) ;
145
+ // we are still not flowing, we will be resuming in the next tick
146
+ assert . strictEqual ( state . flowing , false ) ;
58
147
59
- // resume emitting 'data' events
60
- readable . resume ( ) ;
148
+ // wait for nextTick, so the readableListener flag resets
149
+ process . nextTick ( function ( ) {
150
+ readable . resume ( ) ;
61
151
62
- // add chunk to front
63
- readable . unshift ( 'unshifted' ) ;
152
+ // stop emitting 'data' events
153
+ assert . strictEqual ( state . flowing , true ) ;
154
+ readable . pause ( ) ;
64
155
65
- // end
66
- readable . push ( null ) ;
156
+ // paused
157
+ assert . strictEqual ( state . flowing , false ) ;
158
+
159
+ readable . resume ( ) ;
160
+ assert . strictEqual ( state . flowing , true ) ;
161
+
162
+ // add chunk to front
163
+ readable . unshift ( 'unshifted' ) ;
164
+
165
+ // end
166
+ readable . push ( null ) ;
167
+ } ) ;
168
+ }
0 commit comments