import { IParallelEnumerable , ParallelGeneratorType , TypedData } from "../../types" ; import { BasicParallelEnumerable } from "../BasicParallelEnumerable" ; import { nextIteration } from "./_nextIteration" ; import { nextIterationWithIndex } from "./_nextIterationWithIndex" ; type SelectManyFunc = { param source param selector returns < TSource , OUT > ( source , selector ) : IParallelEnumerable < OUT > ; param source param selector returns < TBindedSource extends { [ key ] : Iterable < TOut > ; } , TOut > ( source , selector ) : IParallelEnumerable < TOut > ; } ; export const selectMany = < TSource , OUT > ( source , selector ) : IParallelEnumerable < any > => { const generator = async ( ) => { let values ; if ( typeof selector === "function" ) { if ( selector . length === 1 ) { values = nextIteration ( source , selector as ( x : TSource ) => Iterable < OUT > ) ; } else { values = nextIterationWithIndex ( source , selector ) ; } } else { values = nextIteration ( source , ( x ) => x [ selector ] ) ; } const valuesArray = [ ] ; switch ( values . type ) { case ParallelGeneratorType . PromiseToArray : { for ( const outer of await values . generator ( ) ) { for ( const y of outer ) { valuesArray . push ( y ) ; } } break ; } case ParallelGeneratorType . ArrayOfPromises : { for ( const outer of values . generator ( ) ) { for ( const y of await outer ) { valuesArray . push ( y ) ; } } break ; } case ParallelGeneratorType . PromiseOfPromises : { for ( const outer of await values . generator ( ) ) { for ( const y of await outer ) { valuesArray . push ( y ) ; } } break ; } } return valuesArray ; } ; return new BasicParallelEnumerable ( { generator , type : ParallelGeneratorType . PromiseToArray , } ) ; } ;