From a8c2f547754ca2c3c26f1282eef6366fc9ce071d Mon Sep 17 00:00:00 2001 From: Thomas Freudenberg Date: Mon, 9 Nov 2020 23:01:30 +0100 Subject: [PATCH] adds StartAsync with return type --- Kurukuru/Spinner.cs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Kurukuru/Spinner.cs b/Kurukuru/Spinner.cs index 5cef4dd..2e25933 100644 --- a/Kurukuru/Spinner.cs +++ b/Kurukuru/Spinner.cs @@ -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; @@ -198,5 +198,37 @@ public static async Task StartAsync(string text, Func action, Pat } } } + + public static Task StartAsync(string text, Func> action, Pattern pattern = null, Pattern fallbackPattern = null) + { + return StartAsync(text, _ => action(), pattern, fallbackPattern); + } + + public static async Task StartAsync(string text, Func> 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; + } + } + } } }