// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Threading.Tasks; namespace System.Reactive.Linq { public partial class AsyncObservable { public static IAsyncObservable Count(this IAsyncObservable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer))); } public static IAsyncObservable Count(this IAsyncObservable source, Func predicate) { if (source == null) throw new ArgumentNullException(nameof(source)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return CreateAsyncObservable.From( source, predicate, static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate))); } public static IAsyncObservable Count(this IAsyncObservable source, Func> predicate) { if (source == null) throw new ArgumentNullException(nameof(source)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return CreateAsyncObservable.From( source, predicate, static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate))); } } public partial class AsyncObserver { public static IAsyncObserver Count(IAsyncObserver observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); var count = 0; return Create( async x => { try { checked { count++; } } catch (Exception ex) { await observer.OnErrorAsync(ex).ConfigureAwait(false); return; } }, observer.OnErrorAsync, async () => { await observer.OnNextAsync(count).ConfigureAwait(false); await observer.OnCompletedAsync().ConfigureAwait(false); } ); } public static IAsyncObserver Count(IAsyncObserver observer, Func predicate) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return Where(Count(observer), predicate); } public static IAsyncObserver Count(IAsyncObserver observer, Func> predicate) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return Where(Count(observer), predicate); } } }