Debian Codegraph @

node/rxjs/src/internal/operators/distinct.ts

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