Skip to content

Commit

Permalink
add SingleEphemeral + breaking change: remove name from CleanupSelfAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Confusingboat committed Jun 2, 2023
1 parent 2850d95 commit daf74d1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Ephemerally.Azure.Cosmos/CosmosContainerEphemeral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public CosmosContainerEphemeral(
base(container, x => x.Id, options.OrDefault())
{ }

protected override Task CleanupSelfAsync(string fullName) =>
Value.Database.TryDeleteContainerAsync(fullName);
protected override Task CleanupSelfAsync() =>
Value.Database.TryDeleteContainerAsync(Metadata.FullName);

protected override Task CleanupAllAsync() =>
Value.Database.TryCleanupContainersAsync();
Expand Down
4 changes: 2 additions & 2 deletions src/Ephemerally.Azure.Cosmos/CosmosDatabaseEphemeral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public CosmosDatabaseEphemeral(
: base(database, x => x.Id, options.OrDefault())
{ }

protected override Task CleanupSelfAsync(string fullName) =>
Value.Client.TryDeleteDatabaseAsync(fullName);
protected override Task CleanupSelfAsync() =>
Value.Client.TryDeleteDatabaseAsync(Metadata.FullName);

protected override Task CleanupAllAsync() =>
Value.Client.TryCleanupDatabasesAsync();
Expand Down
4 changes: 2 additions & 2 deletions src/Ephemerally/Ephemeral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected Ephemeral(TValue value, Func<TValue,string> getFullName, EphemeralOpti
/// In an overridden implementation, this method should delete the TObject.
/// </summary>
/// <returns></returns>
protected abstract Task CleanupSelfAsync(string fullName);
protected abstract Task CleanupSelfAsync();

/// <summary>
/// In an overridden implementation, this method should delete all expired TObject.
Expand All @@ -37,7 +37,7 @@ public async ValueTask DisposeAsync()
{
if (_options.CleanupBehavior == CleanupBehavior.NoCleanup) return;

await CleanupSelfAsync(FullName).ConfigureAwait(false);
await CleanupSelfAsync().ConfigureAwait(false);
if (_options.CleanupBehavior == CleanupBehavior.SelfOnly) return;

await CleanupAllAsync().ConfigureAwait(false);
Expand Down
3 changes: 3 additions & 0 deletions src/Ephemerally/PublicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public static bool IsExpiredAsOf(this IEphemeralMetadata metadata, DateTimeOffse

public static IEphemeralMetadata GetContainerMetadata(this string fullName) =>
EphemeralMetadata.New(fullName);

public static IEphemeral<T> ToEphemeral<T>(this T value,
Func<Task> cleanupSelfAsync) where T : class => new SingleEphemeral<T>(value, cleanupSelfAsync);
}
24 changes: 24 additions & 0 deletions src/Ephemerally/SingleEphemeral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ephemerally;

public sealed class SingleEphemeral<T> : Ephemeral<T> where T : class
{
private readonly Func<Task> _cleanupSelfAsync;

public SingleEphemeral(
T value,
Func<Task> cleanupSelfAsync)
: base(value, x => string.Empty, new EphemeralOptions { CleanupBehavior = CleanupBehavior.SelfOnly })
{
_cleanupSelfAsync = cleanupSelfAsync;
}

protected override Task CleanupSelfAsync() => _cleanupSelfAsync();

protected override Task CleanupAllAsync() => Task.CompletedTask;
}

0 comments on commit daf74d1

Please sign in to comment.