Skip to content

Commit

Permalink
Dispose in reverse order.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCleary committed Mar 3, 2023
1 parent 5f398a6 commit 3e29a26
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Nito.Disposables/AsyncDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ protected override ValueTask DisposeAsync(Func<ValueTask>? context)
if (handlers.Length == 1)
return context();

return DoDisposeAsync(handlers);
return DoDisposeAsync(handlers.Reverse().Cast<Func<ValueTask>>());
}

private async ValueTask DoDisposeAsync(IReadOnlyList<Delegate> handlers)
private async ValueTask DoDisposeAsync(IEnumerable<Func<ValueTask>> handlers)
{
if ((_flags & AsyncDisposeFlags.ExecuteConcurrently) != AsyncDisposeFlags.ExecuteConcurrently)
{
foreach (var handler in handlers)
await ((Func<ValueTask>) handler)().ConfigureAwait(false);
await handler().ConfigureAwait(false);
}
else
{
var tasks = handlers.Select(handler => ((Func<ValueTask>) handler)().AsTask()).ToList();
var tasks = handlers.Select(handler => handler().AsTask()).ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nito.Disposables/CollectionAsyncDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ protected override async ValueTask DisposeAsync(ImmutableQueue<IAsyncDisposable>
{
if ((_flags & AsyncDisposeFlags.ExecuteConcurrently) != AsyncDisposeFlags.ExecuteConcurrently)
{
foreach (var disposable in context)
foreach (var disposable in context.Reverse())
await disposable.DisposeAsync().ConfigureAwait(false);
}
else
{
var tasks = context.Select(disposable => disposable.DisposeAsync().AsTask()).ToList();
var tasks = context.Reverse().Select(disposable => disposable.DisposeAsync().AsTask()).ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nito.Disposables/CollectionDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CollectionDisposable(IEnumerable<IDisposable?> disposables)
/// <inheritdoc />
protected override void Dispose(ImmutableQueue<IDisposable> context)
{
foreach (var disposable in context)
foreach (var disposable in context.Reverse())
disposable?.Dispose();
}

Expand Down
9 changes: 8 additions & 1 deletion src/Nito.Disposables/Disposable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Nito.Disposables
{
Expand All @@ -17,7 +18,13 @@ public Disposable(Action? dispose)
}

/// <inheritdoc />
protected override void Dispose(Action? context) => context?.Invoke();
protected override void Dispose(Action? context)
{
if (context == null)
return;
foreach (var handler in context.GetInvocationList().Reverse().Cast<Action>())
handler();
}

/// <summary>
/// Adds a delegate to be executed when this instance is disposed. If this instance is already disposed or disposing, then <paramref name="dispose"/> is executed immediately.
Expand Down

0 comments on commit 3e29a26

Please sign in to comment.