网上演练
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.8 KiB

// Needed for NET40
#if !NET_4_6
using System;
using LinqInternal.Core;
namespace LinqInternal.Collections
{
internal static class ObservableExtensions
{
public static IDisposable SubscribeAction<T>(this IObservable<T> observable, Action<T> listener)
{
if (observable == null)
{
throw new ArgumentNullException("observable");
}
return observable.Subscribe(listener.ToObserver());
}
public static IDisposable SubscribeAction<TInput, TOutput>(this IObservable<TInput> observable, Action<TOutput> listener, Func<TInput, TOutput> converter)
{
if (observable == null)
{
throw new ArgumentNullException("observable");
}
return observable.Subscribe(listener.ToObserver(converter));
}
public static IDisposable SubscribeConverted<TInput, TOutput>(this IObservable<TInput> observable, IObserver<TOutput> observer, Func<TInput, TOutput> converter)
{
if (observable == null)
{
throw new ArgumentNullException("observable");
}
return observable.Subscribe(new ConvertedObserver<TInput, TOutput>(observer, converter));
}
public static IDisposable SubscribeFiltered<T>(this IObservable<T> observable, IObserver<T> observer, Predicate<T> filter)
{
if (observable == null)
{
throw new ArgumentNullException("observable");
}
return observable.Subscribe(new FilteredObserver<T>(observer, filter));
}
public static IDisposable SubscribeFilteredConverted<TInput, TOutput>(this IObservable<TInput> observable, IObserver<TOutput> observer, Predicate<TInput> filter, Func<TInput, TOutput> converter)
{
if (observable == null)
{
throw new ArgumentNullException("observable");
}
return observable.Subscribe(new FilteredConvertedObserver<TInput, TOutput>(observer, filter, converter));
}
public static IObserver<T> ToObserver<T>(this Action<T> listener)
{
if (listener == null)
{
throw new ArgumentNullException("listener");
}
return new CustomObserver<T>(listener);
}
public static IObserver<TInput> ToObserver<TInput, TOutput>(this Action<TOutput> listener, Func<TInput, TOutput> converter)
{
if (converter == null)
{
throw new ArgumentNullException("converter");
}
if (listener == null)
{
throw new ArgumentNullException("listener");
}
return new CustomObserver<TInput>(input => listener(converter(input)));
}
}
}
#endif