123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- // 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<TSource> Max<TSource>(IAsyncObservable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Max(observer)));
- }
- public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<TSource> source, IComparer<TSource> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Max(observer, comparer)));
- }
- }
- partial class AsyncObserver
- {
- public static IAsyncObserver<TSource> Max<TSource>(IAsyncObserver<TSource> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- return Max(observer, Comparer<TSource>.Default);
- }
- public static IAsyncObserver<TSource> Max<TSource>(IAsyncObserver<TSource> observer, IComparer<TSource> comparer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- var max = default(TSource);
- var found = false;
- return Create<TSource>(
- async x =>
- {
- if (found)
- {
- bool isGreater;
- try
- {
- isGreater = comparer.Compare(x, max) > 0;
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- if (isGreater)
- {
- max = x;
- }
- }
- else
- {
- max = x;
- found = true;
- }
- },
- observer.OnErrorAsync,
- async () =>
- {
- if (!found)
- {
- await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
- }
- else
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- }
- public static IAsyncObserver<int> MaxInt32(IAsyncObserver<int> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = 0;
- var found = false;
- return Create<int>(
- x =>
- {
- if (found)
- {
- if (x > max)
- {
- max = x;
- }
- }
- else
- {
- max = x;
- found = true;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- if (!found)
- {
- await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
- }
- else
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- }
- public static IAsyncObserver<long> MaxInt64(IAsyncObserver<long> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = 0L;
- var found = false;
- return Create<long>(
- x =>
- {
- if (found)
- {
- if (x > max)
- {
- max = x;
- }
- }
- else
- {
- max = x;
- found = true;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- if (!found)
- {
- await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
- }
- else
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- }
- public static IAsyncObserver<float> MaxSingle(IAsyncObserver<float> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = 0.0f;
- var found = false;
- return Create<float>(
- x =>
- {
- if (found)
- {
- if (x > max || double.IsNaN(x))
- {
- max = x;
- }
- }
- else
- {
- max = x;
- found = true;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- if (!found)
- {
- await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
- }
- else
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- }
- public static IAsyncObserver<double> MaxDouble(IAsyncObserver<double> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = 0.0;
- var found = false;
- return Create<double>(
- x =>
- {
- if (found)
- {
- if (x > max || double.IsNaN(x))
- {
- max = x;
- }
- }
- else
- {
- max = x;
- found = true;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- if (!found)
- {
- await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
- }
- else
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- }
- public static IAsyncObserver<decimal> MaxDecimal(IAsyncObserver<decimal> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = 0m;
- var found = false;
- return Create<decimal>(
- x =>
- {
- if (found)
- {
- if (x > max)
- {
- max = x;
- }
- }
- else
- {
- max = x;
- found = true;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- if (!found)
- {
- await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
- }
- else
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- }
- public static IAsyncObserver<int?> MaxNullableInt32(IAsyncObserver<int?> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = default(int?);
- return Create<int?>(
- x =>
- {
- if (max == null || x > max)
- {
- max = x;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- public static IAsyncObserver<long?> MaxNullableInt64(IAsyncObserver<long?> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = default(long?);
- return Create<long?>(
- x =>
- {
- if (max == null || x > max)
- {
- max = x;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- public static IAsyncObserver<float?> MaxNullableSingle(IAsyncObserver<float?> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = default(float?);
- return Create<float?>(
- x =>
- {
- if (x != null && (max == null || x > max || double.IsNaN(x.Value)))
- {
- max = x;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- public static IAsyncObserver<double?> MaxNullableDouble(IAsyncObserver<double?> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = default(double?);
- return Create<double?>(
- x =>
- {
- if (x != null && (max == null || x > max || double.IsNaN(x.Value)))
- {
- max = x;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- public static IAsyncObserver<decimal?> MaxNullableDecimal(IAsyncObserver<decimal?> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- var max = default(decimal?);
- return Create<decimal?>(
- x =>
- {
- if (max == null || x > max)
- {
- max = x;
- }
- return Task.CompletedTask;
- },
- observer.OnErrorAsync,
- async () =>
- {
- await observer.OnNextAsync(max).ConfigureAwait(false);
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- );
- }
- }
- }
|