From 0fce6a333e09a8f3f1454184e53920ea65cd507e Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Sat, 25 Nov 2023 21:57:22 +0800 Subject: [PATCH] feature: Open collections of custom methods --- .../GeneralClientBootstrap.cs | 41 ++++++++++--------- .../Events/CommonArgs/ExceptionEventArgs.cs | 2 + .../PlatformWindows/WindowsStrategy.cs | 2 +- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs b/src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs index 9227e626..b20053c2 100644 --- a/src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs +++ b/src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs @@ -54,8 +54,7 @@ public override GeneralClientBootstrap LaunchAsync() private async Task BaseLaunch() { - //bool isSuccess = await ExecuteCustomOptions(); - //if (!isSuccess) return this; + await ExecuteCustomOptions(); var versionService = new VersionService(); var mainResp = await versionService.ValidationVersion(Packet.MainUpdateUrl); var upgradeResp = await versionService.ValidationVersion(Packet.UpdateUrl); @@ -161,11 +160,11 @@ public GeneralClientBootstrap SetCustomSkipOption(Func> func) /// /// /// - public GeneralClientBootstrap AddCustomOption(Func func) + public GeneralClientBootstrap AddCustomOption(List> funcs) { - if(func == null) throw new ArgumentNullException(nameof(func)); - _customOptions.Add(func); - return this; + if (funcs == null || !funcs.Any()) throw new ArgumentNullException(nameof(funcs)); + _customOptions.AddRange(funcs); + return this; } /// @@ -175,10 +174,10 @@ public GeneralClientBootstrap AddCustomOption(Func func) /// /// /// - public GeneralClientBootstrap AddCustomOption(Func> func) + public GeneralClientBootstrap AddCustomOption(List>> funcs) { - if (func == null) throw new ArgumentNullException(nameof(func)); - _customTaskOptions.Add(func); + if (funcs == null || !funcs.Any()) throw new ArgumentNullException(nameof(funcs)); + _customTaskOptions.AddRange(funcs); return this; } @@ -223,24 +222,26 @@ private async Task IsSkip(bool isForcibly) /// Performs all injected custom operations. /// /// - private async Task ExecuteCustomOptions() + private async Task ExecuteCustomOptions() { - if (_customOptions.Any()) - { - _customOptions.ForEach(option => option.Invoke()); - return true; - } - if (_customTaskOptions.Any()) { foreach (var option in _customTaskOptions) { - await option.Invoke(); + var isSuccess = await option.Invoke(); + if (!isSuccess) + Core.Events.EventManager.Instance.Dispatch>(this, new Core.Events.CommonArgs.ExceptionEventArgs($"{nameof(option)}Execution failure!")); + } + } + else if (_customOptions.Any()) + { + foreach (var option in _customOptions) + { + var isSuccess = option.Invoke(); + if (!isSuccess) + Core.Events.EventManager.Instance.Dispatch>(this, new Core.Events.CommonArgs.ExceptionEventArgs($"{nameof(option)}Execution failure!")); } - return true; } - - return true; } #endregion Private Methods diff --git a/src/c#/GeneralUpdate.Core/Events/CommonArgs/ExceptionEventArgs.cs b/src/c#/GeneralUpdate.Core/Events/CommonArgs/ExceptionEventArgs.cs index 919fae2e..eb845184 100644 --- a/src/c#/GeneralUpdate.Core/Events/CommonArgs/ExceptionEventArgs.cs +++ b/src/c#/GeneralUpdate.Core/Events/CommonArgs/ExceptionEventArgs.cs @@ -11,6 +11,8 @@ public ExceptionEventArgs(Exception exception) _exception = exception; } + public ExceptionEventArgs(string mesage)=> _exception = new Exception(mesage); + public Exception Exception => _exception; } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs b/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs index 864a67e6..228a1fcd 100644 --- a/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs +++ b/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs @@ -146,7 +146,7 @@ private void WaitForProcessToStart(string applicationPath, TimeSpan timeout, Act var startTime = DateTime.UtcNow; while (DateTime.UtcNow - startTime < timeout) { - Thread.Sleep(10 * 1000); + Thread.Sleep(2 * 1000); if (!process.HasExited) { callbackAction?.Invoke(applicationPath);