1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
6 /* tslint:disable:max-line-length */
7 export function distinctUntilChanged<T>(compare?: (x: T, y: T) => boolean): MonoTypeOperatorFunction<T>;
8 export function distinctUntilChanged<T, K>(compare: (x: K, y: K) => boolean, keySelector: (x: T) => K): MonoTypeOperatorFunction<T>;
9 /* tslint:enable:max-line-length */
12 * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item.
14 * If a comparator function is provided, then it will be called for each item to test for whether or not that value should be emitted.
16 * If a comparator function is not provided, an equality check is used by default.
19 * A simple example with numbers
21 * import { of } from 'rxjs';
22 * import { distinctUntilChanged } from 'rxjs/operators';
24 * of(1, 1, 2, 2, 2, 1, 1, 2, 3, 3, 4).pipe(
25 * distinctUntilChanged(),
27 * .subscribe(x => console.log(x)); // 1, 2, 1, 2, 3, 4
30 * An example using a compare function
32 * import { of } from 'rxjs';
33 * import { distinctUntilChanged } from 'rxjs/operators';
41 * { age: 4, name: 'Foo'},
42 * { age: 7, name: 'Bar'},
43 * { age: 5, name: 'Foo'},
44 * { age: 6, name: 'Foo'},
46 * distinctUntilChanged((p: Person, q: Person) => p.name === q.name),
48 * .subscribe(x => console.log(x));
51 * // { age: 4, name: 'Foo' }
52 * // { age: 7, name: 'Bar' }
53 * // { age: 5, name: 'Foo' }
56 * @see {@link distinct}
57 * @see {@link distinctUntilKeyChanged}
59 * @param {function} [compare] Optional comparison function called to test if an item is distinct from the previous item in the source.
60 * @return {Observable} An Observable that emits items from the source Observable with distinct values.
61 * @method distinctUntilChanged
64 export function distinctUntilChanged<T, K>(compare?: (x: K, y: K) => boolean, keySelector?: (x: T) => K): MonoTypeOperatorFunction<T> {
65 return (source: Observable<T>) => source.lift(new DistinctUntilChangedOperator<T, K>(compare, keySelector));
68 class DistinctUntilChangedOperator<T, K> implements Operator<T, T> {
69 constructor(private compare: (x: K, y: K) => boolean,
70 private keySelector: (x: T) => K) {
73 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
74 return source.subscribe(new DistinctUntilChangedSubscriber(subscriber, this.compare, this.keySelector));
79 * We need this JSDoc comment for affecting ESDoc.
83 class DistinctUntilChangedSubscriber<T, K> extends Subscriber<T> {
85 private hasKey: boolean = false;
87 constructor(destination: Subscriber<T>,
88 compare: (x: K, y: K) => boolean,
89 private keySelector: (x: T) => K) {
91 if (typeof compare === 'function') {
92 this.compare = compare;
96 private compare(x: any, y: any): boolean {
100 protected _next(value: T): void {
103 const { keySelector } = this;
104 key = keySelector ? keySelector(value) : value;
106 return this.destination.error(err);
111 const { compare } = this;
112 result = compare(this.key, key);
114 return this.destination.error(err);
121 this.destination.next(value);