Skip to content

Commit

Permalink
toAsyncIterable: Remove unnecessary EOF check (#3914)
Browse files Browse the repository at this point in the history
In #2335 a conditional was added to make sure
toAsyncIterator didn't skip chunks because the reader returned data and
EOF in a single call, fixing #2330.

Later, in #2591, the `Reader` interface changed to
`Promise<number | EOF>`. Since the reader no longer returns data and EOF
in a single call, this conditional is not necessary. We can just return
`{ done: true }` when we get `EOF`.

Co-authored-by: Arun Srinivasan <[email protected]>

Co-authored-by: Arun Srinivasan <[email protected]>
  • Loading branch information
braddunbar and satchmorun authored Feb 7, 2020
1 parent 724e39f commit d7edf39
Showing 1 changed file with 0 additions and 14 deletions.
14 changes: 0 additions & 14 deletions cli/js/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,14 @@ export async function copy(dst: Writer, src: Reader): Promise<number> {
*/
export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array> {
const b = new Uint8Array(1024);
// Keep track if end-of-file has been reached, then
// signal that iterator is done during subsequent next()
// call. This is required because `r` can return a `number | EOF`
// with data read and EOF reached. But if iterator returns
// `done` then `value` is discarded.
//
// See https://github.com/denoland/deno/issues/2330 for reference.
let sawEof = false;

return {
[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
return this;
},

async next(): Promise<IteratorResult<Uint8Array>> {
if (sawEof) {
return { value: new Uint8Array(), done: true };
}

const result = await r.read(b);
if (result === EOF) {
sawEof = true;
return { value: new Uint8Array(), done: true };
}

Expand Down

0 comments on commit d7edf39

Please sign in to comment.