Skip to content

Commit fda1bbc

Browse files
authored
Enhancement Add DisposeWith (#2178)
Provide an extension to allow Disposables to be added to a collection of Disposables in a Fluent manner.
1 parent e59f6db commit fda1bbc

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
5+
namespace System.Reactive.Disposables.Fluent;
6+
7+
/// <summary>
8+
/// Extension methods associated with the IDisposable interface.
9+
/// </summary>
10+
public static class DisposableExtensions
11+
{
12+
/// <summary>
13+
/// Ensures the provided disposable is disposed with the specified <see cref="CompositeDisposable"/>.
14+
/// </summary>
15+
/// <typeparam name="T">
16+
/// The type of the disposable.
17+
/// </typeparam>
18+
/// <param name="item">
19+
/// The disposable we are going to want to be disposed by the CompositeDisposable.
20+
/// </param>
21+
/// <param name="compositeDisposable">
22+
/// The <see cref="CompositeDisposable"/> to which <paramref name="item"/> will be added.
23+
/// </param>
24+
/// <returns>
25+
/// The disposable.
26+
/// </returns>
27+
public static T DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)
28+
where T : IDisposable
29+
{
30+
if (compositeDisposable == null)
31+
{
32+
throw new ArgumentNullException(nameof(compositeDisposable));
33+
}
34+
35+
compositeDisposable.Add(item);
36+
return item;
37+
}
38+
}

Rx.NET/Source/src/System.Reactive/System.Reactive.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
</ItemGroup>
155155

156156
<ItemGroup>
157-
<None Include="build\NuGet.Readme.md" Pack="true" PackagePath="\readme.md"/>
157+
<None Include="build\NuGet.Readme.md" Pack="true" PackagePath="\readme.md" />
158158
<None Include="build\_._" PackagePath="build\net6.0;build\net6.0-windows10.0.19041" Pack="true" />
159159
<None Include="build\_._" PackagePath="buildTransitive\net6.0;buildTransitive\net6.0-windows10.0.19041" Pack="true" />
160160
<None Include="Linq\QbservableEx.NAry.cs">

Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,14 @@ public static System.Reactive.Disposables.ICancelable Create(params System.IDisp
661661
public static System.Reactive.Disposables.ICancelable Create(System.IDisposable disposable1, System.IDisposable disposable2) { }
662662
}
663663
}
664+
namespace System.Reactive.Disposables.Fluent
665+
{
666+
public static class DisposableExtensions
667+
{
668+
public static T DisposeWith<T>(this T item, System.Reactive.Disposables.CompositeDisposable compositeDisposable)
669+
where T : System.IDisposable { }
670+
}
671+
}
664672
namespace System.Reactive.Joins
665673
{
666674
public abstract class Pattern { }
@@ -3190,4 +3198,4 @@ public void Start<TStateMachine>(ref TStateMachine stateMachine)
31903198
where TStateMachine : System.Runtime.CompilerServices.IAsyncStateMachine { }
31913199
public static System.Runtime.CompilerServices.TaskObservableMethodBuilder<T> Create() { }
31923200
}
3193-
}
3201+
}

Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Reactive.Concurrency;
1010
using System.Reactive.Disposables;
11+
using System.Reactive.Disposables.Fluent;
1112
using System.Threading;
1213
using Microsoft.Reactive.Testing;
1314
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -415,6 +416,19 @@ public void CompositeDisposable_Empty_GetEnumerator()
415416
Assert.False(composite.GetEnumerator().MoveNext());
416417
}
417418

419+
[TestMethod]
420+
public void CompositeDisposable_DisposeWith()
421+
{
422+
var c = new CompositeDisposable();
423+
var d = new BooleanDisposable();
424+
d.DisposeWith(c);
425+
Assert.True(c.Contains(d));
426+
427+
c.Dispose();
428+
Assert.True(d.IsDisposed);
429+
Assert.True(c.IsDisposed);
430+
}
431+
418432
[TestMethod]
419433
public void CompositeDisposable_NonCollection_Enumerable_Init()
420434
{

0 commit comments

Comments
 (0)