Using.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 sealed class Using<TSource, TResource> : Producer<TSource, Using<TSource, TResource>._>
  8. where TResource : IDisposable
  9. {
  10. private readonly Func<TResource> _resourceFactory;
  11. private readonly Func<TResource, IObservable<TSource>> _observableFactory;
  12. public Using(Func<TResource> resourceFactory, Func<TResource, IObservable<TSource>> observableFactory)
  13. {
  14. _resourceFactory = resourceFactory;
  15. _observableFactory = observableFactory;
  16. }
  17. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  18. protected override void Run(_ sink) => sink.Run(this);
  19. internal sealed class _ : IdentitySink<TSource>
  20. {
  21. public _(IObserver<TSource> observer)
  22. : base(observer)
  23. {
  24. }
  25. private IDisposable _disposable;
  26. public void Run(Using<TSource, TResource> parent)
  27. {
  28. IObservable<TSource> source;
  29. var disposable = Disposable.Empty;
  30. try
  31. {
  32. var resource = parent._resourceFactory();
  33. if (resource != null)
  34. {
  35. disposable = resource;
  36. }
  37. source = parent._observableFactory(resource);
  38. }
  39. catch (Exception exception)
  40. {
  41. source = Observable.Throw<TSource>(exception);
  42. }
  43. // It is important to set the disposable resource after
  44. // Run(). In the synchronous case this would else dispose
  45. // the the resource before the source subscription.
  46. Run(source);
  47. Disposable.SetSingle(ref _disposable, disposable);
  48. }
  49. protected override void Dispose(bool disposing)
  50. {
  51. base.Dispose(disposing);
  52. if (disposing)
  53. {
  54. Disposable.TryDispose(ref _disposable);
  55. }
  56. }
  57. }
  58. }
  59. }