Skip to content

Commit

Permalink
feat: add async overload for async disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnmoreels committed Jun 25, 2024
1 parent cb18747 commit 9960172
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Arcus.Testing.Core/AsyncDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ private AsyncDisposable(Func<ValueTask> disposeAsync)
/// <summary>
/// Creates an <see cref="AsyncDisposable"/> instance based on an existing synchronous <paramref name="disposable"/>.
/// </summary>
/// <param name="disposable">The synchronous disposable to create as an instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="disposable"/> is <c>null</c>.</exception>
public static AsyncDisposable Create(IDisposable disposable)
{
if (disposable is null)
Expand All @@ -31,20 +33,37 @@ public static AsyncDisposable Create(IDisposable disposable)
/// <summary>
/// Creates an <see cref="AsyncDisposable"/> instance based on an existing synchronous <paramref name="dispose"/> operation.
/// </summary>
/// <param name="dispose">The synchronous operation to create as an instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="dispose"/> is <c>null</c>.</exception>
public static AsyncDisposable Create(Action dispose)
{
if (dispose is null)
{
throw new ArgumentNullException(nameof(dispose));
}

return new AsyncDisposable(() =>
return Create(() =>
{
dispose();
return ValueTask.CompletedTask;
return Task.CompletedTask;
});
}

/// <summary>
/// Creates an <see cref="AsyncDisposable"/> instance based on an existing asynchronous <paramref name="disposeAsync"/> operation.
/// </summary>
/// <param name="disposeAsync">The asynchronous operation to create as an instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="disposeAsync"/> is <c>null</c>.</exception>
public static AsyncDisposable Create(Func<Task> disposeAsync)
{
if (disposeAsync is null)
{
throw new ArgumentNullException(nameof(disposeAsync));
}

return new AsyncDisposable(async () => await disposeAsync());
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
/// </summary>
Expand Down

0 comments on commit 9960172

Please sign in to comment.