ix
Version:
The Interactive Extensions for JavaScript
45 lines (43 loc) • 1.65 kB
JavaScript
import { __awaiter } from "tslib";
import { AsyncIterableX } from '../asynciterablex.mjs';
import { create } from '../create.mjs';
import { throwIfAborted } from '../../aborterror.mjs';
class SharedAsyncIterable extends AsyncIterableX {
constructor(it) {
super();
this._it = {
next(value) {
return it.next(value);
},
};
}
[Symbol.asyncIterator](signal) {
throwIfAborted(signal);
return this._it;
}
}
/**
* 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) {
return selector
? create((signal) => __awaiter(this, void 0, void 0, function* () {
const it = yield selector(new SharedAsyncIterable(source[Symbol.asyncIterator](signal)), signal);
return it[Symbol.asyncIterator](signal);
}))
: new SharedAsyncIterable(source[Symbol.asyncIterator]());
};
}
//# sourceMappingURL=share.mjs.map