AddRef.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. using System.Reactive.Disposables;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. internal class AddRef<TSource> : Producer<TSource, AddRef<TSource>._>
  8. {
  9. private readonly IObservable<TSource> _source;
  10. private readonly RefCountDisposable _refCount;
  11. public AddRef(IObservable<TSource> source, RefCountDisposable refCount)
  12. {
  13. _source = source;
  14. _refCount = refCount;
  15. }
  16. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer, _refCount.GetDisposable());
  17. protected override void Run(_ sink) => sink.Run(_source);
  18. internal sealed class _ : IdentitySink<TSource>
  19. {
  20. private readonly IDisposable _refCountDisposable;
  21. public _(IObserver<TSource> observer, IDisposable refCountDisposable)
  22. : base(observer)
  23. {
  24. _refCountDisposable = refCountDisposable;
  25. }
  26. protected override void Dispose(bool disposing)
  27. {
  28. if (disposing)
  29. {
  30. _refCountDisposable.Dispose();
  31. }
  32. base.Dispose(disposing);
  33. }
  34. }
  35. }
  36. }