Skip to content

Commit d501961

Browse files
feat(sequenceEqual): compareTo should support ObservableInput (#7102)
1 parent 60d6c40 commit d501961

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

spec-dtslint/operators/sequenceEqual-spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ it('should enforce compareTo to be the same type of Observable', () => {
1616
it('should infer correctly given comparator parameter', () => {
1717
const a = of(1, 2, 3).pipe(sequenceEqual(of(1), (val1, val2) => val1 === val2)); // $ExpectType Observable<boolean>
1818
});
19+
20+
it('should support Promises', () => {
21+
of(1, 2, 3).pipe(sequenceEqual(Promise.resolve(1))); // $ExpectType Observable<boolean>
22+
// Enforce the same types produced by Promise and source observable
23+
of(1, 2, 3).pipe(sequenceEqual(Promise.resolve('foo'))); // $ExpectError
24+
});

src/internal/operators/sequenceEqual.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Observable } from '../Observable';
2-
3-
import { OperatorFunction } from '../types';
1+
import { OperatorFunction, ObservableInput } from '../types';
42
import { operate } from '../util/lift';
53
import { createOperatorSubscriber } from './OperatorSubscriber';
4+
import { innerFrom } from '../observable/innerFrom';
65

76
/**
87
* Compares all values of two observables in sequence using an optional comparator function
@@ -13,7 +12,8 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
1312
*
1413
* ![](sequenceEqual.png)
1514
*
16-
* `sequenceEqual` subscribes to two observables and buffers incoming values from each observable. Whenever either
15+
* `sequenceEqual` subscribes to source observable and `compareTo` `ObservableInput` (that internally
16+
* gets converted to an observable) and buffers incoming values from each observable. Whenever either
1717
* observable emits a value, the value is buffered and the buffers are shifted and compared from the bottom
1818
* up; If any value pair doesn't match, the returned observable will emit `false` and complete. If one of the
1919
* observables completes, the operator will wait for the other observable to complete; If the other
@@ -53,14 +53,15 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
5353
* @see {@link zip}
5454
* @see {@link withLatestFrom}
5555
*
56-
* @param {Observable} compareTo The observable sequence to compare the source sequence to.
57-
* @param {function} [comparator] An optional function to compare each value pair
56+
* @param compareTo The `ObservableInput` sequence to compare the source sequence to.
57+
* @param comparator An optional function to compare each value pair.
58+
*
5859
* @return A function that returns an Observable that emits a single boolean
5960
* value representing whether or not the values emitted by the source
60-
* Observable and provided Observable were equal in sequence.
61+
* Observable and provided `ObservableInput` were equal in sequence.
6162
*/
6263
export function sequenceEqual<T>(
63-
compareTo: Observable<T>,
64+
compareTo: ObservableInput<T>,
6465
comparator: (a: T, b: T) => boolean = (a, b) => a === b
6566
): OperatorFunction<T, boolean> {
6667
return operate((source, subscriber) => {
@@ -94,7 +95,7 @@ export function sequenceEqual<T>(
9495
// at the appropriate time.
9596
complete ? emit(false) : selfState.buffer.push(a);
9697
} else {
97-
// If the other stream *does* have values in it's buffer,
98+
// If the other stream *does* have values in its buffer,
9899
// pull the oldest one off so we can compare it to what we
99100
// just got. If it wasn't a match, emit `false` and complete.
100101
!comparator(a, buffer.shift()!) && emit(false);
@@ -119,7 +120,7 @@ export function sequenceEqual<T>(
119120

120121
// Subscribe to each source.
121122
source.subscribe(createSubscriber(aState, bState));
122-
compareTo.subscribe(createSubscriber(bState, aState));
123+
innerFrom(compareTo).subscribe(createSubscriber(bState, aState));
123124
});
124125
}
125126

0 commit comments

Comments
 (0)