mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 将所有小写的命名空间导入更正为首字母大写格式 - 统一 GFramework 框架的命名空间引用规范 - 修复 core、ecs、godot 等模块的命名空间导入错误 - 标准化文档示例代码中的 using 语句格式 - 确保所有文档中的命名空间引用保持一致性 - 更新 global using 语句以匹配正确的命名空间格式
107 lines
4.6 KiB
C#
107 lines
4.6 KiB
C#
using GFramework.Core.Abstractions.Rule;
|
|
using GFramework.Core.Coroutine;
|
|
using GFramework.Core.Coroutine.Extensions;
|
|
using GFramework.Core.Extensions;
|
|
using Mediator;
|
|
|
|
namespace GFramework.Godot.Coroutine;
|
|
|
|
/// <summary>
|
|
/// 提供协程相关的扩展方法,用于简化协程的启动和管理。
|
|
/// </summary>
|
|
public static class ContextAwareCoroutineExtensions
|
|
{
|
|
/// <summary>
|
|
/// 发送命令并直接以协程方式运行(无返回值)
|
|
/// </summary>
|
|
/// <param name="contextAware">上下文感知对象,用于发送命令</param>
|
|
/// <param name="command">要发送的命令对象</param>
|
|
/// <param name="segment">协程运行的时间段,默认为 Process</param>
|
|
/// <param name="tag">协程的标签,可用于标识或分组协程</param>
|
|
/// <param name="cancellationToken">用于取消操作的令牌</param>
|
|
/// <returns>返回协程的句柄,可用于后续操作(如停止协程)</returns>
|
|
public static CoroutineHandle RunCommandCoroutine(
|
|
this IContextAware contextAware,
|
|
ICommand command,
|
|
Segment segment = Segment.Process,
|
|
string? tag = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return contextAware
|
|
.SendCommandAsync(command, cancellationToken)
|
|
.AsTask()
|
|
.ToCoroutineEnumerator()
|
|
.RunCoroutine(segment, tag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送命令并直接以协程方式运行(带返回值)
|
|
/// </summary>
|
|
/// <typeparam name="TResponse">命令返回值的类型</typeparam>
|
|
/// <param name="contextAware">上下文感知对象,用于发送命令</param>
|
|
/// <param name="command">要发送的命令对象</param>
|
|
/// <param name="segment">协程运行的时间段,默认为 Process</param>
|
|
/// <param name="tag">协程的标签,可用于标识或分组协程</param>
|
|
/// <param name="cancellationToken">用于取消操作的令牌</param>
|
|
/// <returns>返回协程的句柄,可用于后续操作(如停止协程)</returns>
|
|
public static CoroutineHandle RunCommandCoroutine<TResponse>(
|
|
this IContextAware contextAware,
|
|
ICommand<TResponse> command,
|
|
Segment segment = Segment.Process,
|
|
string? tag = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return contextAware
|
|
.SendCommandAsync(command, cancellationToken)
|
|
.AsTask()
|
|
.ToCoroutineEnumerator()
|
|
.RunCoroutine(segment, tag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送查询并直接以协程方式运行(带返回值)
|
|
/// </summary>
|
|
/// <typeparam name="TResponse">查询返回值的类型</typeparam>
|
|
/// <param name="contextAware">上下文感知对象,用于发送查询</param>
|
|
/// <param name="query">要发送的查询对象</param>
|
|
/// <param name="segment">协程运行的时间段,默认为 Process</param>
|
|
/// <param name="tag">协程的标签,可用于标识或分组协程</param>
|
|
/// <param name="cancellationToken">用于取消操作的令牌</param>
|
|
/// <returns>返回协程的句柄,可用于后续操作(如停止协程)</returns>
|
|
public static CoroutineHandle RunQueryCoroutine<TResponse>(
|
|
this IContextAware contextAware,
|
|
IQuery<TResponse> query,
|
|
Segment segment = Segment.Process,
|
|
string? tag = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return contextAware
|
|
.SendQueryAsync(query, cancellationToken)
|
|
.AsTask()
|
|
.ToCoroutineEnumerator()
|
|
.RunCoroutine(segment, tag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发布通知并直接以协程方式运行
|
|
/// </summary>
|
|
/// <param name="contextAware">上下文感知对象,用于发布通知</param>
|
|
/// <param name="notification">要发布的通知对象</param>
|
|
/// <param name="segment">协程运行的时间段,默认为 Process</param>
|
|
/// <param name="tag">协程的标签,可用于标识或分组协程</param>
|
|
/// <param name="cancellationToken">用于取消操作的令牌</param>
|
|
/// <returns>返回协程的句柄,可用于后续操作(如停止协程)</returns>
|
|
public static CoroutineHandle RunPublishCoroutine(
|
|
this IContextAware contextAware,
|
|
INotification notification,
|
|
Segment segment = Segment.Process,
|
|
string? tag = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return contextAware
|
|
.PublishAsync(notification, cancellationToken)
|
|
.AsTask()
|
|
.ToCoroutineEnumerator()
|
|
.RunCoroutine(segment, tag);
|
|
}
|
|
} |