1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- // 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<TSource> Distinct<TSource>(IAsyncObservable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Distinct(observer)));
- }
- public static IAsyncObservable<TSource> Distinct<TSource>(IAsyncObservable<TSource> source, IEqualityComparer<TSource> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return Create(
- source,
- comparer,
- static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.Distinct(observer, comparer)));
- }
- }
- public partial class AsyncObserver
- {
- public static IAsyncObserver<TSource> Distinct<TSource>(IAsyncObserver<TSource> observer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- return Distinct(observer, EqualityComparer<TSource>.Default);
- }
- public static IAsyncObserver<TSource> Distinct<TSource>(IAsyncObserver<TSource> observer, IEqualityComparer<TSource> comparer)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- var set = new HashSet<TSource>(comparer);
- return Create<TSource>(
- async x =>
- {
- var added = false;
- try
- {
- added = set.Add(x);
- }
- catch (Exception ex)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- return;
- }
- if (added)
- {
- await observer.OnNextAsync(x).ConfigureAwait(false);
- }
- },
- observer.OnErrorAsync,
- observer.OnCompletedAsync
- );
- }
- }
- }
|