<PackageReference Include="System.Reactive" Version="6.0.1" />

Using<TSource, TResource>

sealed class Using<TSource, TResource> : Producer<TSource, _<TSource, TResource>> where TResource : IDisposable
using System.Reactive.Disposables; using System.Runtime.CompilerServices; namespace System.Reactive.Linq.ObservableImpl { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 1, 1, 1 })] internal sealed class Using<[System.Runtime.CompilerServices.Nullable(2)] TSource, [System.Runtime.CompilerServices.Nullable(0)] TResource> : Producer<TSource, Using<TSource, TResource>._> where TResource : IDisposable { [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] internal sealed class _ : IdentitySink<TSource> { private SingleAssignmentDisposableValue _disposable; public _(IObserver<TSource> observer) : base(observer) { } public void Run(Using<TSource, TResource> parent) { IDisposable disposable = Disposable.Empty; IObservable<TSource> source; try { TResource val = parent._resourceFactory(); if (val != null) disposable = (IDisposable)(object)val; source = parent._observableFactory(val); } catch (Exception exception) { source = Observable.Throw<TSource>(exception); } Run(source); _disposable.Disposable = disposable; } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) _disposable.Dispose(); } } private readonly Func<TResource> _resourceFactory; private readonly Func<TResource, IObservable<TSource>> _observableFactory; public Using(Func<TResource> resourceFactory, Func<TResource, IObservable<TSource>> observableFactory) { _resourceFactory = resourceFactory; _observableFactory = observableFactory; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 0 })] protected override _ CreateSink(IObserver<TSource> observer) { return new _(observer); } protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 0 })] _ sink) { sink.Run(this); } } }