ix
Version:
The Interactive Extensions for JavaScript
59 lines (57 loc) • 2.65 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await } from "tslib";
import { AsyncIterableX } from './../asynciterablex.mjs';
import { identityAsync } from '../../util/identity.mjs';
import { arrayIndexOfAsync } from '../../util/arrayindexof.mjs';
import { comparerAsync } from '../../util/comparer.mjs';
import { wrapWithAbort } from './withabort.mjs';
import { throwIfAborted } from '../../aborterror.mjs';
/** @ignore */
export class DistinctAsyncIterable extends AsyncIterableX {
constructor(source, keySelector, comparer) {
super();
this._source = source;
this._keySelector = keySelector;
this._comparer = comparer;
}
[Symbol.asyncIterator](signal) {
return __asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
throwIfAborted(signal);
const set = [];
try {
for (var _e = true, _f = __asyncValues(wrapWithAbort(this._source, signal)), _g; _g = yield __await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
const key = yield __await(this._keySelector(item, signal));
if ((yield __await(arrayIndexOfAsync(set, key, this._comparer))) === -1) {
set.push(key);
yield yield __await(item);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield __await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
});
}
}
/**
* 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.mjs.map