ix
Version:
The Interactive Extensions for JavaScript
55 lines (53 loc) • 2.2 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await } from "tslib";
import { AsyncIterableX } from '../asynciterablex.mjs';
import { wrapWithAbort } from './withabort.mjs';
import { throwIfAborted } from '../../aborterror.mjs';
/** @ignore */
export class FilterAsyncIterable extends AsyncIterableX {
constructor(source, predicate, thisArg) {
super();
this._source = source;
this._predicate = predicate;
this._thisArg = thisArg;
}
[Symbol.asyncIterator](signal) {
return __asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
throwIfAborted(signal);
let i = 0;
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;
if (yield __await(this._predicate.call(this._thisArg, item, i++))) {
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; }
}
});
}
}
/**
* Filters the elements of an async-iterable sequence based on a predicate.
*
* @template TSource The type of the elements in the source sequence.
* @param {((value: TSource, index: number, signal?: AbortSignal) => boolean | Promise<boolean>)} predicate A function to test each source element
* for a condition.
* @param {*} [thisArg] Optional this for binding.
* @returns {OperatorAsyncFunction<TSource, TSource>} An operator which returns an async-iterable
* sequence that contains elements from the input sequence that satisfy the condition.
*/
export function filter(predicate, thisArg) {
return function filterOperatorFunction(source) {
return new FilterAsyncIterable(source, predicate, thisArg);
};
}
//# sourceMappingURL=filter.mjs.map