// 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.Threading.Tasks; namespace System.Reactive.Linq { partial class AsyncObserver { public static IAsyncObserver MinInt32(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = 0; var found = false; return Create( x => { if (found) { if (x < min) { min = x; } } else { min = 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(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } } ); } public static IAsyncObserver MinInt64(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = 0L; var found = false; return Create( x => { if (found) { if (x < min) { min = x; } } else { min = 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(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } } ); } public static IAsyncObserver MinSingle(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = 0.0f; var found = false; return Create( x => { if (found) { if (x < min || double.IsNaN(x)) { min = x; } } else { min = 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(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } } ); } public static IAsyncObserver MinDouble(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = 0.0; var found = false; return Create( x => { if (found) { if (x < min || double.IsNaN(x)) { min = x; } } else { min = 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(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } } ); } public static IAsyncObserver MinDecimal(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = 0m; var found = false; return Create( x => { if (found) { if (x < min) { min = x; } } else { min = 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(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } } ); } public static IAsyncObserver MinNullableInt32(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = default(int?); return Create( x => { if (min == null || x < min) { min = x; } return Task.CompletedTask; }, observer.OnErrorAsync, async () => { await observer.OnNextAsync(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } ); } public static IAsyncObserver MinNullableInt64(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = default(long?); return Create( x => { if (min == null || x < min) { min = x; } return Task.CompletedTask; }, observer.OnErrorAsync, async () => { await observer.OnNextAsync(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } ); } public static IAsyncObserver MinNullableSingle(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = default(float?); return Create( x => { if (x != null && (min == null || x < min || double.IsNaN(x.Value))) { min = x; } return Task.CompletedTask; }, observer.OnErrorAsync, async () => { await observer.OnNextAsync(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } ); } public static IAsyncObserver MinNullableDouble(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = default(double?); return Create( x => { if (x != null && (min == null || x < min || double.IsNaN(x.Value))) { min = x; } return Task.CompletedTask; }, observer.OnErrorAsync, async () => { await observer.OnNextAsync(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } ); } public static IAsyncObserver MinNullableDecimal(this IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var min = default(decimal?); return Create( x => { if (min == null || x < min) { min = x; } return Task.CompletedTask; }, observer.OnErrorAsync, async () => { await observer.OnNextAsync(min).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } ); } } }