-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: postpone setting flowing for on('readable')
Now state.flowing will be set only after all of the 'readable' listeners are gone and if we have at least one 'data' listener. * on('data') will not flow (flowing === null and not false) if there are 'readable' listeners * pipe() will work regardless of 'readable' listeners * isPause reports only user .pause call (before setting 'data' listener when there is already 'readable' listener also set flowing to false) * resume always sets stream to flowing state
- Loading branch information
1 parent
6fa9528
commit c07abd7
Showing
5 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
// This test ensures that if we have both 'readable' and 'data' | ||
// listeners on Readable instance once all of the 'readable' listeners | ||
// are gone and there are still 'data' listeners stream will *not* | ||
// try to flow if it was explicitly paused. | ||
|
||
const { Readable } = require('stream'); | ||
|
||
const r = new Readable({ | ||
read: () => {}, | ||
}); | ||
|
||
const data = ['foo', 'bar', 'baz']; | ||
|
||
r.pause(); | ||
|
||
r.on('data', common.mustNotCall()); | ||
r.on('end', common.mustNotCall()); | ||
r.once('readable', common.mustCall()); | ||
|
||
for (const d of data) | ||
r.push(d); | ||
r.push(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
// This test ensures that if have 'readable' listener | ||
// on Readable instance it will not disrupt the pipe. | ||
|
||
const assert = require('assert'); | ||
const { Readable, Writable } = require('stream'); | ||
|
||
let receivedData = ''; | ||
const w = new Writable({ | ||
write: (chunk, env, callback) => { | ||
receivedData += chunk; | ||
callback(); | ||
}, | ||
}); | ||
|
||
const data = ['foo', 'bar', 'baz']; | ||
const r = new Readable({ | ||
read: () => {}, | ||
}); | ||
|
||
r.on('readable', common.mustCall()); | ||
|
||
r.pipe(w); | ||
r.push(data[0]); | ||
r.push(data[1]); | ||
r.push(data[2]); | ||
r.push(null); | ||
|
||
w.on('finish', common.mustCall(() => { | ||
assert.strictEqual(receivedData, data.join('')); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
// This test ensures that if we remove last 'readable' listener | ||
// on Readable instance that is piped it will not disrupt the pipe. | ||
|
||
const assert = require('assert'); | ||
const { Readable, Writable } = require('stream'); | ||
|
||
let receivedData = ''; | ||
const w = new Writable({ | ||
write: (chunk, env, callback) => { | ||
receivedData += chunk; | ||
callback(); | ||
}, | ||
}); | ||
|
||
const data = ['foo', 'bar', 'baz']; | ||
const r = new Readable({ | ||
read: () => {}, | ||
}); | ||
|
||
const listener = common.mustNotCall(); | ||
r.on('readable', listener); | ||
|
||
r.pipe(w); | ||
r.push(data[0]); | ||
|
||
r.removeListener('readable', listener); | ||
|
||
process.nextTick(() => { | ||
r.push(data[1]); | ||
r.push(data[2]); | ||
r.push(null); | ||
}); | ||
|
||
w.on('finish', common.mustCall(() => { | ||
assert.strictEqual(receivedData, data.join('')); | ||
})); |