Producer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System.Reactive.Concurrency;
  4. using System.Reactive.Disposables;
  5. namespace System.Reactive
  6. {
  7. /// <summary>
  8. /// Base class for implementation of query operators, providing performance benefits over the use of <see cref="System.Reactive.Linq.Observable.Create">Observable.Create</see>.
  9. /// </summary>
  10. /// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
  11. abstract class Producer<TSource> : IObservable<TSource>
  12. {
  13. /// <summary>
  14. /// Publicly visible Subscribe method.
  15. /// </summary>
  16. /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
  17. /// <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>
  18. public IDisposable Subscribe(IObserver<TSource> observer)
  19. {
  20. if (observer == null)
  21. throw new ArgumentNullException("observer");
  22. var sink = new SingleAssignmentDisposable();
  23. var subscription = new SingleAssignmentDisposable();
  24. if (CurrentThreadScheduler.Instance.ScheduleRequired)
  25. {
  26. CurrentThreadScheduler.Instance.Schedule(this, (_, me) => subscription.Disposable = me.Run(observer, subscription, s => sink.Disposable = s));
  27. }
  28. else
  29. {
  30. subscription.Disposable = this.Run(observer, subscription, s => sink.Disposable = s);
  31. }
  32. return new CompositeDisposable(2) { sink, subscription };
  33. }
  34. /// <summary>
  35. /// Core implementation of the query operator, called upon a new subscription to the producer object.
  36. /// </summary>
  37. /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
  38. /// <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>
  39. /// <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>
  40. /// <returns>Disposable representing all the resources and/or subscriptions the operator uses to process events.</returns>
  41. /// <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>
  42. protected abstract IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink);
  43. }
  44. }
  45. #endif