| 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 |
using System.Reactive.Linq; |
| 5 |
|
| 6 |
namespace System.Reactive.Subjects |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Represents an observable wrapper that can be connected and disconnected from its underlying observable sequence. |
| 10 |
/// </summary> |
| 11 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 12 |
/// <typeparam name="TResult">The type of the elements in the resulting sequence, after transformation through the subject.</typeparam> |
| 13 |
internal class ConnectableObservable<TSource, TResult> : IConnectableObservable<TResult> |
| 14 |
{ |
| 15 |
private readonly ISubject<TSource, TResult> _subject; |
| 16 |
private readonly IObservable<TSource> _source; |
| 17 |
private readonly object _gate; |
| 18 |
|
| 19 |
private Connection _connection; |
| 20 |
|
| 21 |
/// <summary> |
| 22 |
/// Creates an observable that can be connected and disconnected from its source. |
| 23 |
/// </summary> |
| 24 |
/// <param name="source">Underlying observable source sequence that can be connected and disconnected from the wrapper.</param> |
| 25 |
/// <param name="subject">Subject exposed by the connectable observable, receiving data from the underlying source sequence upon connection.</param> |
| 26 |
public ConnectableObservable(IObservable<TSource> source, ISubject<TSource, TResult> subject) |
| 27 |
{ |
| 28 |
_subject = subject; |
| 29 |
_source = source.AsObservable(); // This gets us auto-detach behavior; otherwise, we'd have to roll our own, including trampoline installation. |
| 30 |
_gate = new object(); |
| 31 |
} |
| 32 |
|
| 33 |
/// <summary> |
| 34 |
/// Connects the observable wrapper to its source. All subscribed observers will receive values from the underlying observable sequence as long as the connection is established. |
| 35 |
/// </summary> |
| 36 |
/// <returns>Disposable object used to disconnect the observable wrapper from its source, causing subscribed observer to stop receiving values from the underlying observable sequence.</returns> |
| 37 |
public IDisposable Connect() |
| 38 |
{ |
| 39 |
lock (_gate) |
| 40 |
{ |
| 41 |
if (_connection == null) |
| 42 |
{ |
| 43 |
var subscription = _source.SubscribeSafe(_subject); |
| 44 |
_connection = new Connection(this, subscription); |
| 45 |
} |
| 46 |
|
| 47 |
return _connection; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
class Connection : IDisposable |
| 52 |
{ |
| 53 |
private readonly ConnectableObservable<TSource, TResult> _parent; |
| 54 |
private IDisposable _subscription; |
| 55 |
|
| 56 |
public Connection(ConnectableObservable<TSource, TResult> parent, IDisposable subscription) |
| 57 |
{ |
| 58 |
_parent = parent; |
| 59 |
_subscription = subscription; |
| 60 |
} |
| 61 |
|
| 62 |
public void Dispose() |
| 63 |
{ |
| 64 |
lock (_parent._gate) |
| 65 |
{ |
| 66 |
if (_subscription != null) |
| 67 |
{ |
| 68 |
_subscription.Dispose(); |
| 69 |
_subscription = null; |
| 70 |
|
| 71 |
_parent._connection = null; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/// <summary> |
| 78 |
/// Subscribes an observer to the observable sequence. No values from the underlying observable source will be received unless a connection was established through the Connect method. |
| 79 |
/// </summary> |
| 80 |
/// <param name="observer">Observer that will receive values from the underlying observable source when the current ConnectableObservable instance is connected through a call to Connect.</param> |
| 81 |
/// <returns>Disposable used to unsubscribe from the observable sequence.</returns> |
| 82 |
public IDisposable Subscribe(IObserver<TResult> observer) |
| 83 |
{ |
| 84 |
if (observer == null) |
| 85 |
throw new ArgumentNullException("observer"); |
| 86 |
|
| 87 |
return _subject.SubscribeSafe(observer); |
| 88 |
} |
| 89 |
} |
| 90 |
} |