From 1f724103bd6f6427f1c90d043e98433942ea731e Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sun, 15 Feb 2026 18:18:59 +0800
Subject: [PATCH] =?UTF-8?q?feat(state):=20=E6=B7=BB=E5=8A=A0=E5=BC=82?=
=?UTF-8?q?=E6=AD=A5=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E5=9F=BA=E7=A1=80?=
=?UTF-8?q?=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 新增 IAsyncState 接口定义异步状态行为和转换规则
- 实现 AsyncContextAwareStateBase 基类提供上下文感知异步状态功能
- 添加异步状态进入、退出和转换判断的核心方法
- 集成架构上下文访问能力支持状态管理
- 实现资源销毁和清理机制
---
.../state/IAsyncState.cs | 39 ++++++++
.../state/AsyncContextAwareStateBase.cs | 89 +++++++++++++++++++
2 files changed, 128 insertions(+)
create mode 100644 GFramework.Core.Abstractions/state/IAsyncState.cs
create mode 100644 GFramework.Core/state/AsyncContextAwareStateBase.cs
diff --git a/GFramework.Core.Abstractions/state/IAsyncState.cs b/GFramework.Core.Abstractions/state/IAsyncState.cs
new file mode 100644
index 0000000..729b8ae
--- /dev/null
+++ b/GFramework.Core.Abstractions/state/IAsyncState.cs
@@ -0,0 +1,39 @@
+// 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.
+
+namespace GFramework.Core.Abstractions.state;
+
+///
+/// 异步状态机状态接口,定义了状态的异步行为和转换规则
+///
+public interface IAsyncState
+{
+ ///
+ /// 当状态被激活进入时异步调用
+ ///
+ /// 从哪个状态转换而来,可能为null表示初始状态
+ Task OnEnterAsync(IState? from);
+
+ ///
+ /// 当状态退出时异步调用
+ ///
+ /// 将要转换到的目标状态,可能为null表示结束状态
+ Task OnExitAsync(IState? to);
+
+ ///
+ /// 异步判断当前状态是否可以转换到目标状态
+ ///
+ /// 目标状态
+ /// 如果可以转换则返回true,否则返回false
+ Task CanTransitionToAsync(IState target);
+}
\ No newline at end of file
diff --git a/GFramework.Core/state/AsyncContextAwareStateBase.cs b/GFramework.Core/state/AsyncContextAwareStateBase.cs
new file mode 100644
index 0000000..77dfdec
--- /dev/null
+++ b/GFramework.Core/state/AsyncContextAwareStateBase.cs
@@ -0,0 +1,89 @@
+// 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.architecture;
+using GFramework.Core.Abstractions.rule;
+using GFramework.Core.Abstractions.state;
+using IDisposable = GFramework.Core.Abstractions.lifecycle.IDisposable;
+
+namespace GFramework.Core.state;
+
+///
+/// 上下文感知异步状态基类
+/// 提供基础的异步状态管理功能和架构上下文访问能力
+/// 实现了IAsyncState和IContextAware接口
+///
+public class AsyncContextAwareStateBase : IAsyncState, IContextAware, IDisposable
+{
+ ///
+ /// 架构上下文引用,用于访问架构相关的服务和数据
+ ///
+ private IArchitectureContext? _context;
+
+ ///
+ /// 异步进入状态时调用的方法
+ /// 子类可重写此方法以实现特定的异步状态进入逻辑(如加载资源、初始化数据等)
+ ///
+ /// 从哪个状态转换而来,可能为null表示初始状态
+ public virtual Task OnEnterAsync(IState? from)
+ {
+ return Task.CompletedTask;
+ }
+
+ ///
+ /// 异步退出状态时调用的方法
+ /// 子类可重写此方法以实现特定的异步状态退出逻辑(如保存数据、清理资源等)
+ ///
+ /// 将要转换到的目标状态,可能为null表示结束状态
+ public virtual Task OnExitAsync(IState? to)
+ {
+ return Task.CompletedTask;
+ }
+
+ ///
+ /// 异步判断当前状态是否可以转换到目标状态
+ /// 子类可重写此方法以实现自定义的异步状态转换规则(如验证条件、检查权限等)
+ ///
+ /// 希望转换到的目标状态对象
+ /// 如果允许转换则返回true,否则返回false
+ public virtual Task CanTransitionToAsync(IState target)
+ {
+ return Task.FromResult(true);
+ }
+
+ ///
+ /// 设置架构上下文
+ ///
+ /// 架构上下文实例
+ public void SetContext(IArchitectureContext context)
+ {
+ _context = context;
+ }
+
+ ///
+ /// 获取架构上下文
+ ///
+ /// 架构上下文实例
+ public IArchitectureContext GetContext()
+ {
+ return _context!;
+ }
+
+ ///
+ /// 销毁当前状态,释放相关资源
+ /// 子类可重写此方法以执行特定的清理操作
+ ///
+ public virtual void Destroy()
+ {
+ }
+}
\ No newline at end of file