From b4360b01ca18de1b913a7dab578191d158cddabe Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sun, 1 Feb 2026 15:16:15 +0800
Subject: [PATCH] =?UTF-8?q?refactor(coroutine):=20=E4=BC=98=E5=8C=96=20Wai?=
=?UTF-8?q?tForAllCoroutines=20=E5=AE=9E=E7=8E=B0=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 将字段初始化改为构造函数中赋值
- 添加 _isDone 字段缓存完成状态
- 在构造函数中预处理空列表情况
- 优化 Update 方法减少重复计算
- 简化 IsDone 属性实现
- 提升协程等待性能
---
.../instructions/WaitForAllCoroutines.cs | 31 ++++++++++++-------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/GFramework.Core/coroutine/instructions/WaitForAllCoroutines.cs b/GFramework.Core/coroutine/instructions/WaitForAllCoroutines.cs
index 54d3e2f..c0a33a8 100644
--- a/GFramework.Core/coroutine/instructions/WaitForAllCoroutines.cs
+++ b/GFramework.Core/coroutine/instructions/WaitForAllCoroutines.cs
@@ -5,23 +5,30 @@ namespace GFramework.Core.coroutine.instructions;
///
/// 等待所有协程完成的等待指令
///
-public sealed class WaitForAllCoroutines(
- CoroutineScheduler scheduler,
- IReadOnlyList handles)
- : IYieldInstruction
+public sealed class WaitForAllCoroutines : IYieldInstruction
{
- private readonly IReadOnlyList _handles =
- handles ?? throw new ArgumentNullException(nameof(handles));
+ private readonly IReadOnlyList _handles;
+ private readonly CoroutineScheduler _scheduler;
+ private bool _isDone;
- private readonly CoroutineScheduler _scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
+ public WaitForAllCoroutines(
+ CoroutineScheduler scheduler,
+ IReadOnlyList handles)
+ {
+ _scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
+ _handles = handles ?? throw new ArgumentNullException(nameof(handles));
+
+ // 空列表直接完成
+ _isDone = _handles.Count == 0;
+ }
public void Update(double deltaTime)
{
- // 不需要做任何事
+ if (_isDone) return;
+
+ // 检查所有协程是否都已完成
+ _isDone = _handles.All(handle => !_scheduler.IsCoroutineAlive(handle));
}
- public bool IsDone
- {
- get { return _handles.All(handle => !_scheduler.IsCoroutineAlive(handle)); }
- }
+ public bool IsDone => _isDone;
}
\ No newline at end of file