-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: async iterate readable streams,
iterate
function (#30)
- async iterator on all bluestream readable streams - `iterate(stream)` any readable stream BREAKING CHANGE: dropped the default export
- Loading branch information
Showing
13 changed files
with
1,038 additions
and
809 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ cache: | |
notifications: | ||
email: false | ||
node_js: | ||
- '10' | ||
- '9' | ||
- '8' | ||
after_success: | ||
|
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,46 @@ | ||
import { Readable } from 'stream' | ||
import { readAsync } from '.' | ||
|
||
if ((Symbol as any).asyncIterator === undefined) { | ||
((Symbol as any).asyncIterator) = Symbol.for('asyncIterator') | ||
} | ||
|
||
async function* iterateObjectMode (stream) { | ||
let data = true | ||
while (data) { | ||
data = await readAsync(stream, 1) | ||
if (data) { | ||
yield data[0] | ||
} | ||
} | ||
} | ||
|
||
async function* iterateBufferMode (stream) { | ||
let data = true | ||
while (data) { | ||
data = await readAsync(stream) | ||
if (data) { | ||
yield data | ||
} | ||
} | ||
} | ||
|
||
export function internalIterator (stream: Readable) { | ||
const readableState = (stream as any)._readableState | ||
const objectMode = readableState && readableState.objectMode | ||
if (objectMode) { | ||
return iterateObjectMode(stream) | ||
} | ||
return iterateBufferMode(stream) | ||
} | ||
|
||
export function iterate (stream: Readable) { | ||
if (stream[(Symbol as any).asyncIterator]) { | ||
return stream[(Symbol as any).asyncIterator]() | ||
} | ||
const objectMode = (stream as any)._readableState.objectMode | ||
if (objectMode) { | ||
return iterateObjectMode(stream) | ||
} | ||
return iterateBufferMode(stream) | ||
} |
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
Oops, something went wrong.