import { AsyncIterableX } from '../asynciterablex' ; import { OperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; export class FlatMapAsyncIterable < TSource , TResult > extends AsyncIterableX < TResult > { private _source : AsyncIterable < TSource > ; private _selector : ( value : TSource , index : number , signal ? : AbortSignal ) => AsyncIterable < TResult > | Promise < AsyncIterable < TResult > > ; private _thisArg ? : any ; constructor ( source , selector , thisArg ? ) { super ( ) ; this . _source = source ; this . _selector = selector ; this . _thisArg = thisArg ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; const { _source : source , _selector : selector , _thisArg : thisArg } = this ; let index = 0 ; for await ( const outer of wrapWithAbort ( source , signal ) ) { const inners = await selector . call ( thisArg , outer , index ++ , signal ) ; for await ( const inner of wrapWithAbort ( inners , signal ) ) { yield inner ; } } } } template TSource template TResult param { ( ( value : TSource , index : number , signal ? : AbortSignal * ) => AsyncIterable < TResult > | Promise < AsyncIterable < TResult > > ) } selector param { * } thisArg returns { OperatorAsyncFunction < TSource , TResult > } export function flatMap < TSource , TResult > ( selector , thisArg ? ) { return function flatMapOperatorFunction ( source ) : AsyncIterableX < TResult > { return new FlatMapAsyncIterable < TSource , TResult > ( source , selector , thisArg ) ; } ; }