Skip to content

Commit

Permalink
feat: Add inline preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
angelobreuer committed Jan 21, 2024
1 parent 950a87d commit 351f1b6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Lavalink4NET.Players.Preconditions;

using System;
using System.Threading;
using System.Threading.Tasks;

internal sealed record class InlineAsynchronousPrecondition(Func<ILavalinkPlayer, CancellationToken, ValueTask<bool>> Precondition) : IPlayerPrecondition
{
public ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

return Precondition(player, cancellationToken);
}
}

internal sealed record class InlineAsynchronousPrecondition<TPlayer>(Func<TPlayer, CancellationToken, ValueTask<bool>> Precondition) : IPlayerPrecondition where TPlayer : ILavalinkPlayer
{
public async ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

return player is TPlayer specializedPlayer && await Precondition(specializedPlayer, cancellationToken).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Lavalink4NET.Players.Preconditions;

using System;
using System.Threading;
using System.Threading.Tasks;

internal sealed record class InlineSynchronousPrecondition(Func<ILavalinkPlayer, bool> Precondition) : IPlayerPrecondition
{
public ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

return new ValueTask<bool>(Precondition(player));
}
}

internal sealed record class InlineSynchronousPrecondition<TPlayer>(Func<TPlayer, bool> Precondition) : IPlayerPrecondition where TPlayer : ILavalinkPlayer
{
public ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

return new ValueTask<bool>(player is TPlayer specializedPlayer && Precondition(specializedPlayer));
}
}
17 changes: 17 additions & 0 deletions src/Lavalink4NET/Players/Preconditions/PlayerPrecondition.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace Lavalink4NET.Players.Preconditions;

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

public static class PlayerPrecondition
{
Expand Down Expand Up @@ -37,4 +40,18 @@ public static IPlayerPrecondition Status(ImmutableArray<PlayerState> states)

public static IPlayerPrecondition Status(params PlayerState[] states)
=> new PlayerStatePrecondition(states.ToImmutableArray());

public static IPlayerPrecondition Create(Func<ILavalinkPlayer, CancellationToken, ValueTask<bool>> precondition)
=> new InlineAsynchronousPrecondition(precondition);

public static IPlayerPrecondition Create<TPlayer>(Func<TPlayer, CancellationToken, ValueTask<bool>> precondition)
where TPlayer : ILavalinkPlayer
=> new InlineAsynchronousPrecondition<TPlayer>(precondition);

public static IPlayerPrecondition Create(Func<ILavalinkPlayer, bool> precondition)
=> new InlineSynchronousPrecondition(precondition);

public static IPlayerPrecondition Create<TPlayer>(Func<TPlayer, bool> precondition)
where TPlayer : ILavalinkPlayer
=> new InlineSynchronousPrecondition<TPlayer>(precondition);
}

0 comments on commit 351f1b6

Please sign in to comment.