import { AsyncIterableX } from '../asynciterablex' ; import { identityAsync } from '../../util/identity' ; import { comparerAsync } from '../../util/comparer' ; import { MonoTypeOperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; import { DistinctOptions } from './distinctoptions' ; export class DistinctUntilChangedAsyncIterable < TSource , TKey = TSource > extends AsyncIterableX < TSource > { private _source : AsyncIterable < TSource > ; private _keySelector : ( value : TSource , signal ? : AbortSignal ) => TKey | Promise < TKey > ; private _comparer : ( x : TKey , y : TKey ) => boolean | Promise < boolean > ; constructor ( source , keySelector , comparer ) { super ( ) ; this . _source = source ; this . _keySelector = keySelector ; this . _comparer = comparer ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; let currentKey ; let hasCurrentKey = false ; for await ( const item of wrapWithAbort ( this . _source , signal ) ) { const key = await this . _keySelector ( item , signal ) ; let comparerEquals = false ; if ( hasCurrentKey ) { comparerEquals = await this . _comparer ( currentKey ! , key ) ; } if ( ! hasCurrentKey || ! comparerEquals ) { hasCurrentKey = true ; currentKey = key ; yield item ; } } } } template TSource template TKey param { DistinctOptions < TSource , TKey = TSource returns { MonoTypeOperatorAsyncFunction < TSource > } export function distinctUntilChanged < TSource , TKey = TSource > ( options ? ) { return function distinctUntilChangedOperatorFunction ( source ) : AsyncIterableX < TSource > { const { [ 'keySelector' ] : keySelector = identityAsync , [ 'comparer' ] : comparer = comparerAsync } = options || { } ; return new DistinctUntilChangedAsyncIterable < TSource , TKey > ( source , keySelector , comparer ) ; } ; }