mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 12:21:32 +08:00
- 将 Store 类标记为 sealed 以防止继承 - 引入独立的 dispatch 门闩锁,将状态锁的保护范围缩小为仅保护临界区访问 - 实现 dispatch 过程中的快照机制,确保中间件和 reducer 在锁外执行稳定的不可变序列 - 重构 ExecuteDispatchPipeline 方法,接受快照参数并改为静态方法 - 添加 CreateReducerSnapshot 方法为每次分发创建 reducer 快照 - 更新 StoreBuilder 和 StoreSelection 类为 sealed - 新增测试用例验证长时间运行的 middleware 不会阻塞状态读取和订阅操作 - 修复 dispatch 过程中状态锁占用时间过长的问题,提升并发性能
88 lines
3.2 KiB
C#
88 lines
3.2 KiB
C#
using GFramework.Core.Abstractions.StateManagement;
|
|
|
|
namespace GFramework.Core.StateManagement;
|
|
|
|
/// <summary>
|
|
/// Store 构建器的默认实现。
|
|
/// 该类型用于在 Store 创建之前集中配置比较器、reducer 和中间件,适合模块安装和测试工厂场景。
|
|
/// </summary>
|
|
/// <typeparam name="TState">状态树的根状态类型。</typeparam>
|
|
public sealed class StoreBuilder<TState> : IStoreBuilder<TState>
|
|
{
|
|
/// <summary>
|
|
/// 延迟应用到 Store 的配置操作列表。
|
|
/// 采用延迟配置而不是直接缓存 reducer 适配器,可复用 Store 自身的注册和验证逻辑。
|
|
/// </summary>
|
|
private readonly List<Action<Store<TState>>> _configurators = [];
|
|
|
|
/// <summary>
|
|
/// 状态比较器。
|
|
/// </summary>
|
|
private IEqualityComparer<TState>? _comparer;
|
|
|
|
/// <summary>
|
|
/// 添加一个 Store 中间件。
|
|
/// </summary>
|
|
/// <param name="middleware">要添加的中间件。</param>
|
|
/// <returns>当前构建器实例。</returns>
|
|
public IStoreBuilder<TState> UseMiddleware(IStoreMiddleware<TState> middleware)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(middleware);
|
|
_configurators.Add(store => store.UseMiddleware(middleware));
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 基于给定初始状态创建一个新的 Store。
|
|
/// </summary>
|
|
/// <param name="initialState">Store 的初始状态。</param>
|
|
/// <returns>已应用当前构建器配置的 Store 实例。</returns>
|
|
public IStore<TState> Build(TState initialState)
|
|
{
|
|
var store = new Store<TState>(initialState, _comparer);
|
|
foreach (var configurator in _configurators)
|
|
{
|
|
configurator(store);
|
|
}
|
|
|
|
return store;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 添加一个强类型 reducer。
|
|
/// </summary>
|
|
/// <typeparam name="TAction">当前 reducer 处理的 action 类型。</typeparam>
|
|
/// <param name="reducer">要添加的 reducer。</param>
|
|
/// <returns>当前构建器实例。</returns>
|
|
public IStoreBuilder<TState> AddReducer<TAction>(IReducer<TState, TAction> reducer)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(reducer);
|
|
_configurators.Add(store => store.RegisterReducer(reducer));
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 配置状态比较器。
|
|
/// </summary>
|
|
/// <param name="comparer">状态比较器。</param>
|
|
/// <returns>当前构建器实例。</returns>
|
|
public IStoreBuilder<TState> WithComparer(IEqualityComparer<TState> comparer)
|
|
{
|
|
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用委托快速添加一个 reducer。
|
|
/// </summary>
|
|
/// <typeparam name="TAction">当前 reducer 处理的 action 类型。</typeparam>
|
|
/// <param name="reducer">执行归约的委托。</param>
|
|
/// <returns>当前构建器实例。</returns>
|
|
public IStoreBuilder<TState> AddReducer<TAction>(Func<TState, TAction, TState> reducer)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(reducer);
|
|
_configurators.Add(store => store.RegisterReducer(reducer));
|
|
return this;
|
|
}
|
|
} |