Sink.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #if !NO_PERF
  2. using System.Threading;
  3. namespace System.Reactive
  4. {
  5. /// <summary>
  6. /// Base class for implementation of query operators, providing a lightweight sink that can be disposed to mute the outgoing observer.
  7. /// </summary>
  8. /// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
  9. /// <remarks>Implementations of sinks are responsible to enforce the message grammar on the associated observer. Upon sending a terminal message, a pairing Dispose call should be made to trigger cancellation of related resources and to mute the outgoing observer.</remarks>
  10. abstract class Sink<TSource> : IDisposable
  11. {
  12. protected volatile IObserver<TSource> _observer;
  13. private IDisposable _cancel;
  14. public Sink(IObserver<TSource> observer, IDisposable cancel)
  15. {
  16. _observer = observer;
  17. _cancel = cancel;
  18. }
  19. public virtual void Dispose()
  20. {
  21. _observer = NopObserver<TSource>.Instance;
  22. var cancel = Interlocked.Exchange(ref _cancel, null);
  23. if (cancel != null)
  24. {
  25. cancel.Dispose();
  26. }
  27. }
  28. public IObserver<TSource> GetForwarder()
  29. {
  30. return new _(this);
  31. }
  32. class _ : IObserver<TSource>
  33. {
  34. private readonly Sink<TSource> _forward;
  35. public _(Sink<TSource> forward)
  36. {
  37. _forward = forward;
  38. }
  39. public void OnNext(TSource value)
  40. {
  41. _forward._observer.OnNext(value);
  42. }
  43. public void OnError(Exception error)
  44. {
  45. _forward._observer.OnError(error);
  46. _forward.Dispose();
  47. }
  48. public void OnCompleted()
  49. {
  50. _forward._observer.OnCompleted();
  51. _forward.Dispose();
  52. }
  53. }
  54. }
  55. }
  56. #endif