Skip to content

Commit

Permalink
feat(combine): support zero streams args to combine()
Browse files Browse the repository at this point in the history
If combine() takes just the project function but no member streams, then it behaves like
xs.of(project()). This is useful when doing a spread over an array of member streams, and the array
happens to be empty.
  • Loading branch information
staltz committed May 2, 2016
1 parent 62c656c commit 1b3ca90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 15 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,12 @@ class CombineProducer<R> implements InternalProducer<R> {

_start(out: InternalListener<R>): void {
this.out = out;
const streams = this.streams;
for (let i = streams.length - 1; i >= 0; i--) {
streams[i]._add(new CombineListener(i, this));
const s = this.streams;
const L = s.length;
if (L == 0) this.zero(out); else {
for (let i = 0; i < L; i++) {
s[i]._add(new CombineListener(i, this));
}
}
}

Expand All @@ -224,6 +227,15 @@ class CombineProducer<R> implements InternalProducer<R> {
this.vals = new Array(streams.length);
this.ac = streams.length;
}

zero(out: InternalListener<R>): void {
try {
out._n(this.project<R>());
out._c();
} catch (e) {
out._e(e);
}
}
}

export class FromArrayProducer<T> implements InternalProducer<T> {
Expand Down
22 changes: 19 additions & 3 deletions tests/factory/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ describe('xs.combine', () => {
});
});

it('should handle a group of zero streams', (done) => {
const stream = xs.combine<string>(() => 'hi');
let expected = ['hi'];

stream.addListener({
next: (x) => {
assert.equal(x, expected.shift());
},
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
},
});
});

it('should not break future listeners when CombineProducer tears down', (done) => {
// --0--1-2--| innerA
// ---0---1--| innerB
Expand All @@ -96,10 +112,10 @@ describe('xs.combine', () => {
outer.shamefullySendNext(0);
}, 100);
setTimeout(() => {
innerA.shamefullySendNext(0)
innerA.shamefullySendNext(0);
}, 150);
setTimeout(() => {
innerB.shamefullySendNext(0)
innerB.shamefullySendNext(0);
}, 175);
setTimeout(() => {
arrayInners.push(innerB);
Expand All @@ -109,7 +125,7 @@ describe('xs.combine', () => {
setTimeout(() => {
innerA.shamefullySendNext(2);
outer.shamefullySendNext(2);
innerB.shamefullySendNext(1)
innerB.shamefullySendNext(1);
}, 250);
setTimeout(() => {
innerA.shamefullySendComplete();
Expand Down

0 comments on commit 1b3ca90

Please sign in to comment.