Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support goodbye sources #10

Merged
merged 1 commit into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"delay": "^5.0.0",
"it-all": "^1.0.6",
"it-drain": "^1.0.5",
"p-defer": "^4.0.0",
"streaming-iterables": "^6.0.0"
},
"dependencies": {
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const isDuplex = <TSource, TSink = TSource, RSink = Promise<void>> (obj:
return obj != null && typeof obj.sink === 'function' && isIterable(obj.source)
}

const duplexPipelineFn = <TSource> (duplex: any) => {
const duplexPipelineFn = <TSource> (duplex: it.Duplex<TSource>) => {
return (source: any): it.Source<TSource> => {
const p = duplex.sink(source)

Expand All @@ -34,7 +34,12 @@ const duplexPipelineFn = <TSource> (duplex: any) => {
stream.end(err)
})

return merge(stream, duplex.source)
const sourceWrap = async function * () {
yield * duplex.source
stream.end()
}

return merge(stream, sourceWrap())
}

return duplex.source
Expand Down
36 changes: 36 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import all from 'it-all'
import drain from 'it-drain'
import { filter, collect, consume } from 'streaming-iterables'
import delay from 'delay'
import defer from 'p-defer'
import type { Duplex, Source } from 'it-stream-types'

const oneTwoThree = () => [1, 2, 3]
Expand Down Expand Up @@ -137,4 +138,39 @@ describe('it-pipe', () => {
)
).to.eventually.be.rejected.with.property('message', err.message)
})

it('should support goodbye-style source', async () => {
const deferred = defer()
const end = 5
let otherEnded = false

await pipe(
async function * () {
for (let i = 0; i < end + 1; i++) {
yield i
}

while (!otherEnded) { // eslint-disable-line no-unmodified-loop-condition
await delay(10)
}
}, {
sink: async (source: Source<number>) => {
for await (const val of source) {
if (val === end) {
deferred.resolve()
}
}
},
source: (async function * () {
yield * [1, 2, 3]
yield end
await deferred.promise
}())
},
async (source) => {
await drain(source)
otherEnded = true
}
)
})
})