feat(coroutine): 添加协程组合扩展功能并优化节点存活检查

- 为SendCommandCoroutineWithErrorHandler方法的onError参数添加默认值null
- 新增CoroutineComposeExtensions扩展类,提供Then方法实现协程顺序组合
- 优化CancelWith扩展方法,改进节点存活检查逻辑
- 使用LINQ All方法简化节点存活状态检查代码
- 添加完整的XML文档注释支持
This commit is contained in:
GeWuYou 2026-02-01 12:25:11 +08:00
parent ec8f72932b
commit 5a056fca84
3 changed files with 70 additions and 6 deletions

View File

@ -36,7 +36,7 @@ public static class CommandCoroutineExtensions
public static IEnumerator<IYieldInstruction> SendCommandCoroutineWithErrorHandler<TCommand>(
this IContextAware contextAware,
TCommand command,
Action<Exception>? onError)
Action<Exception>? onError = null)
where TCommand : class, IAsyncCommand
{
var task = contextAware.GetContext().SendCommandAsync(command);

View File

@ -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;
/// <summary>
/// 提供协程组合扩展方法的静态类
/// </summary>
public static class CoroutineComposeExtensions
{
/// <summary>
/// 将一个协程枚举器与一个动作组合,先执行完第一个协程,然后执行指定的动作
/// </summary>
/// <param name="first">第一个协程枚举器</param>
/// <param name="next">在第一个协程完成后要执行的动作</param>
/// <returns>组合后的协程枚举器</returns>
public static IEnumerator<IYieldInstruction> Then(
this IEnumerator<IYieldInstruction> first,
Action next)
{
// 执行第一个协程的所有步骤
while (first.MoveNext())
yield return first.Current;
// 第一个协程完成后执行指定动作
next();
}
/// <summary>
/// 将两个协程枚举器顺序组合,先执行完第一个协程,再执行第二个协程
/// </summary>
/// <param name="first">第一个协程枚举器</param>
/// <param name="second">第二个协程枚举器</param>
/// <returns>组合后的协程枚举器</returns>
public static IEnumerator<IYieldInstruction> Then(
this IEnumerator<IYieldInstruction> first,
IEnumerator<IYieldInstruction> second)
{
// 执行第一个协程的所有步骤
while (first.MoveNext())
yield return first.Current;
// 第一个协程完成后执行第二个协程的所有步骤
while (second.MoveNext())
yield return second.Current;
}
}

View File

@ -45,20 +45,25 @@ public static class CoroutineExtensions
/// <summary>
/// 让协程在多个节点都被销毁时自动取消
/// </summary>
/// <param name="coroutine">要执行的协程枚举器</param>
/// <param name="nodes">用于检查是否存活的节点数组</param>
/// <returns>包装后的协程枚举器</returns>
public static IEnumerator<IYieldInstruction> CancelWith(
this IEnumerator<IYieldInstruction> coroutine,
params Node[] nodes)
{
// 持续执行协程直到任一节点被销毁或协程执行完毕
while (AllNodesAlive(nodes) && coroutine.MoveNext())
yield return coroutine.Current;
}
/// <summary>
/// 检查所有节点是否都处于存活状态
/// </summary>
/// <param name="nodes">要检查的节点数组</param>
/// <returns>如果所有节点都存活则返回true否则返回false</returns>
private static bool AllNodesAlive(Node[] nodes)
{
foreach (var node in nodes)
if (!Timing.IsNodeAlive(node))
return false;
return true;
return nodes.All(Timing.IsNodeAlive);
}
}