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