| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Disposables; |
| 4 |
|
| 5 |
namespace System.Reactive |
| 6 |
{ |
| 7 |
/// <summary> |
| 8 |
/// Class to create an IObservable<T> instance from a delegate-based implementation of the Subscribe method. |
| 9 |
/// </summary> |
| 10 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 11 |
public sealed class AnonymousObservable<T> : ObservableBase<T> |
| 12 |
{ |
| 13 |
private readonly Func<IObserver<T>, IDisposable> _subscribe; |
| 14 |
|
| 15 |
/// <summary> |
| 16 |
/// Creates an observable sequence object from the specified subscription function. |
| 17 |
/// </summary> |
| 18 |
/// <param name="subscribe">Subscribe method implementation.</param> |
| 19 |
/// <exception cref="ArgumentNullException"><paramref name="subscribe"/> is null.</exception> |
| 20 |
public AnonymousObservable(Func<IObserver<T>, IDisposable> subscribe) |
| 21 |
{ |
| 22 |
if (subscribe == null) |
| 23 |
throw new ArgumentNullException("subscribe"); |
| 24 |
|
| 25 |
_subscribe = subscribe; |
| 26 |
} |
| 27 |
|
| 28 |
/// <summary> |
| 29 |
/// Calls the subscription function that was supplied to the constructor. |
| 30 |
/// </summary> |
| 31 |
/// <param name="observer">Observer to send notifications to.</param> |
| 32 |
/// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns> |
| 33 |
protected override IDisposable SubscribeCore(IObserver<T> observer) |
| 34 |
{ |
| 35 |
return _subscribe(observer) ?? Disposable.Empty; |
| 36 |
} |
| 37 |
} |
| 38 |
} |