// 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.Collections.Generic; namespace System.Reactive.Linq { public partial class AsyncObservable { public static IAsyncObservable> ToHashSet(this IAsyncObservable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return Create>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToHashSet(observer))); } public static IAsyncObservable> ToHashSet(this IAsyncObservable source, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (comparer == null) throw new ArgumentNullException(nameof(comparer)); return CreateAsyncObservable>.From( source, comparer, static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.ToHashSet(observer, comparer))); } } public partial class AsyncObserver { public static IAsyncObserver ToHashSet(IAsyncObserver> observer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); return ToHashSet(observer, EqualityComparer.Default); } public static IAsyncObserver ToHashSet(IAsyncObserver> observer, IEqualityComparer comparer) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (comparer == null) throw new ArgumentNullException(nameof(comparer)); return Aggregate>(observer, new HashSet(comparer), (set, x) => { set.Add(x); return set; }); } } }