Skip to content

Commit

Permalink
feat: add cap method and relative test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbubu committed Jan 29, 2020
1 parent c79b101 commit c606ada
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function(ary?: Source<any>[]) {
next()
}

read.add = function(stream: Source<any>) {
read.add = function(stream?: Source<any>) {
if (!stream) {
// the stream will now end when all the streams end.
capped = true
Expand All @@ -121,5 +121,9 @@ export default function(ary?: Source<any>[]) {
next()
}

read.cap = function() {
read.add()
}

return read
}
58 changes: 58 additions & 0 deletions test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as pull from 'pull-stream'
import many from '../src/'

describe('add', () => {
it('add after stream creation', done => {
const m = many()

pull(
m,
pull.collect(function(err, ary) {
expect(err).toBeFalsy()
expect(ary.sort()).toEqual([1, 2, 3, 4, 5, 6])
done()
})
)
m.add(pull.values([1, 2, 3]))
m.add(pull.values([4, 5, 6]))
m.add()
})

it('add after stream creation - more', done => {
const m = many()

pull(
m,
pull.collect(function(err, ary) {
expect(err).toBeFalsy()
expect(ary.sort()).toEqual([])
done()
})
)
m.add()
})

it('do not close inputs until the last minute', done => {
const m = many()
const seen: number[] = []

pull(
m,
pull.through(function(data) {
// wait until the last message to end inputs.
seen.push(data)
if (data >= 6) {
m.cap()
}
}),
pull.collect(function(err, ary) {
expect(err).toBeFalsy()
expect(ary.sort()).toEqual([1, 2, 3, 4, 5, 6])
expect(seen.sort()).toEqual([1, 2, 3, 4, 5, 6])
done()
})
)
m.add(pull.values([1, 2, 3]))
m.add(pull.values([4, 5, 6]))
})
})

0 comments on commit c606ada

Please sign in to comment.