Using.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //
  38. // NB: We do allow the factory to return `null`, similar to the `using` statement in C#. However, we don't want to bother
  39. // users with a TResource? parameter and cause a breaking change to their code, even if their factory returns non-null.
  40. // Right now, we can't track non-null state across the invocation of resourceFactory into observableFactory. If we'd
  41. // be able to do that, it would make sense to warn users about a possible null. In the absence of this, we'd end up
  42. // with a lot of false positives (in fact, most code would cause a warning), and force users to pollute their code with
  43. // the "damn-it" ! operator.
  44. //
  45. source = parent._observableFactory(resource!);
  46. }
  47. catch (Exception exception)
  48. {
  49. source = Observable.Throw<TSource>(exception);
  50. }
  51. // It is important to set the disposable resource after
  52. // Run(). In the synchronous case this would else dispose
  53. // the the resource before the source subscription.
  54. Run(source);
  55. Disposable.SetSingle(ref _disposable, disposable);
  56. }
  57. protected override void Dispose(bool disposing)
  58. {
  59. base.Dispose(disposing);
  60. if (disposing)
  61. {
  62. Disposable.Dispose(ref _disposable);
  63. }
  64. }
  65. }
  66. }
  67. }