Skip to content

Commit

Permalink
docs(distinctUntilChanged): fix docs rendering (#6756)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakovljevic-mladen committed Apr 19, 2022
1 parent 9513524 commit 2374968
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions src/internal/operators/distinctUntilChanged.ts
Expand Up @@ -3,19 +3,38 @@ import { identity } from '../util/identity';
import { operate } from '../util/lift';
import { createOperatorSubscriber } from './OperatorSubscriber';

export function distinctUntilChanged<T>(comparator?: (previous: T, current: T) => boolean): MonoTypeOperatorFunction<T>;
export function distinctUntilChanged<T, K>(
comparator: (previous: K, current: K) => boolean,
keySelector: (value: T) => K
): MonoTypeOperatorFunction<T>;

/**
* Returns a result {@link Observable} that emits all values pushed by the source observable if they
* are distinct in comparison to the last value the result observable emitted.
*
* When provided without parameters or with the first parameter (`{@link distinctUntilChanged#comparator comparator}`),
* it behaves like this:
*
* 1. It will always emit the first value from the source.
* 2. For all subsequent values pushed by the source, they will be compared to the previously emitted values
* using the provided `comparator` or an `===` equality check.
* 3. If the value pushed by the source is determined to be unequal by this check, that value is emitted and
* becomes the new "previously emitted value" internally.
*
* When the second parameter (`{@link distinctUntilChanged#keySelector keySelector}`) is provided, the behavior
* changes:
*
* 1. It will always emit the first value from the source.
* 2. The `keySelector` will be run against all values, including the first value.
* 3. For all values after the first, the selected key will be compared against the key selected from
* the previously emitted value using the `comparator`.
* 4. If the keys are determined to be unequal by this check, the value (not the key), is emitted
* and the selected key from that value is saved for future comparisons against other keys.
*
* ## Examples
*
* A very basic example with no `comparator`. Note that `1` is emitted more than once,
* A very basic example with no `{@link distinctUntilChanged#comparator comparator}`. Note that `1` is emitted more than once,
* because it's distinct in comparison to the _previously emitted_ value,
* not in comparison to _all other emitted values_.
*
Expand All @@ -28,7 +47,7 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
* // Logs: 1, 2, 1, 3
* ```
*
* With a `comparator`, you can do custom comparisons. Let's say
* With a `{@link distinctUntilChanged#comparator comparator}`, you can do custom comparisons. Let's say
* you only want to emit a value when all of its components have
* changed:
*
Expand Down Expand Up @@ -58,7 +77,7 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
* // { engineVersion: '2.0.0', transmissionVersion: '1.5.0' }
* ```
*
* You can also provide a custom `comparator` to check that emitted
* You can also provide a custom `{@link distinctUntilChanged#comparator comparator}` to check that emitted
* changes are only in one direction. Let's say you only want to get
* the next record temperature:
*
Expand All @@ -80,28 +99,8 @@ import { createOperatorSubscriber } from './OperatorSubscriber';
* // Logs: 30, 31, 34, 35
* ```
*
* @param comparator A function used to compare the previous and current values for
* equality. Defaults to a `===` check.
* @return A function that returns an Observable that emits items from the
* source Observable with distinct values.
*/
export function distinctUntilChanged<T>(comparator?: (previous: T, current: T) => boolean): MonoTypeOperatorFunction<T>;

/**
* Returns a result {@link Observable} that emits all values pushed by the source observable if they
* are distinct in comparison to the last value the result observable emitted.
*
* 1. It will always emit the first value from the source.
* 2. The `keySelector` will be run against all values, including the first value.
* 3. For all values after the first, the selected key will be compared against the key selected from
* the previously emitted value using the `comparator`.
* 4. If the keys are determined to be unequal by this check, the value (not the key), is emitted
* and the selected key from that value is saved for future comparisons against other keys.
*
* ## Example
*
* Selecting update events only when the `updatedBy` field shows
* the account changed hands...
* the account changed hands.
*
* ```ts
* import { of, distinctUntilChanged } from 'rxjs';
Expand All @@ -127,17 +126,16 @@ export function distinctUntilChanged<T>(comparator?: (previous: T, current: T) =
* // { updatedBy: 'blesh', data: Array[0] }
* ```
*
* @see {@link distinct}
* @see {@link distinctUntilKeyChanged}
*
* @param comparator A function used to compare the previous and current keys for
* equality. Defaults to a `===` check.
* @param keySelector Used to select a key value to be passed to the `comparator`.
*
* @return A function that returns an Observable that emits items from the
* source Observable with distinct values.
*/
export function distinctUntilChanged<T, K>(
comparator: (previous: K, current: K) => boolean,
keySelector: (value: T) => K
): MonoTypeOperatorFunction<T>;

export function distinctUntilChanged<T, K>(
comparator?: (previous: K, current: K) => boolean,
keySelector: (value: T) => K = identity as (value: T) => K
Expand Down

0 comments on commit 2374968

Please sign in to comment.