import { AsyncIterableX } from '../asynciterablex' ; import { create } from '../create' ; import { OperatorAsyncFunction } from '../../interfaces' ; import { throwIfAborted } from '../../aborterror' ; class SharedAsyncIterable < T > extends AsyncIterableX < T > { private _it : AsyncIterator < T > ; constructor ( it ) { super ( ) ; this . _it = { next ( value ) { return it . next ( value ) ; } , } ; } [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; return this . _it ; } } template TSource returns { OperatorAsyncFunction < TSource , TSource > } export function share < TSource > ( ) ; template TSource template TResult param { ( ( value : AsyncIterable < TSource > , signal ? : AbortSignal * ) => AsyncIterable < TResult > | Promise < AsyncIterable < TResult > > ) } selector returns { OperatorAsyncFunction < TSource , TResult > } export function share < TSource , TResult > ( selector ? ) ; template TSource template TResult param { ( ( value : AsyncIterable < TSource > , signal ? : AbortSignal * ) => AsyncIterable < TResult > | Promise < AsyncIterable < TResult > > ) } selector returns { ( OperatorAsyncFunction < TSource , TSource | TResult > ) } export function share < TSource , TResult = TSource > ( selector ? ) { return function shareOperatorFunction ( source ) : AsyncIterableX < TSource | TResult > { return selector ? create < TResult > ( async ( signal ) => { const it = await selector ( new SharedAsyncIterable ( source [ Symbol . asyncIterator ] ( signal ) ) , signal ) ; return it [ Symbol . asyncIterator ] ( signal ) ; } ) : new SharedAsyncIterable < TSource > ( source [ Symbol . asyncIterator ] ( ) ) ; } ; }