1 import { Observable } from '../Observable';
2 import { Operator } from '../Operator';
3 import { Subscriber } from '../Subscriber';
4 import { OuterSubscriber } from '../OuterSubscriber';
5 import { InnerSubscriber } from '../InnerSubscriber';
6 import { subscribeToResult } from '../util/subscribeToResult';
7 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
10 * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.
12 * If a keySelector function is provided, then it will project each value from the source observable into a new value that it will
13 * check for equality with previously projected values. If a keySelector function is not provided, it will use each value from the
14 * source observable directly with an equality check against previous values.
16 * In JavaScript runtimes that support `Set`, this operator will use a `Set` to improve performance of the distinct value checking.
18 * In other runtimes, this operator will use a minimal implementation of `Set` that relies on an `Array` and `indexOf` under the
19 * hood, so performance will degrade as more values are checked for distinction. Even in newer browsers, a long-running `distinct`
20 * use might result in memory leaks. To help alleviate this in some scenarios, an optional `flushes` parameter is also provided so
21 * that the internal `Set` can be "flushed", basically clearing it of values.
24 * A simple example with numbers
26 * import { of } from 'rxjs';
27 * import { distinct } from 'rxjs/operators';
29 * of(1, 1, 2, 2, 2, 1, 2, 3, 4, 3, 2, 1).pipe(
32 * .subscribe(x => console.log(x)); // 1, 2, 3, 4
35 * An example using a keySelector function
37 * import { of } from 'rxjs';
38 * import { distinct } from 'rxjs/operators';
46 * { age: 4, name: 'Foo'},
47 * { age: 7, name: 'Bar'},
48 * { age: 5, name: 'Foo'},
50 * distinct((p: Person) => p.name),
52 * .subscribe(x => console.log(x));
55 * // { age: 4, name: 'Foo' }
56 * // { age: 7, name: 'Bar' }
58 * @see {@link distinctUntilChanged}
59 * @see {@link distinctUntilKeyChanged}
61 * @param {function} [keySelector] Optional function to select which value you want to check as distinct.
62 * @param {Observable} [flushes] Optional Observable for flushing the internal HashSet of the operator.
63 * @return {Observable} An Observable that emits items from the source Observable with distinct values.
67 export function distinct<T, K>(keySelector?: (value: T) => K,
68 flushes?: Observable<any>): MonoTypeOperatorFunction<T> {
69 return (source: Observable<T>) => source.lift(new DistinctOperator(keySelector, flushes));
72 class DistinctOperator<T, K> implements Operator<T, T> {
73 constructor(private keySelector: (value: T) => K, private flushes: Observable<any>) {
76 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
77 return source.subscribe(new DistinctSubscriber(subscriber, this.keySelector, this.flushes));
82 * We need this JSDoc comment for affecting ESDoc.
86 export class DistinctSubscriber<T, K> extends OuterSubscriber<T, T> {
87 private values = new Set<K>();
89 constructor(destination: Subscriber<T>, private keySelector: (value: T) => K, flushes: Observable<any>) {
93 this.add(subscribeToResult(this, flushes));
97 notifyNext(outerValue: T, innerValue: T,
98 outerIndex: number, innerIndex: number,
99 innerSub: InnerSubscriber<T, T>): void {
103 notifyError(error: any, innerSub: InnerSubscriber<T, T>): void {
107 protected _next(value: T): void {
108 if (this.keySelector) {
109 this._useKeySelector(value);
111 this._finalizeNext(value, value);
115 private _useKeySelector(value: T): void {
117 const { destination } = this;
119 key = this.keySelector(value);
121 destination.error(err);
124 this._finalizeNext(key, value);
127 private _finalizeNext(key: K|T, value: T) {
128 const { values } = this;
129 if (!values.has(<K>key)) {
131 this.destination.next(value);