Producer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /// Interface with variance annotation; allows for better type checking when detecting capabilities in SubscribeSafe.
  11. /// </summary>
  12. /// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
  13. internal interface IProducer<
  14. #if !NO_VARIANCE
  15. out
  16. #endif
  17. TSource> : IObservable<TSource>
  18. {
  19. IDisposable SubscribeRaw(IObserver<TSource> observer, bool enableSafeguard);
  20. }
  21. /// <summary>
  22. /// Base class for implementation of query operators, providing performance benefits over the use of Observable.Create.
  23. /// </summary>
  24. /// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
  25. internal abstract class Producer<TSource> : IProducer<TSource>
  26. {
  27. /// <summary>
  28. /// Publicly visible Subscribe method.
  29. /// </summary>
  30. /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
  31. /// <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>
  32. public IDisposable Subscribe(IObserver<TSource> observer)
  33. {
  34. if (observer == null)
  35. throw new ArgumentNullException("observer");
  36. return SubscribeRaw(observer, true);
  37. }
  38. public IDisposable SubscribeRaw(IObserver<TSource> observer, bool enableSafeguard)
  39. {
  40. var state = new State();
  41. state.observer = observer;
  42. state.sink = new SingleAssignmentDisposable();
  43. state.subscription = new SingleAssignmentDisposable();
  44. var d = StableCompositeDisposable.Create(state.sink, state.subscription);
  45. //
  46. // See AutoDetachObserver.cs for more information on the safeguarding requirement and
  47. // its implementation aspects.
  48. //
  49. if (enableSafeguard)
  50. {
  51. state.observer = SafeObserver<TSource>.Create(state.observer, d);
  52. }
  53. if (CurrentThreadScheduler.IsScheduleRequired)
  54. {
  55. CurrentThreadScheduler.Instance.Schedule(state, Run);
  56. }
  57. else
  58. {
  59. state.subscription.Disposable = this.Run(state.observer, state.subscription, state.Assign);
  60. }
  61. return d;
  62. }
  63. struct State
  64. {
  65. public SingleAssignmentDisposable sink;
  66. public SingleAssignmentDisposable subscription;
  67. public IObserver<TSource> observer;
  68. public void Assign(IDisposable s)
  69. {
  70. sink.Disposable = s;
  71. }
  72. }
  73. private IDisposable Run(IScheduler _, State x)
  74. {
  75. x.subscription.Disposable = this.Run(x.observer, x.subscription, x.Assign);
  76. return Disposable.Empty;
  77. }
  78. /// <summary>
  79. /// Core implementation of the query operator, called upon a new subscription to the producer object.
  80. /// </summary>
  81. /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
  82. /// <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>
  83. /// <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>
  84. /// <returns>Disposable representing all the resources and/or subscriptions the operator uses to process events.</returns>
  85. /// <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>
  86. protected abstract IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink);
  87. }
  88. }
  89. #endif