UNPKG

@reactivex/ix-esnext-esm

Version:

The Interactive Extensions for JavaScript

46 lines (44 loc) 1.94 kB
import { AsyncIterableX } from './../asynciterablex.js'; import { identityAsync } from '../../util/identity.js'; import { arrayIndexOfAsync } from '../../util/arrayindexof.js'; import { comparerAsync } from '../../util/comparer.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class DistinctAsyncIterable extends AsyncIterableX { _source; _keySelector; _comparer; constructor(source, keySelector, comparer) { super(); this._source = source; this._keySelector = keySelector; this._comparer = comparer; } async *[Symbol.asyncIterator](signal) { throwIfAborted(signal); const set = []; for await (const item of wrapWithAbort(this._source, signal)) { const key = await this._keySelector(item, signal); if ((await arrayIndexOfAsync(set, key, this._comparer)) === -1) { set.push(key); yield item; } } } } /** * Returns an async-iterable sequence that contains only distinct elements according to the keySelector and comparer. * * @template TSource The type of the elements in the source sequence. * @template TKey The type of the discriminator key computed for each element in the source sequence. * @param {DistinctOptions<TSource, TKey = TSource>} [options] The optional arguments for a key selector and comparer function. * @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator that returns distinct elements according to the keySelector and options. */ export function distinct(options) { return function distinctOperatorFunction(source) { const { ['keySelector']: keySelector = identityAsync, ['comparer']: comparer = comparerAsync } = options || {}; return new DistinctAsyncIterable(source, keySelector, comparer); }; } //# sourceMappingURL=distinct.js.map