Skip to content

Commit

Permalink
[MTGOSDK/Core/Reflection] DLRWrapper: Add TryUntil method for multipl…
Browse files Browse the repository at this point in the history
…e retry attempts
  • Loading branch information
Qonfused committed Dec 24, 2023
1 parent 08ee19d commit d75e815
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions MTGOSDK/src/Core/Reflection/DLRWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,30 @@ public static async Task<bool> WaitUntil(
return false;
}

/// <summary>
/// Safely executes a lambda function with a given number of retries.
/// </summary>
/// <param name="lambda">The function to execute.</param>
/// <param name="delay">The delay in ms between retries (optional).</param>
/// <param name="retries">The number of times to retry (optional).</param>
/// <returns>The result of the function, otherwise an exception is thrown.</returns>
public static async Task<dynamic> TryUntil(
Func<dynamic> lambda,
int delay = 250,
int retries = 20)
{
while (true)
{
try { return lambda(); }
catch
{
retries--;
if (retries <= 0) throw;
await Task.Delay(delay);
}
}
}

//
// Wrapper Attributes
//
Expand Down

0 comments on commit d75e815

Please sign in to comment.