UNPKG

@reactivex/ix-es2015-cjs

Version:

The Interactive Extensions for JavaScript

69 lines (67 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.distinctUntilChanged = exports.DistinctUntilChangedAsyncIterable = void 0; const tslib_1 = require("tslib"); const asynciterablex_js_1 = require("../asynciterablex.js"); const identity_js_1 = require("../../util/identity.js"); const comparer_js_1 = require("../../util/comparer.js"); const withabort_js_1 = require("./withabort.js"); const aborterror_js_1 = require("../../aborterror.js"); /** @ignore */ class DistinctUntilChangedAsyncIterable extends asynciterablex_js_1.AsyncIterableX { constructor(source, keySelector, comparer) { super(); this._source = source; this._keySelector = keySelector; this._comparer = comparer; } [Symbol.asyncIterator](signal) { return tslib_1.__asyncGenerator(this, arguments, function* _a() { var _b, e_1, _c, _d; (0, aborterror_js_1.throwIfAborted)(signal); let currentKey; let hasCurrentKey = false; try { for (var _e = true, _f = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._source, signal)), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) { _d = _g.value; _e = false; const item = _d; const key = yield tslib_1.__await(this._keySelector(item, signal)); let comparerEquals = false; if (hasCurrentKey) { comparerEquals = yield tslib_1.__await(this._comparer(currentKey, key)); } if (!hasCurrentKey || !comparerEquals) { hasCurrentKey = true; currentKey = key; yield yield tslib_1.__await(item); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_e && !_b && (_c = _f.return)) yield tslib_1.__await(_c.call(_f)); } finally { if (e_1) throw e_1.error; } } }); } } exports.DistinctUntilChangedAsyncIterable = DistinctUntilChangedAsyncIterable; /** * Returns an async-iterable sequence that contains only distinct contiguous elements according to the optional 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 options for adding a key selector and comparer. * @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator that returns an async-iterable that contains only distinct contiguous items. */ function distinctUntilChanged(options) { return function distinctUntilChangedOperatorFunction(source) { const { ['keySelector']: keySelector = identity_js_1.identityAsync, ['comparer']: comparer = comparer_js_1.comparerAsync } = options || {}; return new DistinctUntilChangedAsyncIterable(source, keySelector, comparer); }; } exports.distinctUntilChanged = distinctUntilChanged; //# sourceMappingURL=distinctuntilchanged.js.map