The example given does not pertain to TypeScript and, therefore, the use of undefined
is restricted in the types below as a first argument. Instead, it is recommended to specify a default comparator and set the keySelector
.
Type Declaration:
import { MonoTypeOperatorFunction } from '../types';
export declare function distinctUntilChanged<T>(comparator?: (previous: T, current: T) => boolean): MonoTypeOperatorFunction<T>;
export declare function distinctUntilChanged<T, K>(comparator: (previous: K, current: K) => boolean, keySelector: (value: T) => K): MonoTypeOperatorFunction<T>;
Code Example:
import { of, distinctUntilChanged, Observable } from 'rxjs';
export interface TypeObj {
updatedBy: string;
data: Array<string>;
}
// Represents a sequence of updates for a specific account
const accountUpdates$: Observable<TypeObj> = of(
{ updatedBy: 'blesh', data: [] },
{ updatedBy: 'blesh', data: [] },
{ updatedBy: 'ncjamieson', data: [] },
{ updatedBy: 'ncjamieson', data: [] },
{ updatedBy: 'blesh', data: [] }
);
// Filtering out only the events where ownership changed
const changedHands$ = accountUpdates$.pipe(
distinctUntilChanged<TypeObj, string>(
(prev: string, curr: string): boolean => prev === curr,
(update: TypeObj) => update?.updatedBy
)
);
changedHands$.subscribe(console.log);