Skip to content

Commit 38782d8

Browse files
committed
fix(sampleCombine): change API to fit compose() usage
1 parent cff94da commit 38782d8

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

src/extra/sampleCombine.ts

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
1-
import {CombineSignature, InternalListener, Operator, Stream} from '../core';
1+
import {InternalListener, Operator, Stream} from '../core';
2+
3+
export interface SampleCombineSignature {
4+
(): <T>(s: Stream<T>) => Stream<[T]>;
5+
<T1>(s1: Stream<T1>): <T>(s: Stream<T>) => Stream<[T, T1]>;
6+
<T1, T2>(
7+
s1: Stream<T1>,
8+
s2: Stream<T2>): <T>(s: Stream<T>) => Stream<[T, T1, T2]>;
9+
<T1, T2, T3>(
10+
s1: Stream<T1>,
11+
s2: Stream<T2>,
12+
s3: Stream<T3>): <T>(s: Stream<T>) => Stream<[T, T1, T2, T3]>;
13+
<T1, T2, T3, T4>(
14+
s1: Stream<T1>,
15+
s2: Stream<T2>,
16+
s3: Stream<T3>,
17+
s4: Stream<T4>): <T>(s: Stream<T>) => Stream<[T, T1, T2, T3, T4]>;
18+
<T1, T2, T3, T4, T5>(
19+
s1: Stream<T1>,
20+
s2: Stream<T2>,
21+
s3: Stream<T3>,
22+
s4: Stream<T4>,
23+
s5: Stream<T5>): <T>(s: Stream<T>) => Stream<[T, T1, T2, T3, T4, T5]>;
24+
<T1, T2, T3, T4, T5, T6>(
25+
s1: Stream<T1>,
26+
s2: Stream<T2>,
27+
s3: Stream<T3>,
28+
s4: Stream<T4>,
29+
s5: Stream<T5>,
30+
s6: Stream<T6>): <T>(s: Stream<T>) => Stream<[T, T1, T2, T3, T4, T5, T6]>;
31+
<T1, T2, T3, T4, T5, T6, T7>(
32+
s1: Stream<T1>,
33+
s2: Stream<T2>,
34+
s3: Stream<T3>,
35+
s4: Stream<T4>,
36+
s5: Stream<T5>,
37+
s6: Stream<T6>,
38+
s7: Stream<T7>): <T>(s: Stream<T>) => Stream<[T, T1, T2, T3, T4, T5, T6, T7]>;
39+
<T1, T2, T3, T4, T5, T6, T7, T8>(
40+
s1: Stream<T1>,
41+
s2: Stream<T2>,
42+
s3: Stream<T3>,
43+
s4: Stream<T4>,
44+
s5: Stream<T5>,
45+
s6: Stream<T6>,
46+
s7: Stream<T7>,
47+
s8: Stream<T8>): <T>(s: Stream<T>) => Stream<[T, T1, T2, T3, T4, T5, T6, T7, T8]>;
48+
(...streams: Array<Stream<any>>): (s: Stream<any>) => Stream<Array<any>>;
49+
}
250

