mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 13:33:28 +08:00
refactor(godot): 将异步方法重命名为遵循Async约定的方法名
- 将WaitUntilReady方法重命名为WaitUntilReadyAsync - 将AddChildX方法重命名为AddChildXAsync - 更新所有相关文档中的方法调用引用 - 修改架构层锚点等待方法调用为异步版本 - 更新测试代码中的方法调用以匹配新的方法名 - 调整函数式异步扩展方法命名约定 - 统一所有异步扩展方法的命名规范
This commit is contained in:
parent
8db379c53f
commit
e96b5f24b4
@ -18,7 +18,7 @@ public class AsyncExtensionsTests
|
|||||||
public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout()
|
public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout()
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
var result = await AsyncExtensions.WithTimeout(
|
var result = await AsyncExtensions.WithTimeoutAsync(
|
||||||
_ => Task.FromResult(42),
|
_ => Task.FromResult(42),
|
||||||
TimeSpan.FromSeconds(1));
|
TimeSpan.FromSeconds(1));
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ public class AsyncExtensionsTests
|
|||||||
{
|
{
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TimeoutException>(async () =>
|
Assert.ThrowsAsync<TimeoutException>(async () =>
|
||||||
await AsyncExtensions.WithTimeout(
|
await AsyncExtensions.WithTimeoutAsync(
|
||||||
async ct =>
|
async ct =>
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2), ct);
|
await Task.Delay(TimeSpan.FromSeconds(2), ct);
|
||||||
@ -54,7 +54,7 @@ public class AsyncExtensionsTests
|
|||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TaskCanceledException>(async () =>
|
Assert.ThrowsAsync<TaskCanceledException>(async () =>
|
||||||
await AsyncExtensions.WithTimeout(
|
await AsyncExtensions.WithTimeoutAsync(
|
||||||
async ct =>
|
async ct =>
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2), ct);
|
await Task.Delay(TimeSpan.FromSeconds(2), ct);
|
||||||
@ -75,7 +75,7 @@ public class AsyncExtensionsTests
|
|||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TimeoutException>(async () =>
|
Assert.ThrowsAsync<TimeoutException>(async () =>
|
||||||
await AsyncExtensions.WithTimeout(
|
await AsyncExtensions.WithTimeoutAsync(
|
||||||
async ct =>
|
async ct =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -104,7 +104,7 @@ public class AsyncExtensionsTests
|
|||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await AsyncExtensions.WithTimeout(
|
await AsyncExtensions.WithTimeoutAsync(
|
||||||
_ => Task.CompletedTask,
|
_ => Task.CompletedTask,
|
||||||
TimeSpan.FromSeconds(1));
|
TimeSpan.FromSeconds(1));
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
@ -122,7 +122,7 @@ public class AsyncExtensionsTests
|
|||||||
{
|
{
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TimeoutException>(async () =>
|
Assert.ThrowsAsync<TimeoutException>(async () =>
|
||||||
await AsyncExtensions.WithTimeout(
|
await AsyncExtensions.WithTimeoutAsync(
|
||||||
ct => Task.Delay(TimeSpan.FromSeconds(2), ct),
|
ct => Task.Delay(TimeSpan.FromSeconds(2), ct),
|
||||||
TimeSpan.FromMilliseconds(100)));
|
TimeSpan.FromMilliseconds(100)));
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ public class AsyncExtensionsTests
|
|||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TimeoutException>(async () =>
|
Assert.ThrowsAsync<TimeoutException>(async () =>
|
||||||
await AsyncExtensions.WithTimeout(
|
await AsyncExtensions.WithTimeoutAsync(
|
||||||
async ct =>
|
async ct =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -171,7 +171,7 @@ public class AsyncExtensionsTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await taskFactory.WithRetry(3, TimeSpan.FromMilliseconds(10));
|
var result = await taskFactory.WithRetryAsync(3, TimeSpan.FromMilliseconds(10));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.EqualTo(42));
|
Assert.That(result, Is.EqualTo(42));
|
||||||
@ -195,7 +195,7 @@ public class AsyncExtensionsTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await taskFactory.WithRetry(3, TimeSpan.FromMilliseconds(10));
|
var result = await taskFactory.WithRetryAsync(3, TimeSpan.FromMilliseconds(10));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.EqualTo(42));
|
Assert.That(result, Is.EqualTo(42));
|
||||||
@ -218,7 +218,7 @@ public class AsyncExtensionsTests
|
|||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<AggregateException>(async () =>
|
Assert.ThrowsAsync<AggregateException>(async () =>
|
||||||
await taskFactory.WithRetry(2, TimeSpan.FromMilliseconds(10)));
|
await taskFactory.WithRetryAsync(2, TimeSpan.FromMilliseconds(10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -237,7 +237,7 @@ public class AsyncExtensionsTests
|
|||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<AggregateException>(async () =>
|
Assert.ThrowsAsync<AggregateException>(async () =>
|
||||||
await taskFactory.WithRetry(3, TimeSpan.FromMilliseconds(10),
|
await taskFactory.WithRetryAsync(3, TimeSpan.FromMilliseconds(10),
|
||||||
ex => ex is not ArgumentException));
|
ex => ex is not ArgumentException));
|
||||||
|
|
||||||
await Task.Delay(50); // 等待任务完成
|
await Task.Delay(50); // 等待任务完成
|
||||||
@ -287,7 +287,7 @@ public class AsyncExtensionsTests
|
|||||||
var task = Task.FromResult(42);
|
var task = Task.FromResult(42);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await task.WithFallback(_ => -1);
|
var result = await task.WithFallbackAsync(_ => -1);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.EqualTo(42));
|
Assert.That(result, Is.EqualTo(42));
|
||||||
@ -303,7 +303,7 @@ public class AsyncExtensionsTests
|
|||||||
var task = Task.FromException<int>(new InvalidOperationException("Test error"));
|
var task = Task.FromException<int>(new InvalidOperationException("Test error"));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await task.WithFallback(_ => -1);
|
var result = await task.WithFallbackAsync(_ => -1);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.EqualTo(-1));
|
Assert.That(result, Is.EqualTo(-1));
|
||||||
@ -321,7 +321,7 @@ public class AsyncExtensionsTests
|
|||||||
Exception? capturedEx = null;
|
Exception? capturedEx = null;
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await task.WithFallback(ex =>
|
await task.WithFallbackAsync(ex =>
|
||||||
{
|
{
|
||||||
capturedEx = ex;
|
capturedEx = ex;
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@ -18,12 +18,12 @@ public static class AsyncExtensions
|
|||||||
/// <exception cref="OperationCanceledException">当操作被取消时抛出</exception>
|
/// <exception cref="OperationCanceledException">当操作被取消时抛出</exception>
|
||||||
/// <example>
|
/// <example>
|
||||||
/// <code>
|
/// <code>
|
||||||
/// var result = await WithTimeout(
|
/// var result = await WithTimeoutAsync(
|
||||||
/// ct => SomeAsyncOperation(ct),
|
/// ct => SomeAsyncOperation(ct),
|
||||||
/// TimeSpan.FromSeconds(5));
|
/// TimeSpan.FromSeconds(5));
|
||||||
/// </code>
|
/// </code>
|
||||||
/// </example>
|
/// </example>
|
||||||
public static async Task<T> WithTimeout<T>(
|
public static async Task<T> WithTimeoutAsync<T>(
|
||||||
Func<CancellationToken, Task<T>> taskFactory,
|
Func<CancellationToken, Task<T>> taskFactory,
|
||||||
TimeSpan timeout,
|
TimeSpan timeout,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
@ -66,7 +66,7 @@ public static class AsyncExtensions
|
|||||||
/// <exception cref="ArgumentNullException">当 taskFactory 为 null 时抛出</exception>
|
/// <exception cref="ArgumentNullException">当 taskFactory 为 null 时抛出</exception>
|
||||||
/// <exception cref="TimeoutException">当任务超时时抛出</exception>
|
/// <exception cref="TimeoutException">当任务超时时抛出</exception>
|
||||||
/// <exception cref="OperationCanceledException">当操作被取消时抛出</exception>
|
/// <exception cref="OperationCanceledException">当操作被取消时抛出</exception>
|
||||||
public static async Task WithTimeout(
|
public static async Task WithTimeoutAsync(
|
||||||
Func<CancellationToken, Task> taskFactory,
|
Func<CancellationToken, Task> taskFactory,
|
||||||
TimeSpan timeout,
|
TimeSpan timeout,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
@ -108,10 +108,10 @@ public static class AsyncExtensions
|
|||||||
/// <example>
|
/// <example>
|
||||||
/// <code>
|
/// <code>
|
||||||
/// var result = await RiskyOperation()
|
/// var result = await RiskyOperation()
|
||||||
/// .WithFallback(ex => DefaultValue);
|
/// .WithFallbackAsync(ex => DefaultValue);
|
||||||
/// </code>
|
/// </code>
|
||||||
/// </example>
|
/// </example>
|
||||||
public static async Task<T> WithFallback<T>(this Task<T> task, Func<Exception, T> fallback)
|
public static async Task<T> WithFallbackAsync<T>(this Task<T> task, Func<Exception, T> fallback)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(task);
|
ArgumentNullException.ThrowIfNull(task);
|
||||||
ArgumentNullException.ThrowIfNull(fallback);
|
ArgumentNullException.ThrowIfNull(fallback);
|
||||||
|
|||||||
@ -33,10 +33,10 @@ public static class AsyncFunctionalExtensions
|
|||||||
/// <example>
|
/// <example>
|
||||||
/// <code>
|
/// <code>
|
||||||
/// var result = await (() => UnreliableOperation())
|
/// var result = await (() => UnreliableOperation())
|
||||||
/// .WithRetry(maxRetries: 3, delay: TimeSpan.FromSeconds(1));
|
/// .WithRetryAsync(maxRetries: 3, delay: TimeSpan.FromSeconds(1));
|
||||||
/// </code>
|
/// </code>
|
||||||
/// </example>
|
/// </example>
|
||||||
public static async Task<T> WithRetry<T>(
|
public static async Task<T> WithRetryAsync<T>(
|
||||||
this Func<Task<T>> taskFactory,
|
this Func<Task<T>> taskFactory,
|
||||||
int maxRetries,
|
int maxRetries,
|
||||||
TimeSpan delay,
|
TimeSpan delay,
|
||||||
|
|||||||
@ -106,7 +106,7 @@ public abstract class AbstractArchitecture(
|
|||||||
throw new InvalidOperationException("Anchor not initialized");
|
throw new InvalidOperationException("Anchor not initialized");
|
||||||
|
|
||||||
// 等待锚点准备就绪
|
// 等待锚点准备就绪
|
||||||
await _anchor.WaitUntilReady();
|
await _anchor.WaitUntilReadyAsync();
|
||||||
|
|
||||||
// 延迟调用将扩展节点添加为锚点的子节点
|
// 延迟调用将扩展节点添加为锚点的子节点
|
||||||
_anchor.CallDeferred(Node.MethodName.AddChild, module.Node);
|
_anchor.CallDeferred(Node.MethodName.AddChild, module.Node);
|
||||||
|
|||||||
@ -51,7 +51,7 @@ public static class NodeExtensions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="node">要等待其准备就绪的节点</param>
|
/// <param name="node">要等待其准备就绪的节点</param>
|
||||||
/// <returns>表示异步操作的任务</returns>
|
/// <returns>表示异步操作的任务</returns>
|
||||||
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);
|
if (!node.IsInsideTree()) await node.ToSignal(node, Node.SignalName.Ready);
|
||||||
}
|
}
|
||||||
@ -173,10 +173,10 @@ public static class NodeExtensions
|
|||||||
/// <param name="parent">父节点</param>
|
/// <param name="parent">父节点</param>
|
||||||
/// <param name="child">要添加的子节点</param>
|
/// <param name="child">要添加的子节点</param>
|
||||||
/// <returns>异步任务</returns>
|
/// <returns>异步任务</returns>
|
||||||
public static async Task AddChildX(this Node parent, Node child)
|
public static async Task AddChildXAsync(this Node parent, Node child)
|
||||||
{
|
{
|
||||||
parent.AddChild(child);
|
parent.AddChild(child);
|
||||||
await child.WaitUntilReady();
|
await child.WaitUntilReadyAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -62,7 +62,7 @@ node.QueueFreeX(); // 延迟释放
|
|||||||
node.FreeX(); // 立即释放
|
node.FreeX(); // 立即释放
|
||||||
|
|
||||||
// 等待节点就绪
|
// 等待节点就绪
|
||||||
await node.WaitUntilReady();
|
await node.WaitUntilReadyAsync();
|
||||||
|
|
||||||
// 检查节点有效性
|
// 检查节点有效性
|
||||||
if (node.IsValidNode()) Console.WriteLine("节点有效");
|
if (node.IsValidNode()) Console.WriteLine("节点有效");
|
||||||
@ -92,7 +92,7 @@ node.ForEachChild<Sprite2D>(sprite => {
|
|||||||
var root = node.GetRootNodeX();
|
var root = node.GetRootNodeX();
|
||||||
|
|
||||||
// 异步添加子节点
|
// 异步添加子节点
|
||||||
await parent.AddChildX(childNode);
|
await parent.AddChildXAsync(childNode);
|
||||||
|
|
||||||
// 设置场景树暂停状态
|
// 设置场景树暂停状态
|
||||||
node.Paused(true); // 暂停
|
node.Paused(true); // 暂停
|
||||||
@ -178,7 +178,7 @@ public class GameManager : Node
|
|||||||
|
|
||||||
// 安全添加子节点
|
// 安全添加子节点
|
||||||
var player = new Player();
|
var player = new Player();
|
||||||
await AddChildX(player);
|
await AddChildXAsync(player);
|
||||||
|
|
||||||
// 查找并配置玩家
|
// 查找并配置玩家
|
||||||
var sprite = player.FindChildX<Sprite2D>("Sprite");
|
var sprite = player.FindChildX<Sprite2D>("Sprite");
|
||||||
@ -237,7 +237,7 @@ public class SceneManager : Node
|
|||||||
var instance = packedScene.Instantiate<T>();
|
var instance = packedScene.Instantiate<T>();
|
||||||
|
|
||||||
// 等待场景加载完成
|
// 等待场景加载完成
|
||||||
await instance.WaitUntilReady();
|
await instance.WaitUntilReadyAsync();
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
@ -250,7 +250,7 @@ public class SceneManager : Node
|
|||||||
ForEachChild<Node>(child => child.QueueFreeX());
|
ForEachChild<Node>(child => child.QueueFreeX());
|
||||||
|
|
||||||
// 加载新场景
|
// 加载新场景
|
||||||
await AddChildX(newScene);
|
await AddChildXAsync(newScene);
|
||||||
|
|
||||||
// 设置输入处理
|
// 设置输入处理
|
||||||
newScene.SetInputAsHandled();
|
newScene.SetInputAsHandled();
|
||||||
@ -310,7 +310,7 @@ var button = node.FindChild("Button") as Button;
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// ✅ 推荐:等待节点就绪
|
// ✅ 推荐:等待节点就绪
|
||||||
await child.WaitUntilReady();
|
await child.WaitUntilReadyAsync();
|
||||||
|
|
||||||
// ❌ 避免:假设节点已就绪
|
// ❌ 避免:假设节点已就绪
|
||||||
child.DoSomething();
|
child.DoSomething();
|
||||||
|
|||||||
@ -234,7 +234,7 @@ var bullet = bulletScene.Instantiate<Bullet>();
|
|||||||
AddChildX(bullet);
|
AddChildX(bullet);
|
||||||
|
|
||||||
// 等待节点准备就绪
|
// 等待节点准备就绪
|
||||||
await bullet.WaitUntilReady();
|
await bullet.WaitUntilReadyAsync();
|
||||||
|
|
||||||
// 获取父节点
|
// 获取父节点
|
||||||
var parent = GetParentX<GameLevel>();
|
var parent = GetParentX<GameLevel>();
|
||||||
|
|||||||
@ -306,7 +306,7 @@ public partial class SafeNodeOperations : Node
|
|||||||
AddChildX(bullet);
|
AddChildX(bullet);
|
||||||
|
|
||||||
// 安全的异步操作
|
// 安全的异步操作
|
||||||
this.WaitUntilReady()
|
this.WaitUntilReadyAsync()
|
||||||
.Then(() => {
|
.Then(() => {
|
||||||
// 节点准备就绪后的操作
|
// 节点准备就绪后的操作
|
||||||
InitializeAfterReady();
|
InitializeAfterReady();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user