diff --git a/GFramework.Core.Tests/mediator/MediatorAdvancedFeaturesTests.cs b/GFramework.Core.Tests/mediator/MediatorAdvancedFeaturesTests.cs index 99de30f..970becc 100644 --- a/GFramework.Core.Tests/mediator/MediatorAdvancedFeaturesTests.cs +++ b/GFramework.Core.Tests/mediator/MediatorAdvancedFeaturesTests.cs @@ -47,12 +47,6 @@ public class MediatorAdvancedFeaturesTests private ArchitectureContext? _context; private MicrosoftDiContainer? _container; - [Test] - public async Task Request_With_Logging_Behavior_Should_Log_Correctly() - { - // 由于我们没有实现实际的日志行为,这个测试暂时跳过 - Assert.Ignore("Logging behavior not implemented in this test setup"); - } [Test] public async Task Request_With_Validation_Behavior_Should_Validate_Input() diff --git a/GFramework.Godot/coroutine/ContextAwareCoroutineExtensions.cs b/GFramework.Godot/coroutine/ContextAwareCoroutineExtensions.cs new file mode 100644 index 0000000..7a1293a --- /dev/null +++ b/GFramework.Godot/coroutine/ContextAwareCoroutineExtensions.cs @@ -0,0 +1,107 @@ +using GFramework.Core.Abstractions.rule; +using GFramework.Core.coroutine; +using GFramework.Core.coroutine.extensions; +using GFramework.Core.extensions; +using Mediator; + +namespace GFramework.Godot.coroutine; + +/// +/// 提供协程相关的扩展方法,用于简化协程的启动和管理。 +/// +public static class ContextAwareCoroutineExtensions +{ + /// + /// 发送命令并直接以协程方式运行(无返回值) + /// + /// 上下文感知对象,用于发送命令 + /// 要发送的命令对象 + /// 协程运行的时间段,默认为 Process + /// 协程的标签,可用于标识或分组协程 + /// 用于取消操作的令牌 + /// 返回协程的句柄,可用于后续操作(如停止协程) + 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); + } + + /// + /// 发送命令并直接以协程方式运行(带返回值) + /// + /// 命令返回值的类型 + /// 上下文感知对象,用于发送命令 + /// 要发送的命令对象 + /// 协程运行的时间段,默认为 Process + /// 协程的标签,可用于标识或分组协程 + /// 用于取消操作的令牌 + /// 返回协程的句柄,可用于后续操作(如停止协程) + 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); + } + + /// + /// 发送查询并直接以协程方式运行(带返回值) + /// + /// 查询返回值的类型 + /// 上下文感知对象,用于发送查询 + /// 要发送的查询对象 + /// 协程运行的时间段,默认为 Process + /// 协程的标签,可用于标识或分组协程 + /// 用于取消操作的令牌 + /// 返回协程的句柄,可用于后续操作(如停止协程) + public static CoroutineHandle RunQueryCoroutine( + this IContextAware contextAware, + IQuery query, + Segment segment = Segment.Process, + string? tag = null, + CancellationToken cancellationToken = default) + { + return contextAware + .SendQueryAsync(query, cancellationToken) + .AsTask() + .ToCoroutineEnumerator() + .RunCoroutine(segment, tag); + } + + /// + /// 发布通知并直接以协程方式运行 + /// + /// 上下文感知对象,用于发布通知 + /// 要发布的通知对象 + /// 协程运行的时间段,默认为 Process + /// 协程的标签,可用于标识或分组协程 + /// 用于取消操作的令牌 + /// 返回协程的句柄,可用于后续操作(如停止协程) + public static CoroutineHandle RunPublishCoroutine( + this IContextAware contextAware, + INotification notification, + Segment segment = Segment.Process, + string? tag = null, + CancellationToken cancellationToken = default) + { + return contextAware + .PublishEventAsync(notification, cancellationToken) + .AsTask() + .ToCoroutineEnumerator() + .RunCoroutine(segment, tag); + } +} \ No newline at end of file diff --git a/GFramework.Godot/coroutine/CoroutineExtensions.cs b/GFramework.Godot/coroutine/CoroutineNodeExtensions.cs similarity index 50% rename from GFramework.Godot/coroutine/CoroutineExtensions.cs rename to GFramework.Godot/coroutine/CoroutineNodeExtensions.cs index 1a0c112..d40a72f 100644 --- a/GFramework.Godot/coroutine/CoroutineExtensions.cs +++ b/GFramework.Godot/coroutine/CoroutineNodeExtensions.cs @@ -1,37 +1,47 @@ -using GFramework.Core.Abstractions.coroutine; +// 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; using GFramework.Core.coroutine; using Godot; namespace GFramework.Godot.coroutine; /// -/// 提供协程相关的扩展方法,用于简化协程的启动和管理。 +/// 提供协程相关的扩展方法,用于在Godot环境中管理协程的生命周期。 /// -public static class CoroutineExtensions +public static class CoroutineNodeExtensions { /// - /// 启动协程的扩展方法。 + /// 启动协程的扩展方法。 /// /// 要启动的协程枚举器。 /// 协程运行的时间段,默认为 Process。 /// 协程的标签,可用于标识或分组协程。 /// 返回协程的句柄,可用于后续操作(如停止协程)。 - public static CoroutineHandle RunCoroutine( - this IEnumerator coroutine, - Segment segment = Segment.Process, - string? tag = null) + public static CoroutineHandle RunCoroutine(this IEnumerator coroutine, + Segment segment = Segment.Process, string? tag = null) { return Timing.RunCoroutine(coroutine, segment, tag); } /// - /// 让协程在指定节点被销毁时自动取消。 + /// 让协程在指定节点被销毁时自动取消。 /// - /// 要执行的协程枚举器。 + /// 要包装的协程枚举器。 /// 用于检查是否存活的节点。 /// 包装后的协程枚举器。 - public static IEnumerator CancelWith( - this IEnumerator coroutine, + public static IEnumerator CancelWith(this IEnumerator coroutine, Node node) { while (Timing.IsNodeAlive(node) && coroutine.MoveNext()) @@ -39,16 +49,14 @@ public static class CoroutineExtensions } /// - /// 让协程在任一节点被销毁时自动取消。 + /// 让协程在任一节点被销毁时自动取消。 /// - /// 要执行的协程枚举器。 + /// 要包装的协程枚举器。 /// 第一个用于检查是否存活的节点。 /// 第二个用于检查是否存活的节点。 /// 包装后的协程枚举器。 - public static IEnumerator CancelWith( - this IEnumerator coroutine, - Node node1, - Node node2) + public static IEnumerator CancelWith(this IEnumerator coroutine, + Node node1, Node node2) { while (Timing.IsNodeAlive(node1) && Timing.IsNodeAlive(node2) && @@ -57,27 +65,26 @@ public static class CoroutineExtensions } /// - /// 让协程在多个节点都被销毁时自动取消。 + /// 让协程在多个节点都被销毁时自动取消。 /// - /// 要执行的协程枚举器。 + /// 要包装的协程枚举器。 /// 用于检查是否存活的节点数组。 /// 包装后的协程枚举器。 - public static IEnumerator CancelWith( - this IEnumerator coroutine, + public static IEnumerator CancelWith(this IEnumerator coroutine, params Node[] nodes) { // 持续执行协程直到任一节点被销毁或协程执行完毕 - while (AllNodesAlive(nodes) && coroutine.MoveNext()) + while (nodes.AllNodesAlive() && coroutine.MoveNext()) yield return coroutine.Current; } /// - /// 检查所有节点是否都处于存活状态。 + /// 检查所有节点是否都处于存活状态。 /// /// 要检查的节点数组。 /// 如果所有节点都存活则返回 true,否则返回 false。 - private static bool AllNodesAlive(Node[] nodes) + private static bool AllNodesAlive(this Node[] nodes) { return nodes.All(Timing.IsNodeAlive); } -} +} \ No newline at end of file