AddRef.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. using System.Reactive.Disposables;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. class AddRef<TSource> : Producer<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 IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var d = StableCompositeDisposable.Create(_refCount.GetDisposable(), cancel);
  19. var sink = new _(observer, d);
  20. setSink(sink);
  21. return _source.SubscribeSafe(sink);
  22. }
  23. class _ : Sink<TSource>, IObserver<TSource>
  24. {
  25. public _(IObserver<TSource> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. }
  29. public void OnNext(TSource value)
  30. {
  31. base._observer.OnNext(value);
  32. }
  33. public void OnError(Exception error)
  34. {
  35. base._observer.OnError(error);
  36. base.Dispose();
  37. }
  38. public void OnCompleted()
  39. {
  40. base._observer.OnCompleted();
  41. base.Dispose();
  42. }
  43. }
  44. }
  45. }
  46. #endif