@reactivex/ix-es2015-cjs
Version:
The Interactive Extensions for JavaScript
87 lines (85 loc) • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.intersect = exports.IntersectAsyncIterable = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("../asynciterablex.js");
const arrayindexof_js_1 = require("../../util/arrayindexof.js");
const comparer_js_1 = require("../../util/comparer.js");
const withabort_js_1 = require("./withabort.js");
const aborterror_js_1 = require("../../aborterror.js");
function arrayRemove(array, item, comparer, signal) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
(0, aborterror_js_1.throwIfAborted)(signal);
const idx = yield (0, arrayindexof_js_1.arrayIndexOfAsync)(array, item, comparer);
if (idx === -1) {
return false;
}
array.splice(idx, 1);
return true;
});
}
/** @ignore */
class IntersectAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(first, second, comparer) {
super();
this._first = first;
this._second = second;
this._comparer = comparer;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d, _e, e_2, _f, _g;
const map = [];
try {
for (var _h = true, _j = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._second, signal)), _k; _k = yield tslib_1.__await(_j.next()), _b = _k.done, !_b; _h = true) {
_d = _k.value;
_h = false;
const secondItem = _d;
map.push(secondItem);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_h && !_b && (_c = _j.return)) yield tslib_1.__await(_c.call(_j));
}
finally { if (e_1) throw e_1.error; }
}
try {
for (var _l = true, _m = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._first, signal)), _o; _o = yield tslib_1.__await(_m.next()), _e = _o.done, !_e; _l = true) {
_g = _o.value;
_l = false;
const firstItem = _g;
if (yield tslib_1.__await(arrayRemove(map, firstItem, this._comparer, signal))) {
yield yield tslib_1.__await(firstItem);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_l && !_e && (_f = _m.return)) yield tslib_1.__await(_f.call(_m));
}
finally { if (e_2) throw e_2.error; }
}
});
}
}
exports.IntersectAsyncIterable = IntersectAsyncIterable;
/**
* Produces the set intersection of two async-iterable sequences.
*
* @template TSource The type of the elements of the input sequences.
* @param {AsyncIterable<TSource>} second An async-iterable sequence whose distinct elements that also
* appear in the first sequence will be returned.
* @param {((x: TSource, y: TSource) => boolean | Promise<boolean>)} [comparer=comparerAsync] An equality comparer to compare values.
* @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator that returns a sequence that contains the elements that form the set
* intersection of two sequences.
*/
function intersect(second, comparer = comparer_js_1.comparerAsync) {
return function intersectOperatorFunction(first) {
return new IntersectAsyncIterable(first, second, comparer);
};
}
exports.intersect = intersect;
//# sourceMappingURL=intersect.js.map