@reactivex/ix-es2015-cjs
Version:
The Interactive Extensions for JavaScript
63 lines (61 loc) • 2.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.expand = exports.ExpandAsyncIterable = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("../asynciterablex.js");
const withabort_js_1 = require("./withabort.js");
const aborterror_js_1 = require("../../aborterror.js");
/** @ignore */
class ExpandAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(source, selector) {
super();
this._source = source;
this._selector = selector;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
(0, aborterror_js_1.throwIfAborted)(signal);
const q = [this._source];
while (q.length > 0) {
const src = q.shift();
try {
for (var _e = true, _f = (e_1 = void 0, tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(src, signal))), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
const items = yield tslib_1.__await(this._selector(item, signal));
q.push(items);
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.ExpandAsyncIterable = ExpandAsyncIterable;
/**
* Expands (breadth first) the async-iterable sequence by recursively applying a selector function to generate more sequences at each recursion level.
*
* @template TSource Source sequence element type.
* @param {((
* value: TSource,
* signal?: AbortSignal
* ) => AsyncIterable<TSource> | Promise<AsyncIterable<TSource>>)} selector Selector function to retrieve the next sequence to expand.
* @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator which returns a sequence with results
* from the recursive expansion of the source sequence.
*/
function expand(selector) {
return function expandOperatorFunction(source) {
return new ExpandAsyncIterable(source, selector);
};
}
exports.expand = expand;
//# sourceMappingURL=expand.js.map