mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 20:34:29 +08:00
- 新增 RunInBatch() 方法支持批处理通知折叠 - 添加 Undo()/Redo() 基于历史缓冲区的状态回退前进功能 - 实现 TimeTravelTo() 跳转到指定历史索引的能力 - 提供 ClearHistory() 以当前状态重置历史锚点的功能 - 支持可选历史缓冲区、撤销/重做和时间旅行功能 - 添加可选批处理通知折叠机制 - 实现多态 action 匹配(基类/接口)支持 - 在诊断信息中增加历史游标和批处理状态 - StoreBuilder 新增 WithHistoryCapacity() 和 WithActionMatching() 配置方法 - 优化 reducer 注册支持全局序号以实现稳定排序 - 实现多态匹配时的类型继承距离计算 - 添加批处理嵌套支持和状态通知延迟机制
92 lines
3.5 KiB
C#
92 lines
3.5 KiB
C#
using GFramework.Core.Events;
|
|
|
|
namespace GFramework.Core.Tests.StateManagement;
|
|
|
|
/// <summary>
|
|
/// Store 到 EventBus 桥接扩展的单元测试。
|
|
/// 这些测试验证旧模块兼容桥接能够正确转发 dispatch 和状态变化事件,并支持运行时拆除。
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class StoreEventBusExtensionsTests
|
|
{
|
|
/// <summary>
|
|
/// 测试桥接会发布每次 dispatch 事件,并对批处理后的状态变化只发送一次最终状态事件。
|
|
/// </summary>
|
|
[Test]
|
|
public void BridgeToEventBus_Should_Publish_Dispatches_And_Collapsed_State_Changes()
|
|
{
|
|
var eventBus = new EventBus();
|
|
var store = CreateStore();
|
|
var dispatchedEvents = new List<StoreDispatchedEvent<CounterState>>();
|
|
var stateChangedEvents = new List<StoreStateChangedEvent<CounterState>>();
|
|
|
|
eventBus.Register<StoreDispatchedEvent<CounterState>>(dispatchedEvents.Add);
|
|
eventBus.Register<StoreStateChangedEvent<CounterState>>(stateChangedEvents.Add);
|
|
|
|
store.BridgeToEventBus(eventBus);
|
|
|
|
store.Dispatch(new IncrementAction(1));
|
|
store.RunInBatch(() =>
|
|
{
|
|
store.Dispatch(new IncrementAction(1));
|
|
store.Dispatch(new IncrementAction(1));
|
|
});
|
|
|
|
Assert.That(dispatchedEvents.Count, Is.EqualTo(3));
|
|
Assert.That(dispatchedEvents[0].DispatchRecord.NextState.Count, Is.EqualTo(1));
|
|
Assert.That(dispatchedEvents[2].DispatchRecord.NextState.Count, Is.EqualTo(3));
|
|
|
|
Assert.That(stateChangedEvents.Count, Is.EqualTo(2));
|
|
Assert.That(stateChangedEvents[0].State.Count, Is.EqualTo(1));
|
|
Assert.That(stateChangedEvents[1].State.Count, Is.EqualTo(3));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试桥接句柄注销后不会再继续向 EventBus 发送事件。
|
|
/// </summary>
|
|
[Test]
|
|
public void BridgeToEventBus_UnRegister_Should_Stop_Future_Publications()
|
|
{
|
|
var eventBus = new EventBus();
|
|
var store = CreateStore();
|
|
var dispatchedEvents = new List<StoreDispatchedEvent<CounterState>>();
|
|
var stateChangedEvents = new List<StoreStateChangedEvent<CounterState>>();
|
|
|
|
eventBus.Register<StoreDispatchedEvent<CounterState>>(dispatchedEvents.Add);
|
|
eventBus.Register<StoreStateChangedEvent<CounterState>>(stateChangedEvents.Add);
|
|
|
|
var bridge = store.BridgeToEventBus(eventBus);
|
|
|
|
store.Dispatch(new IncrementAction(1));
|
|
bridge.UnRegister();
|
|
store.Dispatch(new IncrementAction(1));
|
|
|
|
Assert.That(dispatchedEvents.Count, Is.EqualTo(1));
|
|
Assert.That(stateChangedEvents.Count, Is.EqualTo(1));
|
|
Assert.That(stateChangedEvents[0].State.Count, Is.EqualTo(1));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建一个带基础 reducer 的测试 Store。
|
|
/// </summary>
|
|
/// <returns>测试用 Store 实例。</returns>
|
|
private static Store<CounterState> CreateStore()
|
|
{
|
|
var store = new Store<CounterState>(new CounterState(0, "Player"));
|
|
store.RegisterReducer<IncrementAction>((state, action) => state with { Count = state.Count + action.Amount });
|
|
return store;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用于桥接测试的状态类型。
|
|
/// </summary>
|
|
/// <param name="Count">当前计数值。</param>
|
|
/// <param name="Name">当前名称。</param>
|
|
private sealed record CounterState(int Count, string Name);
|
|
|
|
/// <summary>
|
|
/// 用于桥接测试的计数 action。
|
|
/// </summary>
|
|
/// <param name="Amount">要增加的数量。</param>
|
|
private sealed record IncrementAction(int Amount);
|
|
} |