RefCount.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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;
  6. using System.Reactive.Disposables;
  7. using System.Reactive.Subjects;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. class RefCount<TSource> : Producer<TSource>
  11. {
  12. private readonly IConnectableObservable<TSource> _source;
  13. private readonly object _gate;
  14. private int _count;
  15. private IDisposable _connectableSubscription;
  16. public RefCount(IConnectableObservable<TSource> source)
  17. {
  18. _source = source;
  19. _gate = new object();
  20. _count = 0;
  21. _connectableSubscription = default(IDisposable);
  22. }
  23. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  24. {
  25. var sink = new _(this, observer, cancel);
  26. setSink(sink);
  27. return sink.Run();
  28. }
  29. class _ : Sink<TSource>, IObserver<TSource>
  30. {
  31. private readonly RefCount<TSource> _parent;
  32. public _(RefCount<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  33. : base(observer, cancel)
  34. {
  35. _parent = parent;
  36. }
  37. public IDisposable Run()
  38. {
  39. var subscription = _parent._source.SubscribeSafe(this);
  40. lock (_parent._gate)
  41. {
  42. if (++_parent._count == 1)
  43. {
  44. _parent._connectableSubscription = _parent._source.Connect();
  45. }
  46. }
  47. return Disposable.Create(() =>
  48. {
  49. subscription.Dispose();
  50. lock (_parent._gate)
  51. {
  52. if (--_parent._count == 0)
  53. {
  54. _parent._connectableSubscription.Dispose();
  55. }
  56. }
  57. });
  58. }
  59. public void OnNext(TSource value)
  60. {
  61. base._observer.OnNext(value);
  62. }
  63. public void OnError(Exception error)
  64. {
  65. base._observer.OnError(error);
  66. base.Dispose();
  67. }
  68. public void OnCompleted()
  69. {
  70. base._observer.OnCompleted();
  71. base.Dispose();
  72. }
  73. }
  74. }
  75. }
  76. #endif