Skip to content

Commit

Permalink
feat: Add aggregate preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
angelobreuer committed Jan 21, 2024
1 parent 073b429 commit 9774152
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Lavalink4NET/Players/Preconditions/AggregateAllPrecondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Lavalink4NET.Players.Preconditions;

using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;

internal sealed class AggregateAllPrecondition(ImmutableArray<IPlayerPrecondition> Preconditions) : IPlayerPrecondition
{
public async ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

foreach (var precondition in Preconditions)
{
if (!await precondition.CheckAsync(player, cancellationToken).ConfigureAwait(false))
{
return false;
}
}

return true;
}
}
25 changes: 25 additions & 0 deletions src/Lavalink4NET/Players/Preconditions/AggregateAnyPrecondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Lavalink4NET.Players.Preconditions;

using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;

internal sealed class AggregateAnyPrecondition(ImmutableArray<IPlayerPrecondition> Preconditions) : IPlayerPrecondition
{
public async ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

foreach (var precondition in Preconditions)
{
if (await precondition.CheckAsync(player, cancellationToken).ConfigureAwait(false))
{
return true;
}
}

return false;
}
}
14 changes: 14 additions & 0 deletions src/Lavalink4NET/Players/Preconditions/PlayerPrecondition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Lavalink4NET.Players.Preconditions;

using System.Collections.Immutable;

public static class PlayerPrecondition
{
public static IPlayerPrecondition Playing { get; } = new SimplePrecondition(PlayerState.Playing);
Expand All @@ -17,4 +19,16 @@ public static class PlayerPrecondition
public static IPlayerPrecondition HistoryEmpty { get; } = new HistoryEmptyPrecondition();

public static IPlayerPrecondition HistoryNotEmpty { get; } = new HistoryNotEmptyPrecondition();

public static IPlayerPrecondition Any(ImmutableArray<IPlayerPrecondition> preconditions)
=> new AggregateAnyPrecondition(preconditions);

public static IPlayerPrecondition Any(params IPlayerPrecondition[] preconditions)
=> new AggregateAnyPrecondition(preconditions.ToImmutableArray());

public static IPlayerPrecondition All(ImmutableArray<IPlayerPrecondition> preconditions)
=> new AggregateAllPrecondition(preconditions);

public static IPlayerPrecondition All(params IPlayerPrecondition[] preconditions)
=> new AggregateAllPrecondition(preconditions.ToImmutableArray());
}

0 comments on commit 9774152

Please sign in to comment.