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

AutoDetachObserver<T>

using System.Reactive.Disposables; using System.Runtime.CompilerServices; namespace System.Reactive { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] internal sealed class AutoDetachObserver<[System.Runtime.CompilerServices.Nullable(2)] T> : ObserverBase<T>, ISafeObserver<T>, IObserver<T>, IDisposable { private readonly IObserver<T> _observer; private SingleAssignmentDisposableValue _disposable; public AutoDetachObserver(IObserver<T> observer) { _observer = observer; } public void SetResource(IDisposable resource) { _disposable.Disposable = resource; } protected override void OnNextCore(T value) { bool flag = false; try { _observer.OnNext(value); flag = true; } finally { if (!flag) Dispose(); } } protected override void OnErrorCore(Exception exception) { try { _observer.OnError(exception); } finally { Dispose(); } } protected override void OnCompletedCore() { try { _observer.OnCompleted(); } finally { Dispose(); } } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) _disposable.Dispose(); } } }