| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_PERF |
| 4 |
using System.Reactive.Concurrency; |
| 5 |
using System.Reactive.Disposables; |
| 6 |
|
| 7 |
namespace System.Reactive |
| 8 |
{ |
| 9 |
/// <summary> |
| 10 |
/// Base class for implementation of query operators, providing performance benefits over the use of <see cref="System.Reactive.Linq.Observable.Create">Observable.Create</see>. |
| 11 |
/// </summary> |
| 12 |
/// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam> |
| 13 |
abstract class Producer<TSource> : IObservable<TSource> |
| 14 |
{ |
| 15 |
/// <summary> |
| 16 |
/// Publicly visible Subscribe method. |
| 17 |
/// </summary> |
| 18 |
/// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param> |
| 19 |
/// <returns>IDisposable to cancel the subscription. This causes the underlying sink to be notified of unsubscription, causing it to prevent further messages from being sent to the observer.</returns> |
| 20 |
public IDisposable Subscribe(IObserver<TSource> observer) |
| 21 |
{ |
| 22 |
if (observer == null) |
| 23 |
throw new ArgumentNullException("observer"); |
| 24 |
|
| 25 |
var sink = new SingleAssignmentDisposable(); |
| 26 |
var subscription = new SingleAssignmentDisposable(); |
| 27 |
|
| 28 |
if (CurrentThreadScheduler.Instance.ScheduleRequired) |
| 29 |
{ |
| 30 |
CurrentThreadScheduler.Instance.Schedule(this, (_, me) => subscription.Disposable = me.Run(observer, subscription, s => sink.Disposable = s)); |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
subscription.Disposable = this.Run(observer, subscription, s => sink.Disposable = s); |
| 35 |
} |
| 36 |
|
| 37 |
return new CompositeDisposable(2) { sink, subscription }; |
| 38 |
} |
| 39 |
|
| 40 |
/// <summary> |
| 41 |
/// Core implementation of the query operator, called upon a new subscription to the producer object. |
| 42 |
/// </summary> |
| 43 |
/// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param> |
| 44 |
/// <param name="cancel">The subscription disposable object returned from the Run call, passed in such that it can be forwarded to the sink, allowing it to dispose the subscription upon sending a final message (or prematurely for other reasons).</param> |
| 45 |
/// <param name="setSink">Callback to communicate the sink object to the subscriber, allowing consumers to tunnel a Dispose call into the sink, which can stop the processing.</param> |
| 46 |
/// <returns>Disposable representing all the resources and/or subscriptions the operator uses to process events.</returns> |
| 47 |
/// <remarks>The <paramref name="observer">observer</paramref> passed in to this method is not protected using auto-detach behavior upon an OnError or OnCompleted call. The implementation must ensure proper resource disposal and enforce the message grammar.</remarks> |
| 48 |
protected abstract IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink); |
| 49 |
} |
| 50 |
} |
| 51 |
#endif |