// 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.Reactive.Disposables;
namespace System.Reactive
{
///
/// Class to create an IObservable<T> instance from a delegate-based implementation of the Subscribe method.
///
/// The type of the elements in the sequence.
public sealed class AnonymousObservable : ObservableBase
{
private readonly Func, IDisposable> _subscribe;
///
/// Creates an observable sequence object from the specified subscription function.
///
/// Subscribe method implementation.
/// is null.
public AnonymousObservable(Func, IDisposable> subscribe)
{
if (subscribe == null)
throw new ArgumentNullException(nameof(subscribe));
_subscribe = subscribe;
}
///
/// Calls the subscription function that was supplied to the constructor.
///
/// Observer to send notifications to.
/// Disposable object representing an observer's subscription to the observable sequence.
protected override IDisposable SubscribeCore(IObserver observer)
{
return _subscribe(observer) ?? Disposable.Empty;
}
}
}