Skip to content

Commit

Permalink
Merge pull request #7 from thoemmi/start-async-with-return-type
Browse files Browse the repository at this point in the history
adds StartAsync with return type
  • Loading branch information
mayuki authored Nov 10, 2020
2 parents 059dae1 + a8c2f54 commit 5b4f65d
Showing 1 changed file with 33 additions and 1 deletion.
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;
}
}
}
}
}

0 comments on commit 5b4f65d

Please sign in to comment.