-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace R3; | ||
|
||
public struct DisposableBag : IDisposable | ||
{ | ||
IDisposable[]? items; | ||
bool isDisposed; | ||
int count; | ||
|
||
public DisposableBag(int capacity) | ||
{ | ||
items = new IDisposable[capacity]; | ||
} | ||
|
||
public void Add(IDisposable item) | ||
{ | ||
if (isDisposed) | ||
{ | ||
item.Dispose(); | ||
return; | ||
} | ||
|
||
if (items == null) | ||
{ | ||
items = new IDisposable[4]; | ||
} | ||
else if (count == items.Length) | ||
{ | ||
Array.Resize(ref items, count * 2); | ||
} | ||
|
||
items[count++] = item; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
if (items != null) | ||
{ | ||
for (int i = 0; i < count; i++) | ||
{ | ||
items[i]?.Dispose(); | ||
} | ||
|
||
items = null; | ||
count = 0; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Clear(); | ||
isDisposed = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace R3.Tests; | ||
|
||
public class DisposableBagTest | ||
{ | ||
[Fact] | ||
public void Test() | ||
{ | ||
var bag = new DisposableBag(); | ||
|
||
var disposed = new bool[6]; | ||
|
||
var subject = new Subject<int>(); | ||
subject.Do(onDispose: () => disposed[0] = true).Subscribe().AddTo(ref bag); | ||
subject.Do(onDispose: () => disposed[1] = true).Subscribe().AddTo(ref bag); | ||
subject.Do(onDispose: () => disposed[2] = true).Subscribe().AddTo(ref bag); | ||
subject.Do(onDispose: () => disposed[3] = true).Subscribe().AddTo(ref bag); | ||
subject.Do(onDispose: () => disposed[4] = true).Subscribe().AddTo(ref bag); | ||
subject.Do(onDispose: () => disposed[5] = true).Subscribe().AddTo(ref bag); | ||
|
||
disposed.Should().Equal([false, false, false, false, false, false]); | ||
|
||
bag.Dispose(); | ||
|
||
disposed.Should().Equal([true, true, true, true, true, true]); | ||
} | ||
} |