From e96b5f24b46b44d575ab17ac92e4d1e01236775d Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Thu, 5 Mar 2026 12:49:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(godot):=20=E5=B0=86=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BA=E9=81=B5?= =?UTF-8?q?=E5=BE=AAAsync=E7=BA=A6=E5=AE=9A=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将WaitUntilReady方法重命名为WaitUntilReadyAsync - 将AddChildX方法重命名为AddChildXAsync - 更新所有相关文档中的方法调用引用 - 修改架构层锚点等待方法调用为异步版本 - 更新测试代码中的方法调用以匹配新的方法名 - 调整函数式异步扩展方法命名约定 - 统一所有异步扩展方法的命名规范 --- .../extensions/AsyncExtensionsTests.cs | 28 +++++++++---------- GFramework.Core/extensions/AsyncExtensions.cs | 10 +++---- .../async/AsyncFunctionalExtensions.cs | 4 +-- .../architecture/AbstractArchitecture.cs | 2 +- GFramework.Godot/extensions/NodeExtensions.cs | 6 ++-- docs/zh-CN/godot/extensions.md | 12 ++++---- docs/zh-CN/godot/index.md | 2 +- docs/zh-CN/tutorials/godot-integration.md | 2 +- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs b/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs index 399e9c7..cb6ab3b 100644 --- a/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs @@ -18,7 +18,7 @@ public class AsyncExtensionsTests public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout() { // Act - var result = await AsyncExtensions.WithTimeout( + var result = await AsyncExtensions.WithTimeoutAsync( _ => Task.FromResult(42), TimeSpan.FromSeconds(1)); @@ -34,7 +34,7 @@ public class AsyncExtensionsTests { // Act & Assert Assert.ThrowsAsync(async () => - await AsyncExtensions.WithTimeout( + await AsyncExtensions.WithTimeoutAsync( async ct => { await Task.Delay(TimeSpan.FromSeconds(2), ct); @@ -54,7 +54,7 @@ public class AsyncExtensionsTests cts.Cancel(); // Act & Assert Assert.ThrowsAsync(async () => - await AsyncExtensions.WithTimeout( + await AsyncExtensions.WithTimeoutAsync( async ct => { await Task.Delay(TimeSpan.FromSeconds(2), ct); @@ -75,7 +75,7 @@ public class AsyncExtensionsTests // Act & Assert Assert.ThrowsAsync(async () => - await AsyncExtensions.WithTimeout( + await AsyncExtensions.WithTimeoutAsync( async ct => { try @@ -104,7 +104,7 @@ public class AsyncExtensionsTests var stopwatch = Stopwatch.StartNew(); // Act - await AsyncExtensions.WithTimeout( + await AsyncExtensions.WithTimeoutAsync( _ => Task.CompletedTask, TimeSpan.FromSeconds(1)); stopwatch.Stop(); @@ -122,7 +122,7 @@ public class AsyncExtensionsTests { // Act & Assert Assert.ThrowsAsync(async () => - await AsyncExtensions.WithTimeout( + await AsyncExtensions.WithTimeoutAsync( ct => Task.Delay(TimeSpan.FromSeconds(2), ct), TimeSpan.FromMilliseconds(100))); } @@ -138,7 +138,7 @@ public class AsyncExtensionsTests // Act & Assert Assert.ThrowsAsync(async () => - await AsyncExtensions.WithTimeout( + await AsyncExtensions.WithTimeoutAsync( async ct => { try @@ -171,7 +171,7 @@ public class AsyncExtensionsTests }; // Act - var result = await taskFactory.WithRetry(3, TimeSpan.FromMilliseconds(10)); + var result = await taskFactory.WithRetryAsync(3, TimeSpan.FromMilliseconds(10)); // Assert Assert.That(result, Is.EqualTo(42)); @@ -195,7 +195,7 @@ public class AsyncExtensionsTests }; // Act - var result = await taskFactory.WithRetry(3, TimeSpan.FromMilliseconds(10)); + var result = await taskFactory.WithRetryAsync(3, TimeSpan.FromMilliseconds(10)); // Assert Assert.That(result, Is.EqualTo(42)); @@ -218,7 +218,7 @@ public class AsyncExtensionsTests // Act & Assert Assert.ThrowsAsync(async () => - await taskFactory.WithRetry(2, TimeSpan.FromMilliseconds(10))); + await taskFactory.WithRetryAsync(2, TimeSpan.FromMilliseconds(10))); } /// @@ -237,7 +237,7 @@ public class AsyncExtensionsTests // Act & Assert Assert.ThrowsAsync(async () => - await taskFactory.WithRetry(3, TimeSpan.FromMilliseconds(10), + await taskFactory.WithRetryAsync(3, TimeSpan.FromMilliseconds(10), ex => ex is not ArgumentException)); await Task.Delay(50); // 等待任务完成 @@ -287,7 +287,7 @@ public class AsyncExtensionsTests var task = Task.FromResult(42); // Act - var result = await task.WithFallback(_ => -1); + var result = await task.WithFallbackAsync(_ => -1); // Assert Assert.That(result, Is.EqualTo(42)); @@ -303,7 +303,7 @@ public class AsyncExtensionsTests var task = Task.FromException(new InvalidOperationException("Test error")); // Act - var result = await task.WithFallback(_ => -1); + var result = await task.WithFallbackAsync(_ => -1); // Assert Assert.That(result, Is.EqualTo(-1)); @@ -321,7 +321,7 @@ public class AsyncExtensionsTests Exception? capturedEx = null; // Act - await task.WithFallback(ex => + await task.WithFallbackAsync(ex => { capturedEx = ex; return -1; diff --git a/GFramework.Core/extensions/AsyncExtensions.cs b/GFramework.Core/extensions/AsyncExtensions.cs index 2b640dd..4839e30 100644 --- a/GFramework.Core/extensions/AsyncExtensions.cs +++ b/GFramework.Core/extensions/AsyncExtensions.cs @@ -18,12 +18,12 @@ public static class AsyncExtensions /// 当操作被取消时抛出 /// /// - /// var result = await WithTimeout( + /// var result = await WithTimeoutAsync( /// ct => SomeAsyncOperation(ct), /// TimeSpan.FromSeconds(5)); /// /// - public static async Task WithTimeout( + public static async Task WithTimeoutAsync( Func> taskFactory, TimeSpan timeout, CancellationToken cancellationToken = default) @@ -66,7 +66,7 @@ public static class AsyncExtensions /// 当 taskFactory 为 null 时抛出 /// 当任务超时时抛出 /// 当操作被取消时抛出 - public static async Task WithTimeout( + public static async Task WithTimeoutAsync( Func taskFactory, TimeSpan timeout, CancellationToken cancellationToken = default) @@ -108,10 +108,10 @@ public static class AsyncExtensions /// /// /// var result = await RiskyOperation() - /// .WithFallback(ex => DefaultValue); + /// .WithFallbackAsync(ex => DefaultValue); /// /// - public static async Task WithFallback(this Task task, Func fallback) + public static async Task WithFallbackAsync(this Task task, Func fallback) { ArgumentNullException.ThrowIfNull(task); ArgumentNullException.ThrowIfNull(fallback); diff --git a/GFramework.Core/functional/async/AsyncFunctionalExtensions.cs b/GFramework.Core/functional/async/AsyncFunctionalExtensions.cs index 7cce5b2..ed34273 100644 --- a/GFramework.Core/functional/async/AsyncFunctionalExtensions.cs +++ b/GFramework.Core/functional/async/AsyncFunctionalExtensions.cs @@ -33,10 +33,10 @@ public static class AsyncFunctionalExtensions /// /// /// var result = await (() => UnreliableOperation()) - /// .WithRetry(maxRetries: 3, delay: TimeSpan.FromSeconds(1)); + /// .WithRetryAsync(maxRetries: 3, delay: TimeSpan.FromSeconds(1)); /// /// - public static async Task WithRetry( + public static async Task WithRetryAsync( this Func> taskFactory, int maxRetries, TimeSpan delay, diff --git a/GFramework.Godot/architecture/AbstractArchitecture.cs b/GFramework.Godot/architecture/AbstractArchitecture.cs index c7ca2f4..111e94c 100644 --- a/GFramework.Godot/architecture/AbstractArchitecture.cs +++ b/GFramework.Godot/architecture/AbstractArchitecture.cs @@ -106,7 +106,7 @@ public abstract class AbstractArchitecture( throw new InvalidOperationException("Anchor not initialized"); // 等待锚点准备就绪 - await _anchor.WaitUntilReady(); + await _anchor.WaitUntilReadyAsync(); // 延迟调用将扩展节点添加为锚点的子节点 _anchor.CallDeferred(Node.MethodName.AddChild, module.Node); diff --git a/GFramework.Godot/extensions/NodeExtensions.cs b/GFramework.Godot/extensions/NodeExtensions.cs index ac93cd8..19bd824 100644 --- a/GFramework.Godot/extensions/NodeExtensions.cs +++ b/GFramework.Godot/extensions/NodeExtensions.cs @@ -51,7 +51,7 @@ public static class NodeExtensions /// /// 要等待其准备就绪的节点 /// 表示异步操作的任务 - public static async Task WaitUntilReady(this Node node) + public static async Task WaitUntilReadyAsync(this Node node) { if (!node.IsInsideTree()) await node.ToSignal(node, Node.SignalName.Ready); } @@ -173,10 +173,10 @@ public static class NodeExtensions /// 父节点 /// 要添加的子节点 /// 异步任务 - public static async Task AddChildX(this Node parent, Node child) + public static async Task AddChildXAsync(this Node parent, Node child) { parent.AddChild(child); - await child.WaitUntilReady(); + await child.WaitUntilReadyAsync(); } /// diff --git a/docs/zh-CN/godot/extensions.md b/docs/zh-CN/godot/extensions.md index 67038c9..e7426b1 100644 --- a/docs/zh-CN/godot/extensions.md +++ b/docs/zh-CN/godot/extensions.md @@ -62,7 +62,7 @@ node.QueueFreeX(); // 延迟释放 node.FreeX(); // 立即释放 // 等待节点就绪 -await node.WaitUntilReady(); +await node.WaitUntilReadyAsync(); // 检查节点有效性 if (node.IsValidNode()) Console.WriteLine("节点有效"); @@ -92,7 +92,7 @@ node.ForEachChild(sprite => { var root = node.GetRootNodeX(); // 异步添加子节点 -await parent.AddChildX(childNode); +await parent.AddChildXAsync(childNode); // 设置场景树暂停状态 node.Paused(true); // 暂停 @@ -178,7 +178,7 @@ public class GameManager : Node // 安全添加子节点 var player = new Player(); - await AddChildX(player); + await AddChildXAsync(player); // 查找并配置玩家 var sprite = player.FindChildX("Sprite"); @@ -237,7 +237,7 @@ public class SceneManager : Node var instance = packedScene.Instantiate(); // 等待场景加载完成 - await instance.WaitUntilReady(); + await instance.WaitUntilReadyAsync(); return instance; } @@ -250,7 +250,7 @@ public class SceneManager : Node ForEachChild(child => child.QueueFreeX()); // 加载新场景 - await AddChildX(newScene); + await AddChildXAsync(newScene); // 设置输入处理 newScene.SetInputAsHandled(); @@ -310,7 +310,7 @@ var button = node.FindChild("Button") as Button; ```csharp // ✅ 推荐:等待节点就绪 -await child.WaitUntilReady(); +await child.WaitUntilReadyAsync(); // ❌ 避免:假设节点已就绪 child.DoSomething(); diff --git a/docs/zh-CN/godot/index.md b/docs/zh-CN/godot/index.md index 1311f94..e2c98a3 100644 --- a/docs/zh-CN/godot/index.md +++ b/docs/zh-CN/godot/index.md @@ -234,7 +234,7 @@ var bullet = bulletScene.Instantiate(); AddChildX(bullet); // 等待节点准备就绪 -await bullet.WaitUntilReady(); +await bullet.WaitUntilReadyAsync(); // 获取父节点 var parent = GetParentX(); diff --git a/docs/zh-CN/tutorials/godot-integration.md b/docs/zh-CN/tutorials/godot-integration.md index 6c4ef3a..54fe538 100644 --- a/docs/zh-CN/tutorials/godot-integration.md +++ b/docs/zh-CN/tutorials/godot-integration.md @@ -306,7 +306,7 @@ public partial class SafeNodeOperations : Node AddChildX(bullet); // 安全的异步操作 - this.WaitUntilReady() + this.WaitUntilReadyAsync() .Then(() => { // 节点准备就绪后的操作 InitializeAfterReady();