ix
Version:
The Interactive Extensions for JavaScript
82 lines (80 loc) • 3.36 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await, __awaiter } from "tslib";
import { AsyncIterableX } from '../asynciterablex.mjs';
import { arrayIndexOfAsync } from '../../util/arrayindexof.mjs';
import { comparerAsync } from '../../util/comparer.mjs';
import { wrapWithAbort } from './withabort.mjs';
import { throwIfAborted } from '../../aborterror.mjs';
function arrayRemove(array, item, comparer, signal) {
return __awaiter(this, void 0, void 0, function* () {
throwIfAborted(signal);
const idx = yield arrayIndexOfAsync(array, item, comparer);
if (idx === -1) {
return false;
}
array.splice(idx, 1);
return true;
});
}
/** @ignore */
export class IntersectAsyncIterable extends AsyncIterableX {
constructor(first, second, comparer) {
super();
this._first = first;
this._second = second;
this._comparer = comparer;
}
[Symbol.asyncIterator](signal) {
return __asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d, _e, e_2, _f, _g;
const map = [];
try {
for (var _h = true, _j = __asyncValues(wrapWithAbort(this._second, signal)), _k; _k = yield __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 __await(_c.call(_j));
}
finally { if (e_1) throw e_1.error; }
}
try {
for (var _l = true, _m = __asyncValues(wrapWithAbort(this._first, signal)), _o; _o = yield __await(_m.next()), _e = _o.done, !_e; _l = true) {
_g = _o.value;
_l = false;
const firstItem = _g;
if (yield __await(arrayRemove(map, firstItem, this._comparer, signal))) {
yield yield __await(firstItem);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_l && !_e && (_f = _m.return)) yield __await(_f.call(_m));
}
finally { if (e_2) throw e_2.error; }
}
});
}
}
/**
* 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.
*/
export function intersect(second, comparer = comparerAsync) {
return function intersectOperatorFunction(first) {
return new IntersectAsyncIterable(first, second, comparer);
};
}
//# sourceMappingURL=intersect.mjs.map