import { equalityComparerAsync } from '../util/comparer' ; import { identityAsync } from '../util/identity' ; import { ExtremaOptions } from './extremaoptions' ; import { wrapWithAbort } from './operators/withabort' ; import { throwIfAborted } from '../aborterror' ; template TSource param { AsyncIterable < TSource > } source param { ExtremaByOptions < TKey > } options returns { Promise < TResult > } export async function max < TSource , TResult = TSource > ( source , options ? ) { const { [ 'comparer' ] : comparer = equalityComparerAsync , [ 'signal' ] : signal , [ 'selector' ] : selector = identityAsync , } = options || { } ; throwIfAborted ( signal ) ; const it = wrapWithAbort ( source , signal ) [ Symbol . asyncIterator ] ( ) ; let next = await it . next ( ) ; if ( next . done ) { throw new Error ( 'Sequence contains no elements' ) ; } let maxValue = await selector ( next . value ) ; while ( ! ( next = await it . next ( ) ) . done ) { const current = await selector ( next . value ) ; if ( ( await comparer ( current , maxValue ) ) > 0 ) { maxValue = current ; } } return maxValue ; }