@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
56 lines (54 loc) • 2.23 kB
JavaScript
import { __awaiter, __extends, __generator } from "tslib";
import { AsyncIterableX } from '../asynciterablex.js';
import { create } from '../create.js';
import { throwIfAborted } from '../../aborterror.js';
var SharedAsyncIterable = /** @class */ (function (_super) {
__extends(SharedAsyncIterable, _super);
function SharedAsyncIterable(it) {
var _this = _super.call(this) || this;
_this._it = {
next: function (value) {
return it.next(value);
},
};
return _this;
}
SharedAsyncIterable.prototype[Symbol.asyncIterator] = function (signal) {
throwIfAborted(signal);
return this._it;
};
return SharedAsyncIterable;
}(AsyncIterableX));
/**
* Shares the source sequence within a selector function where each iterator can fetch the next element from the
* source sequence.
*
* @template TSource Source sequence element type.
* @template TResult Result sequence element type.
* @param {((
* value: AsyncIterable<TSource>,
* signal?: AbortSignal
* ) => AsyncIterable<TResult> | Promise<AsyncIterable<TResult>>)} [selector] Selector function with shared access
* to the source sequence for each iterator.
* @returns {(OperatorAsyncFunction<TSource, TSource | TResult>)} Sequence resulting from applying the selector function to the
* shared view over the source sequence.
*/
export function share(selector) {
return function shareOperatorFunction(source) {
var _this = this;
return selector
? create(function (signal) { return __awaiter(_this, void 0, void 0, function () {
var it;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, selector(new SharedAsyncIterable(source[Symbol.asyncIterator](signal)), signal)];
case 1:
it = _a.sent();
return [2 /*return*/, it[Symbol.asyncIterator](signal)];
}
});
}); })
: new SharedAsyncIterable(source[Symbol.asyncIterator]());
};
}
//# sourceMappingURL=share.js.map