| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 | // Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the Apache 2.0 License.// See the LICENSE file in the project root for more information. using System.Collections.Generic;using System.Threading.Tasks;namespace System.Reactive.Linq{    partial class AsyncObservable    {        public static IAsyncObservable<IList<TSource>> MinBy<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            return Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.MinBy(observer, keySelector)));        }        public static IAsyncObservable<IList<TSource>> MinBy<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            if (comparer == null)                throw new ArgumentNullException(nameof(comparer));            return Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.MinBy(observer, keySelector, comparer)));        }        public static IAsyncObservable<IList<TSource>> MinBy<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, Task<TKey>> keySelector)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            return Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.MinBy(observer, keySelector)));        }        public static IAsyncObservable<IList<TSource>> MinBy<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            if (comparer == null)                throw new ArgumentNullException(nameof(comparer));            return Create<IList<TSource>>(observer => source.SubscribeSafeAsync(AsyncObserver.MinBy(observer, keySelector, comparer)));        }    }    partial class AsyncObserver    {        public static IAsyncObserver<TSource> MinBy<TSource, TKey>(IAsyncObserver<IList<TSource>> observer, Func<TSource, TKey> keySelector)        {            if (observer == null)                throw new ArgumentNullException(nameof(observer));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            return MinBy(observer, x => Task.FromResult(keySelector(x)), Comparer<TKey>.Default);        }        public static IAsyncObserver<TSource> MinBy<TSource, TKey>(IAsyncObserver<IList<TSource>> observer, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)        {            if (observer == null)                throw new ArgumentNullException(nameof(observer));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            if (comparer == null)                throw new ArgumentNullException(nameof(comparer));            return MinBy(observer, x => Task.FromResult(keySelector(x)), comparer);        }        public static IAsyncObserver<TSource> MinBy<TSource, TKey>(IAsyncObserver<IList<TSource>> observer, Func<TSource, Task<TKey>> keySelector)        {            if (observer == null)                throw new ArgumentNullException(nameof(observer));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            return MinBy(observer, keySelector, Comparer<TKey>.Default);        }        public static IAsyncObserver<TSource> MinBy<TSource, TKey>(IAsyncObserver<IList<TSource>> observer, Func<TSource, Task<TKey>> keySelector, IComparer<TKey> comparer)        {            if (observer == null)                throw new ArgumentNullException(nameof(observer));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            if (comparer == null)                throw new ArgumentNullException(nameof(comparer));            var hasValue = false;            var lastKey = default(TKey);            var list = new List<TSource>();            return Create<TSource>(                async x =>                {                    var key = default(TKey);                    try                    {                        key = await keySelector(x).ConfigureAwait(false);                    }                    catch (Exception ex)                    {                        await observer.OnErrorAsync(ex).ConfigureAwait(false);                        return;                    }                    var comparison = 0;                    if (!hasValue)                    {                        hasValue = true;                        lastKey = key;                    }                    else                    {                        try                        {                            comparison = comparer.Compare(key, lastKey);                        }                        catch (Exception ex)                        {                            await observer.OnErrorAsync(ex).ConfigureAwait(false);                            return;                        }                    }                    if (comparison < 0)                    {                        lastKey = key;                        list.Clear();                    }                    if (comparison <= 0)                    {                        list.Add(x);                    }                },                observer.OnErrorAsync,                async () =>                {                    await observer.OnNextAsync(list).ConfigureAwait(false);                    await observer.OnCompletedAsync().ConfigureAwait(false);                }            );        }    }}
 |