From f9f3090a7ab4d92838ff2313af04ac4cf92a8f61 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Thu, 14 Nov 2024 01:28:12 +0000 Subject: [PATCH 1/2] Feature Add DisposeWith Provide an extension to allow Disposables to be added to a collection of Disposables in a Fluent manner. --- .../Disposables/DisposableMixins.cs | 35 +++++++++++++++++++ .../Tests/Disposables/DisposableTests.cs | 13 +++++++ 2 files changed, 48 insertions(+) create mode 100644 Rx.NET/Source/src/System.Reactive/Disposables/DisposableMixins.cs diff --git a/Rx.NET/Source/src/System.Reactive/Disposables/DisposableMixins.cs b/Rx.NET/Source/src/System.Reactive/Disposables/DisposableMixins.cs new file mode 100644 index 000000000..e5b972f4e --- /dev/null +++ b/Rx.NET/Source/src/System.Reactive/Disposables/DisposableMixins.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT License. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace System.Reactive.Disposables; + +/// +/// Extension methods associated with the IDisposable interface. +/// +public static class DisposableMixins +{ + /// + /// Ensures the provided disposable is disposed with the specified ICollection of IDisposable./>. + /// + /// The type of the disposable. + /// The disposable we are going to want to be disposed by the disposable collection. + /// The composite disposable. + /// + /// The disposable. + /// + /// compositeDisposable + public static T DisposeWith(this T item, ICollection disposableCollection) + where T : IDisposable + { + if (disposableCollection == null) + { + throw new ArgumentNullException(nameof(disposableCollection)); + } + + disposableCollection.Add(item); + return item; + } +} diff --git a/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs b/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs index 2c6bee05d..6f27fb1c5 100644 --- a/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs +++ b/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs @@ -415,6 +415,19 @@ public void CompositeDisposable_Empty_GetEnumerator() Assert.False(composite.GetEnumerator().MoveNext()); } + [TestMethod] + public void CompositeDisposable_DisposeWith() + { + var c = new CompositeDisposable(); + var d = new BooleanDisposable(); + d.DisposeWith(c); + Assert.True(c.Contains(d)); + + c.Dispose(); + Assert.True(d.IsDisposed); + Assert.True(c.IsDisposed); + } + [TestMethod] public void CompositeDisposable_NonCollection_Enumerable_Init() { From cccfded8c0669a1a994725735482a2adc160a68a Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Mon, 18 Nov 2024 23:59:08 +0000 Subject: [PATCH 2/2] Update API test --- .../Api/ApiApprovalTests.Core.verified.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs b/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs index 6e3936ec8..3c802fff6 100644 --- a/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs +++ b/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs @@ -600,6 +600,11 @@ public static class Disposable public static System.IDisposable Create(System.Action dispose) { } public static System.IDisposable Create(TState state, System.Action dispose) { } } + public static class DisposableMixins + { + public static T DisposeWith(this T item, System.Collections.Generic.ICollection disposableCollection) + where T : System.IDisposable { } + } public interface ICancelable : System.IDisposable { bool IsDisposed { get; }