Skip to content

Commit b18c2eb

Browse files
feat(sample): notifier now supports any ObservableInput (#7104)
* feat(sample): add ObservableInput support in notifier * test(sample): added tests for sample's ObservableInput notifier * chore(sample): cleanup unused declaration * Delete index.d.ts * Delete index.d.ts Co-authored-by: Ben Lesh <[email protected]>
1 parent 8f1b976 commit b18c2eb

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

spec-dtslint/operators/sample-spec.ts

+69-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,77 @@
11
import { of } from 'rxjs';
22
import { sample } from 'rxjs/operators';
3+
import { asInteropObservable } from '../../spec/helpers/interop-helper';
34

45
it('should enforce parameter', () => {
5-
const a = of(1, 2, 3).pipe(sample()); // $ExpectError
6+
of(1, 2, 3).pipe(sample()); // $ExpectError
67
});
78

89
it('should accept observable as notifier parameter', () => {
9-
const a = of(1, 2, 3).pipe(sample(of(4))); // $ExpectType Observable<number>
10-
const b = of(1, 2, 3).pipe(sample(of('a'))); // $ExpectType Observable<number>
10+
of(1, 2, 3).pipe(sample(of(4))); // $ExpectType Observable<number>
11+
of(1, 2, 3).pipe(sample(of('a'))); // $ExpectType Observable<number>
12+
});
13+
14+
it('should accept interop observable notifier', () => {
15+
of(1, 2, 3).pipe(sample(asInteropObservable(of(true)))); // $ExpectType Observable<number>
16+
});
17+
18+
it('should accept promise notifier', () => {
19+
of(1, 2, 3).pipe(sample(Promise.resolve(true))); // $ExpectType Observable<number>
20+
});
21+
22+
it('should async iterable notifier', () => {
23+
const asyncRange = {
24+
from: 1,
25+
to: 2,
26+
[Symbol.asyncIterator]() {
27+
return {
28+
current: this.from,
29+
last: this.to,
30+
async next() {
31+
await Promise.resolve();
32+
const done = (this.current > this.last);
33+
return {
34+
done,
35+
value: done ? this.current++ : undefined
36+
};
37+
}
38+
};
39+
}
40+
};
41+
of(1, 2, 3).pipe(sample(asyncRange)); // $ExpectType Observable<number>
42+
});
43+
44+
it('should accept iterable notifier', () => {
45+
const syncRange = {
46+
from: 1,
47+
to: 2,
48+
[Symbol.iterator]() {
49+
return {
50+
current: this.from,
51+
last: this.to,
52+
next() {
53+
const done = (this.current > this.last);
54+
return {
55+
done,
56+
value: done ? this.current++ : undefined
57+
};
58+
}
59+
};
60+
}
61+
};
62+
of(1, 2, 3).pipe(sample(syncRange)); // $ExpectType Observable<number>
63+
});
64+
65+
it('should accept readable stream notifier', () => {
66+
const readableStream = new ReadableStream<string>({
67+
pull(controller) {
68+
controller.enqueue('x');
69+
controller.close();
70+
},
71+
});
72+
of(1, 2, 3).pipe(sample(readableStream)); // $ExpectType Observable<number>
73+
});
74+
75+
it('should enforce types of the notifier', () => {
76+
of(1, 2, 3).pipe(sample(8)); // $ExpectError
1177
});

src/internal/operators/sample.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Observable } from '../Observable';
2-
import { MonoTypeOperatorFunction } from '../types';
1+
import { innerFrom } from '../observable/innerFrom';
2+
import { MonoTypeOperatorFunction, ObservableInput } from '../types';
33
import { operate } from '../util/lift';
44
import { noop } from '../util/noop';
55
import { createOperatorSubscriber } from './OperatorSubscriber';
@@ -9,11 +9,11 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
99
* another Observable, the `notifier`, emits.
1010
*
1111
* <span class="informal">It's like {@link sampleTime}, but samples whenever
12-
* the `notifier` Observable emits something.</span>
12+
* the `notifier` `ObservableInput` emits something.</span>
1313
*
1414
* ![](sample.png)
1515
*
16-
* Whenever the `notifier` Observable emits a value, `sample`
16+
* Whenever the `notifier` `ObservableInput` emits a value, `sample`
1717
* looks at the source Observable and emits whichever value it has most recently
1818
* emitted since the previous sampling, unless the source has not emitted
1919
* anything since the previous sampling. The `notifier` is subscribed to as soon
@@ -38,13 +38,13 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
3838
* @see {@link sampleTime}
3939
* @see {@link throttle}
4040
*
41-
* @param notifier The Observable to use for sampling the
41+
* @param notifier The `ObservableInput` to use for sampling the
4242
* source Observable.
4343
* @return A function that returns an Observable that emits the results of
4444
* sampling the values emitted by the source Observable whenever the notifier
4545
* Observable emits value or completes.
4646
*/
47-
export function sample<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
47+
export function sample<T>(notifier: ObservableInput<any>): MonoTypeOperatorFunction<T> {
4848
return operate((source, subscriber) => {
4949
let hasValue = false;
5050
let lastValue: T | null = null;
@@ -54,7 +54,7 @@ export function sample<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T
5454
lastValue = value;
5555
})
5656
);
57-
notifier.subscribe(
57+
innerFrom(notifier).subscribe(
5858
createOperatorSubscriber(
5959
subscriber,
6060
() => {

0 commit comments

Comments
 (0)