| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Reactive.Disposables
- {
- public sealed class CompositeAsyncDisposable : IAsyncDisposable
- {
- private readonly AsyncLock _gate = new AsyncLock();
- private readonly List<IAsyncDisposable> _disposables = new List<IAsyncDisposable>();
- private bool _disposed;
- public async Task AddAsync(IAsyncDisposable disposable)
- {
- if (disposable == null)
- throw new ArgumentNullException(nameof(disposable));
- var shouldDispose = false;
- using (await _gate.LockAsync().ConfigureAwait(false))
- {
- if (_disposed)
- {
- shouldDispose = true;
- }
- else
- {
- _disposables.Add(disposable);
- }
- }
- if (shouldDispose)
- {
- await disposable.DisposeAsync().ConfigureAwait(false);
- }
- }
- public async Task<bool> RemoveAsync(IAsyncDisposable disposable)
- {
- if (disposable == null)
- throw new ArgumentNullException(nameof(disposable));
- var shouldDispose = false;
- using (await _gate.LockAsync().ConfigureAwait(false))
- {
- if (!_disposed && _disposables.Remove(disposable))
- {
- shouldDispose = true;
- }
- }
- if (shouldDispose)
- {
- await disposable.DisposeAsync().ConfigureAwait(false);
- }
- return shouldDispose;
- }
- public async Task DisposeAsync()
- {
- var disposables = default(IAsyncDisposable[]);
- using (await _gate.LockAsync().ConfigureAwait(false))
- {
- if (!_disposed)
- {
- _disposed = true;
- disposables = _disposables.ToArray();
- _disposables.Clear();
- }
- }
- if (disposables != null)
- {
- var tasks = disposables.Select(disposable => disposable.DisposeAsync());
- await Task.WhenAll(tasks);
- }
- }
- }
- }
|