351
export class SampleCombineListener<T> implements InternalListener<T> {
452
constructor(private i: number, private p: SampleCombineOperator<any>) {
@@ -75,6 +123,13 @@ export class SampleCombineOperator<T> implements Operator<T, Array<any>> {
75123
}
76124
}
77125

126+
let sampleCombine: SampleCombineSignature;
127+
sampleCombine = function sampleCombine(...streams: Array<Stream<any>>) {
128+
return function sampleCombineOperator(sampler: Stream<any>): Stream<Array<any>> {
129+
return new Stream<Array<any>>(new SampleCombineOperator(sampler, streams));
130+
};
131+
} as SampleCombineSignature;
132+
78133
/**
79134
* Combines a source stream with multiple other streams. The result stream
80135
* will emit the latest events from all input streams, but only when the
@@ -90,10 +145,10 @@ export class SampleCombineOperator<T> implements Operator<T, Array<any>> {
90145
* Marble diagram:
91146
*
92147
* ```text
93-
* --1----2-----3--------4---
94-
* ----a-----b-----c--d------
148+
* --1----2-----3--------4--- (source)
149+
* ----a-----b-----c--d------ (other)
95150
* sampleCombine
96-
* --1?---2a----3b-------4d--
151+
* -------2a----3b-------4d--
97152
* ```
98153
*
99154
* Examples:
@@ -105,7 +160,7 @@ export class SampleCombineOperator<T> implements Operator<T, Array<any>> {
105160
* const sampler = xs.periodic(1000).take(3)
106161
* const other = xs.periodic(100)
107162
*
108-
* const stream = sampleCombine(sampler, other)
163+
* const stream = sampler.compose(sampleCombine(other))
109164
*
110165
* stream.addListener({
111166
* next: i => console.log(i),
@@ -127,7 +182,7 @@ export class SampleCombineOperator<T> implements Operator<T, Array<any>> {
127182
* const sampler = xs.periodic(1000).take(3)
128183
* const other = xs.periodic(100).take(2)
129184
*
130-
* const stream = sampleCombine(sampler, other)
185+
* const stream = sampler.compose(sampleCombine(other))
131186
*
132187
* stream.addListener({
133188
* next: i => console.log(i),
@@ -142,14 +197,8 @@ export class SampleCombineOperator<T> implements Operator<T, Array<any>> {
142197
* > [2, 1]
143198
* ```
144199
*
145-
* @param {Stream} sampler The source stream of which to sample.
146-
* @param {...Stream} streams One or more streams to combine.
200+
* @param {...Stream} streams One or more streams to combine with the sampler
201+
* stream.
147202
* @return {Stream}
148203
*/
149-
let sampleCombine: CombineSignature;
150-
sampleCombine = function(sampler: Stream<any>,
151-
...streams: Array<Stream<any>>): Stream<Array<any>> {
152-
return new Stream<Array<any>>(new SampleCombineOperator<any>(sampler, streams));
153-
} as CombineSignature;
154-
155-
export default sampleCombine;
204+
export default sampleCombine;

tests/extra/sampleCombine.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import * as assert from 'assert';
66

77
describe('sampleCombine (extra)', () => {
88
it('should combine AND-style two streams together', (done) => {
9-
const stream1 = xs.periodic(100).take(3);
9+
const stream1 = xs.periodic(100).take(3).startWith(null);
1010
const stream2 = xs.periodic(99).take(3);
11-
const stream = sampleCombine(stream1, stream2);
11+
const stream = stream1.compose(sampleCombine(stream2));
1212
let expected = [[0, 0], [1, 1], [2, 2]];
1313
stream.addListener({
1414
next: (x) => {
@@ -35,14 +35,15 @@ describe('sampleCombine (extra)', () => {
3535
stop: () => {}
3636
});
3737

38-
const combined: Stream<[string, string]> = sampleCombine(stream1, stream2);
38+
const combined: Stream<[string, string]> = stream1
39+
.compose(sampleCombine(stream2));
3940
done();
4041
});
4142

4243
it('should complete only when the sample stream has completed', (done) => {
4344
const stream1 = xs.periodic(100).take(4);
4445
const stream2 = xs.periodic(99).take(1);
45-
const stream = sampleCombine(stream1, stream2).map(arr => arr.join(''));
46+
const stream = stream1.compose(sampleCombine(stream2)).map(arr => arr.join(''));
4647
let expected = ['00', '10', '20', '30'];
4748
stream.addListener({
4849
next: (x) => {
@@ -56,24 +57,9 @@ describe('sampleCombine (extra)', () => {
5657
});
5758
});
5859

59-
it('should emit an empty array if combining zero streams', (done) => {
60-
const stream = sampleCombine();
61-
62-
stream.addListener({
63-
next: (a) => {
64-
assert.equal(Array.isArray(a), true);
65-
assert.equal(a.length, 0);
66-
},
67-
error: done,
68-
complete: () => {
69-
done();
70-
},
71-
});
72-
});
73-
7460
it('should just wrap the value if combining one stream', (done) => {
7561
const source = xs.periodic(100).take(3);
76-
const stream = sampleCombine(source);
62+
const stream = source.compose(sampleCombine());
7763
let expected = [[0], [1], [2]];
7864

7965
stream.addListener({
@@ -100,7 +86,9 @@ describe('sampleCombine (extra)', () => {
10086
const arrayInners: Array<Stream<number>> = [];
10187
const stream = outer
10288
.map(x => {
103-
return sampleCombine(...arrayInners)
89+
const sampler = arrayInners[0];
90+
const others = arrayInners.slice(1, 1000);
91+
return sampler.compose(sampleCombine(...others))
10492
.map(combination => `${x}${combination.join('')}`);
10593
})
10694
.flatten();
@@ -147,15 +135,15 @@ describe('sampleCombine (extra)', () => {
147135
it('should return a Stream when combining a MemoryStream with a Stream', (done) => {
148136
const input1 = xs.periodic(80).take(4).remember();
149137
const input2 = xs.periodic(50).take(3);
150-
const stream: Stream<[number, number]> = sampleCombine(input1, input2);
138+
const stream: Stream<[number, number]> = input1.compose(sampleCombine(input2));
151139
assert.strictEqual(stream instanceof Stream, true);
152140
done();
153141
});
154142

155143
it('should return a Stream when combining a MemoryStream with a MemoryStream', (done) => {
156144
const input1 = xs.periodic(80).take(4).remember();
157145
const input2 = xs.periodic(50).take(3).remember();
158-
const stream: Stream<[number, number]> = sampleCombine(input1, input2);
146+
const stream: Stream<[number, number]> = input1.compose(sampleCombine(input2));
159147
assert.strictEqual(stream instanceof Stream, true);
160148
done();
161149
});

0 commit comments

Comments
 (0)