Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds StartAsync with return type #7

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion Kurukuru/Spinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Kurukuru
{
public class Spinner : IDisposable
{
private CancellationTokenSource _cancellationTokenSource;
private readonly CancellationTokenSource _cancellationTokenSource;
private Task _task;
private Pattern _pattern;
private Pattern _fallbackPattern;
Expand Down Expand Up @@ -198,5 +198,37 @@ public static async Task StartAsync(string text, Func<Spinner, Task> action, Pat
}
}
}

public static Task<TResult> StartAsync<TResult>(string text, Func<Task<TResult>> action, Pattern pattern = null, Pattern fallbackPattern = null)
{
return StartAsync(text, _ => action(), pattern, fallbackPattern);
}

public static async Task<TResult> StartAsync<TResult>(string text, Func<Spinner, Task<TResult>> action, Pattern pattern = null, Pattern fallbackPattern = null)
{
using (var spinner = new Spinner(text, pattern, fallbackPattern: fallbackPattern))
{
spinner.Start();

try
{
var result = await action(spinner);
if (!spinner.Stopped)
{
spinner.Succeed();
}

return result;
}
catch
{
if (!spinner.Stopped)
{
spinner.Fail();
}
throw;
}
}
}
}
}