- WaitEventAsync<T>() 增加可选 CancellationToken
- WaitSuccessOrFailureAsync<TSuccess, TFailure>() 增加可选 CancellationToken
- 保持现有调用兼容:原来的参数顺序和默认行为不变
- 抽出统一完成/清理逻辑:
- 成功
- 失败
- 超时
- 取消
- 防止重复完成导致重复 Unsubscribe
- 超时任务在事件完成后会被取消,避免后台 delay 继续跑
- 取消时会反订阅事件,适合后续 Procedure 生命周期迁移
222 lines
7.8 KiB
C#
222 lines
7.8 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using GameFramework.Event;
|
|
|
|
namespace SepCore.AsyncTask
|
|
{
|
|
/// <summary>
|
|
/// 异步任务辅助类,用于将事件回调转换为 UniTask
|
|
/// </summary>
|
|
public static class AsyncTaskHelper
|
|
{
|
|
/// <summary>
|
|
/// 等待指定事件触发
|
|
/// </summary>
|
|
/// <typeparam name="T">事件参数类型</typeparam>
|
|
/// <param name="eventId">事件ID</param>
|
|
/// <param name="predicate">事件过滤条件</param>
|
|
/// <param name="timeout">超时时间(秒),0表示不超时</param>
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
/// <returns>事件参数</returns>
|
|
public static UniTask<T> WaitEventAsync<T>(
|
|
int eventId,
|
|
Func<T, bool> predicate = null,
|
|
float timeout = 0f,
|
|
CancellationToken cancellationToken = default) where T : GameEventArgs
|
|
{
|
|
var tcs = new UniTaskCompletionSource<T>();
|
|
var eventComponent = global::GameEntry.Event;
|
|
var timeoutCancellationTokenSource = timeout > 0f ? new CancellationTokenSource() : null;
|
|
CancellationTokenRegistration cancellationRegistration = default;
|
|
int isCompleted = 0;
|
|
|
|
EventHandler<GameEventArgs> handler = null;
|
|
void Cleanup(bool disposeCancellationRegistration)
|
|
{
|
|
eventComponent.Unsubscribe(eventId, handler);
|
|
timeoutCancellationTokenSource?.Cancel();
|
|
timeoutCancellationTokenSource?.Dispose();
|
|
if (disposeCancellationRegistration)
|
|
{
|
|
cancellationRegistration.Dispose();
|
|
}
|
|
}
|
|
|
|
bool TryComplete(bool disposeCancellationRegistration = true)
|
|
{
|
|
if (Interlocked.Exchange(ref isCompleted, 1) != 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Cleanup(disposeCancellationRegistration);
|
|
return true;
|
|
}
|
|
|
|
handler = (_, e) =>
|
|
{
|
|
var args = e as T;
|
|
if (args == null) return;
|
|
|
|
if (predicate != null && !predicate(args)) return;
|
|
|
|
if (TryComplete())
|
|
{
|
|
tcs.TrySetResult(args);
|
|
}
|
|
};
|
|
|
|
eventComponent.Subscribe(eventId, handler);
|
|
|
|
if (cancellationToken.CanBeCanceled)
|
|
{
|
|
cancellationRegistration = cancellationToken.Register(() =>
|
|
{
|
|
if (TryComplete(false))
|
|
{
|
|
tcs.TrySetCanceled(cancellationToken);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 超时处理
|
|
if (timeoutCancellationTokenSource != null)
|
|
{
|
|
WaitTimeoutAsync(timeout, timeoutCancellationTokenSource.Token, () =>
|
|
{
|
|
if (TryComplete())
|
|
{
|
|
tcs.TrySetException(new TimeoutException($"等待事件超时: {timeout}秒"));
|
|
}
|
|
}).Forget();
|
|
}
|
|
|
|
return tcs.Task;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 等待成功或失败事件
|
|
/// </summary>
|
|
/// <typeparam name="TSuccess">成功事件参数类型</typeparam>
|
|
/// <typeparam name="TFailure">失败事件参数类型</typeparam>
|
|
/// <param name="successEventId">成功事件ID</param>
|
|
/// <param name="failureEventId">失败事件ID</param>
|
|
/// <param name="successPredicate">成功事件过滤条件</param>
|
|
/// <param name="failurePredicate">失败事件过滤条件</param>
|
|
/// <param name="timeout">超时时间(秒),0表示不超时</param>
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
/// <returns>成功事件参数</returns>
|
|
public static UniTask<TSuccess> WaitSuccessOrFailureAsync<TSuccess, TFailure>(
|
|
int successEventId,
|
|
int failureEventId,
|
|
Func<TSuccess, bool> successPredicate = null,
|
|
Func<TFailure, bool> failurePredicate = null,
|
|
float timeout = 0f,
|
|
CancellationToken cancellationToken = default)
|
|
where TSuccess : GameEventArgs
|
|
where TFailure : GameEventArgs
|
|
{
|
|
var tcs = new UniTaskCompletionSource<TSuccess>();
|
|
var eventComponent = global::GameEntry.Event;
|
|
var timeoutCancellationTokenSource = timeout > 0f ? new CancellationTokenSource() : null;
|
|
CancellationTokenRegistration cancellationRegistration = default;
|
|
int isCompleted = 0;
|
|
|
|
// 先声明两个 handler,再赋值 lambda,避免前向引用
|
|
EventHandler<GameEventArgs> successHandler = null;
|
|
EventHandler<GameEventArgs> failureHandler = null;
|
|
void Cleanup(bool disposeCancellationRegistration)
|
|
{
|
|
eventComponent.Unsubscribe(successEventId, successHandler);
|
|
eventComponent.Unsubscribe(failureEventId, failureHandler);
|
|
timeoutCancellationTokenSource?.Cancel();
|
|
timeoutCancellationTokenSource?.Dispose();
|
|
if (disposeCancellationRegistration)
|
|
{
|
|
cancellationRegistration.Dispose();
|
|
}
|
|
}
|
|
|
|
bool TryComplete(bool disposeCancellationRegistration = true)
|
|
{
|
|
if (Interlocked.Exchange(ref isCompleted, 1) != 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Cleanup(disposeCancellationRegistration);
|
|
return true;
|
|
}
|
|
|
|
successHandler = (_, e) =>
|
|
{
|
|
var args = e as TSuccess;
|
|
if (args == null) return;
|
|
|
|
if (successPredicate != null && !successPredicate(args)) return;
|
|
|
|
if (TryComplete())
|
|
{
|
|
tcs.TrySetResult(args);
|
|
}
|
|
};
|
|
|
|
failureHandler = (_, e) =>
|
|
{
|
|
var args = e as TFailure;
|
|
if (args == null) return;
|
|
|
|
if (failurePredicate != null && !failurePredicate(args)) return;
|
|
|
|
if (TryComplete())
|
|
{
|
|
tcs.TrySetException(new Exception($"操作失败: {args}"));
|
|
}
|
|
};
|
|
|
|
eventComponent.Subscribe(successEventId, successHandler);
|
|
eventComponent.Subscribe(failureEventId, failureHandler);
|
|
|
|
if (cancellationToken.CanBeCanceled)
|
|
{
|
|
cancellationRegistration = cancellationToken.Register(() =>
|
|
{
|
|
if (TryComplete(false))
|
|
{
|
|
tcs.TrySetCanceled(cancellationToken);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 超时处理
|
|
if (timeoutCancellationTokenSource != null)
|
|
{
|
|
WaitTimeoutAsync(timeout, timeoutCancellationTokenSource.Token, () =>
|
|
{
|
|
if (TryComplete())
|
|
{
|
|
tcs.TrySetException(new TimeoutException($"等待事件超时: {timeout}秒"));
|
|
}
|
|
}).Forget();
|
|
}
|
|
|
|
return tcs.Task;
|
|
}
|
|
|
|
private static async UniTaskVoid WaitTimeoutAsync(float timeout, CancellationToken cancellationToken, Action onTimeout)
|
|
{
|
|
try
|
|
{
|
|
await UniTask.Delay(TimeSpan.FromSeconds(timeout), cancellationToken: cancellationToken);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
return;
|
|
}
|
|
|
|
onTimeout?.Invoke();
|
|
}
|
|
}
|
|
}
|