Producer.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_PERF
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Disposables;
  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. var sink = new SingleAssignmentDisposable();
  25. var subscription = new SingleAssignmentDisposable();
  26. if (CurrentThreadScheduler.Instance.ScheduleRequired)
  27. {
  28. CurrentThreadScheduler.Instance.Schedule(this, (_, me) => subscription.Disposable = me.Run(observer, subscription, s => sink.Disposable = s));
  29. }
  30. else
  31. {
  32. subscription.Disposable = this.Run(observer, subscription, s => sink.Disposable = s);
  33. }
  34. return new CompositeDisposable(2) { sink, subscription };
  35. }
  36. /// <summary>
  37. /// Core implementation of the query operator, called upon a new subscription to the producer object.
  38. /// </summary>
  39. /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
  40. /// <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>
  41. /// <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>
  42. /// <returns>Disposable representing all the resources and/or subscriptions the operator uses to process events.</returns>
  43. /// <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>
  44. protected abstract IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink);
  45. }
  46. }
  47. #endif