// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license 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("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;
        }
    }
}