-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make readable stream's ready promise fulfill-only
Part of #245; see discussions in #243 (comment). Consumers should always either check the state property, or count on the fact that read() will throw when used on an errored stream.
- Loading branch information
Showing
6 changed files
with
70 additions
and
94 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
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
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
24 changes: 12 additions & 12 deletions
24
reference-implementation/test/utils/readable-stream-to-array.js
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
export default function readableStreamToArray(readable) { | ||
return new Promise((resolve, reject) => { | ||
var chunks = []; | ||
var chunks = []; | ||
|
||
readable.closed.then(() => resolve(chunks), reject); | ||
pump(); | ||
pump(); | ||
return readable.closed.then(() => chunks); | ||
|
||
function pump() { | ||
while (readable.state === 'readable') { | ||
chunks.push(readable.read()); | ||
} | ||
function pump() { | ||
while (readable.state === "readable") { | ||
chunks.push(readable.read()); | ||
} | ||
|
||
if (readable.state === 'waiting') { | ||
readable.ready.then(pump); | ||
} | ||
if (readable.state === "waiting") { | ||
readable.ready.then(pump); | ||
} | ||
}); | ||
|
||
// Otherwise the stream is "closed" or "errored", which will be handled above. | ||
} | ||
} |