From 5a056fca846652402ae569e74b3bc5f7f79d4433 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 1 Feb 2026 12:25:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(coroutine):=20=E6=B7=BB=E5=8A=A0=E5=8D=8F?= =?UTF-8?q?=E7=A8=8B=E7=BB=84=E5=90=88=E6=89=A9=E5=B1=95=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E8=8A=82=E7=82=B9=E5=AD=98=E6=B4=BB?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为SendCommandCoroutineWithErrorHandler方法的onError参数添加默认值null - 新增CoroutineComposeExtensions扩展类,提供Then方法实现协程顺序组合 - 优化CancelWith扩展方法,改进节点存活检查逻辑 - 使用LINQ All方法简化节点存活状态检查代码 - 添加完整的XML文档注释支持 --- .../extensions/CommandCoroutineExtensions.cs | 2 +- .../extensions/CoroutineComposeExtensions.cs | 59 +++++++++++++++++++ .../coroutine/CoroutineExtensions.cs | 15 +++-- 3 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 GFramework.Core/coroutine/extensions/CoroutineComposeExtensions.cs diff --git a/GFramework.Core/coroutine/extensions/CommandCoroutineExtensions.cs b/GFramework.Core/coroutine/extensions/CommandCoroutineExtensions.cs index 76da41a..041d30e 100644 --- a/GFramework.Core/coroutine/extensions/CommandCoroutineExtensions.cs +++ b/GFramework.Core/coroutine/extensions/CommandCoroutineExtensions.cs @@ -36,7 +36,7 @@ public static class CommandCoroutineExtensions public static IEnumerator SendCommandCoroutineWithErrorHandler( this IContextAware contextAware, TCommand command, - Action? onError) + Action? onError = null) where TCommand : class, IAsyncCommand { var task = contextAware.GetContext().SendCommandAsync(command); diff --git a/GFramework.Core/coroutine/extensions/CoroutineComposeExtensions.cs b/GFramework.Core/coroutine/extensions/CoroutineComposeExtensions.cs new file mode 100644 index 0000000..8ca8114 --- /dev/null +++ b/GFramework.Core/coroutine/extensions/CoroutineComposeExtensions.cs @@ -0,0 +1,59 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using GFramework.Core.Abstractions.coroutine; + +namespace GFramework.Core.coroutine.extensions; + +/// +/// 提供协程组合扩展方法的静态类 +/// +public static class CoroutineComposeExtensions +{ + /// + /// 将一个协程枚举器与一个动作组合,先执行完第一个协程,然后执行指定的动作 + /// + /// 第一个协程枚举器 + /// 在第一个协程完成后要执行的动作 + /// 组合后的协程枚举器 + public static IEnumerator Then( + this IEnumerator first, + Action next) + { + // 执行第一个协程的所有步骤 + while (first.MoveNext()) + yield return first.Current; + + // 第一个协程完成后执行指定动作 + next(); + } + + /// + /// 将两个协程枚举器顺序组合,先执行完第一个协程,再执行第二个协程 + /// + /// 第一个协程枚举器 + /// 第二个协程枚举器 + /// 组合后的协程枚举器 + public static IEnumerator Then( + this IEnumerator first, + IEnumerator second) + { + // 执行第一个协程的所有步骤 + while (first.MoveNext()) + yield return first.Current; + + // 第一个协程完成后执行第二个协程的所有步骤 + while (second.MoveNext()) + yield return second.Current; + } +} \ No newline at end of file diff --git a/GFramework.Godot/coroutine/CoroutineExtensions.cs b/GFramework.Godot/coroutine/CoroutineExtensions.cs index 7dfd6dd..7ea08d4 100644 --- a/GFramework.Godot/coroutine/CoroutineExtensions.cs +++ b/GFramework.Godot/coroutine/CoroutineExtensions.cs @@ -45,20 +45,25 @@ public static class CoroutineExtensions /// /// 让协程在多个节点都被销毁时自动取消 /// + /// 要执行的协程枚举器 + /// 用于检查是否存活的节点数组 + /// 包装后的协程枚举器 public static IEnumerator CancelWith( this IEnumerator coroutine, params Node[] nodes) { + // 持续执行协程直到任一节点被销毁或协程执行完毕 while (AllNodesAlive(nodes) && coroutine.MoveNext()) yield return coroutine.Current; } + /// + /// 检查所有节点是否都处于存活状态 + /// + /// 要检查的节点数组 + /// 如果所有节点都存活则返回true,否则返回false private static bool AllNodesAlive(Node[] nodes) { - foreach (var node in nodes) - if (!Timing.IsNodeAlive(node)) - return false; - - return true; + return nodes.All(Timing.IsNodeAlive); } } \ No newline at end of file