CompositeAsyncDisposable.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Disposables
  9. {
  10. public sealed class CompositeAsyncDisposable : IAsyncDisposable
  11. {
  12. private readonly AsyncGate _gate = new();
  13. private readonly List<IAsyncDisposable> _disposables;
  14. private bool _disposed;
  15. public CompositeAsyncDisposable()
  16. {
  17. _disposables = new List<IAsyncDisposable>();
  18. }
  19. public CompositeAsyncDisposable(params IAsyncDisposable[] disposables)
  20. {
  21. if (disposables == null)
  22. throw new ArgumentNullException(nameof(disposables));
  23. _disposables = new List<IAsyncDisposable>(disposables);
  24. }
  25. public CompositeAsyncDisposable(IEnumerable<IAsyncDisposable> disposables)
  26. {
  27. if (disposables == null)
  28. throw new ArgumentNullException(nameof(disposables));
  29. _disposables = new List<IAsyncDisposable>(disposables);
  30. }
  31. public async ValueTask AddAsync(IAsyncDisposable disposable)
  32. {
  33. if (disposable == null)
  34. throw new ArgumentNullException(nameof(disposable));
  35. var shouldDispose = false;
  36. using (await _gate.LockAsync().ConfigureAwait(false))
  37. {
  38. if (_disposed)
  39. {
  40. shouldDispose = true;
  41. }
  42. else
  43. {
  44. _disposables.Add(disposable);
  45. }
  46. }
  47. if (shouldDispose)
  48. {
  49. await disposable.DisposeAsync().ConfigureAwait(false);
  50. }
  51. }
  52. public async ValueTask<bool> RemoveAsync(IAsyncDisposable disposable)
  53. {
  54. if (disposable == null)
  55. throw new ArgumentNullException(nameof(disposable));
  56. var shouldDispose = false;
  57. using (await _gate.LockAsync().ConfigureAwait(false))
  58. {
  59. if (!_disposed && _disposables.Remove(disposable))
  60. {
  61. shouldDispose = true;
  62. }
  63. }
  64. if (shouldDispose)
  65. {
  66. await disposable.DisposeAsync().ConfigureAwait(false);
  67. }
  68. return shouldDispose;
  69. }
  70. public async ValueTask DisposeAsync()
  71. {
  72. var disposables = default(IAsyncDisposable[]);
  73. using (await _gate.LockAsync().ConfigureAwait(false))
  74. {
  75. if (!_disposed)
  76. {
  77. _disposed = true;
  78. disposables = _disposables.ToArray();
  79. _disposables.Clear();
  80. }
  81. }
  82. if (disposables != null)
  83. {
  84. var tasks = disposables.Select(disposable => disposable.DisposeAsync().AsTask());
  85. await Task.WhenAll(tasks);
  86. }
  87. }
  88. }
  89. }