UNPKG

2.56 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { MonoTypeOperatorFunction } from '../types';
3/**
4 * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.
5 *
6 * If a keySelector function is provided, then it will project each value from the source observable into a new value that it will
7 * check for equality with previously projected values. If a keySelector function is not provided, it will use each value from the
8 * source observable directly with an equality check against previous values.
9 *
10 * In JavaScript runtimes that support `Set`, this operator will use a `Set` to improve performance of the distinct value checking.
11 *
12 * In other runtimes, this operator will use a minimal implementation of `Set` that relies on an `Array` and `indexOf` under the
13 * hood, so performance will degrade as more values are checked for distinction. Even in newer browsers, a long-running `distinct`
14 * use might result in memory leaks. To help alleviate this in some scenarios, an optional `flushes` parameter is also provided so
15 * that the internal `Set` can be "flushed", basically clearing it of values.
16 *
17 * ## Examples
18 *
19 * A simple example with numbers
20 *
21 * ```ts
22 * import { of } from 'rxjs';
23 * import { distinct } from 'rxjs/operators';
24 *
25 * of(1, 1, 2, 2, 2, 1, 2, 3, 4, 3, 2, 1)
26 * .pipe(
27 * distinct()
28 * )
29 * .subscribe(x => console.log(x));
30 *
31 * // Outputs
32 * // 1
33 * // 2
34 * // 3
35 * // 4
36 * ```
37 *
38 * An example using a keySelector function
39 *
40 * ```ts
41 * import { of } from 'rxjs';
42 * import { distinct } from 'rxjs/operators';
43 *
44 * interface Person {
45 * age: number,
46 * name: string
47 * }
48 *
49 * of(
50 * { age: 4, name: 'Foo'},
51 * { age: 7, name: 'Bar'},
52 * { age: 5, name: 'Foo'}
53 * ).pipe(
54 * distinct((p: Person) => p.name)
55 * )
56 * .subscribe(x => console.log(x));
57 *
58 * // Outputs
59 * // { age: 4, name: 'Foo' }
60 * // { age: 7, name: 'Bar' }
61 * ```
62 * @see {@link distinctUntilChanged}
63 * @see {@link distinctUntilKeyChanged}
64 *
65 * @param {function} [keySelector] Optional function to select which value you want to check as distinct.
66 * @param {Observable} [flushes] Optional Observable for flushing the internal HashSet of the operator.
67 * @return A function that returns an Observable that emits items from the
68 * source Observable with distinct values.
69 */
70export declare function distinct<T, K>(keySelector?: (value: T) => K, flushes?: Observable<any>): MonoTypeOperatorFunction<T>;
71//# sourceMappingURL=distinct.d.ts.map
\No newline at end of file