import { IComparer , IOrderedParallelEnumerable , ParallelGeneratorType } from "../types" ; import { asAsyncSortedKeyValues } from "./_ordered/asAsyncSortedKeyValues" ; import { asAsyncSortedKeyValuesSync } from "./_ordered/asAsyncSortedKeyValuesSync" ; import { asSortedKeyValues } from "./_ordered/asSortedKeyValues" ; import { asSortedKeyValuesSync } from "./_ordered/asSortedKeyValuesSync" ; import { BasicParallelEnumerable } from "./BasicParallelEnumerable" ; private export class OrderedParallelEnumerable < T > extends BasicParallelEnumerable < T > implements IOrderedParallelEnumerable < T > { private constructor ( private readonly orderedPairs ) { super ( { generator : async ( ) => { const asyncVals = orderedPairs ( ) ; const array = [ ] ; for await ( const val of asyncVals ) { array . push ( ... val ) ; } return array ; } , type : ParallelGeneratorType . PromiseToArray , } ) ; } public static generateAsync < TSource , TKey > ( source , keySelector , ascending , comparer ? ) { let orderedPairs ; if ( source instanceof OrderedParallelEnumerable ) { orderedPairs = async function * ( ) { for await ( const pair of source . orderedPairs ( ) ) { yield * asAsyncSortedKeyValuesSync ( pair , keySelector , ascending , comparer ) ; } } ; } else { orderedPairs = ( ) => asAsyncSortedKeyValues ( source , keySelector , ascending , comparer ) ; } return new OrderedParallelEnumerable ( orderedPairs ) ; } public static generate < TSource , TKey > ( source , keySelector , ascending , comparer ? ) { let orderedPairs ; if ( source instanceof OrderedParallelEnumerable ) { orderedPairs = async function * ( ) { for await ( const pair of source . orderedPairs ( ) ) { yield * asSortedKeyValuesSync ( pair , keySelector , ascending , comparer ) ; } } ; } else { orderedPairs = ( ) => asSortedKeyValues ( source , keySelector , ascending , comparer ) ; } return new OrderedParallelEnumerable ( orderedPairs ) ; } public thenBy < TKey > ( keySelector , comparer ? ) { return OrderedParallelEnumerable . generate < T , TKey > ( this , keySelector , true , comparer ) ; } public thenByAsync < TKey > ( keySelector , comparer ? ) { return OrderedParallelEnumerable . generateAsync ( this , keySelector , true , comparer ) ; } public thenByDescending < TKey > ( keySelector , comparer ? ) { return OrderedParallelEnumerable . generate < T , TKey > ( this , keySelector , false , comparer ) ; } public thenByDescendingAsync < TKey > ( keySelector , comparer ? ) { return OrderedParallelEnumerable . generateAsync < T , TKey > ( this , keySelector , false , comparer ) ; } }