CompositeAsyncDisposable.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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 AsyncLock _gate = new AsyncLock();
  13. private readonly List<IAsyncDisposable> _disposables = new List<IAsyncDisposable>();
  14. private bool _disposed;
  15. public async Task AddAsync(IAsyncDisposable disposable)
  16. {
  17. if (disposable == null)
  18. throw new ArgumentNullException(nameof(disposable));
  19. var shouldDispose = false;
  20. using (await _gate.LockAsync().ConfigureAwait(false))
  21. {
  22. if (_disposed)
  23. {
  24. shouldDispose = true;
  25. }
  26. else
  27. {
  28. _disposables.Add(disposable);
  29. }
  30. }
  31. if (shouldDispose)
  32. {
  33. await disposable.DisposeAsync().ConfigureAwait(false);
  34. }
  35. }
  36. public async Task<bool> RemoveAsync(IAsyncDisposable disposable)
  37. {
  38. if (disposable == null)
  39. throw new ArgumentNullException(nameof(disposable));
  40. var shouldDispose = false;
  41. using (await _gate.LockAsync().ConfigureAwait(false))
  42. {
  43. if (!_disposed && _disposables.Remove(disposable))
  44. {
  45. shouldDispose = true;
  46. }
  47. }
  48. if (shouldDispose)
  49. {
  50. await disposable.DisposeAsync().ConfigureAwait(false);
  51. }
  52. return shouldDispose;
  53. }
  54. public async Task DisposeAsync()
  55. {
  56. var disposables = default(IAsyncDisposable[]);
  57. using (await _gate.LockAsync().ConfigureAwait(false))
  58. {
  59. if (!_disposed)
  60. {
  61. _disposed = true;
  62. disposables = _disposables.ToArray();
  63. _disposables.Clear();
  64. }
  65. }
  66. if (disposables != null)
  67. {
  68. var tasks = disposables.Select(disposable => disposable.DisposeAsync());
  69. await Task.WhenAll(tasks);
  70. }
  71. }
  72. }
  73. }