feat(sdk): migrate to EventBus 2.0
Replace the legacy EventBase runtime with generated multi-bus bindings and explicit scheduling. Migrate app, data, network, demo, and mod consumers; add generated network-event registration and owner-scoped mod content overrides.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
@@ -9,7 +8,7 @@ using UnityEngine;
|
||||
|
||||
namespace ShrinkContext.EventBusAdapter.Tests
|
||||
{
|
||||
public sealed class PingEvent : EventBase
|
||||
public sealed class PingEvent : IShrinkEvent
|
||||
{
|
||||
public int Value { get; set; }
|
||||
}
|
||||
@@ -33,64 +32,74 @@ namespace ShrinkContext.EventBusAdapter.Tests
|
||||
public void TearDown()
|
||||
{
|
||||
_runtime.ShutdownAsync().GetAwaiter().GetResult();
|
||||
EventBus.UnregisterAllEvents();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EffectSubscribe_ReceivesEvents_AndAutoUnsubscribesOnContextDispose()
|
||||
public void EffectAttach_ReceivesEvents_AndAutoUnsubscribesOnContextDispose()
|
||||
{
|
||||
var received = new List<int>();
|
||||
_ctx.EffectSubscribe<PingEvent>(e => received.Add(e.Value));
|
||||
var subscriber = new InstanceSubscriber(received);
|
||||
_ctx.EffectAttach(subscriber);
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 1 });
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 2 });
|
||||
EventBus.Post(new PingEvent { Value = 1 });
|
||||
EventBus.Post(new PingEvent { Value = 2 });
|
||||
CollectionAssert.AreEqual(new[] { 1, 2 }, received);
|
||||
|
||||
_ctx.DisposeAsync().GetAwaiter().GetResult();
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 3 });
|
||||
EventBus.Post(new PingEvent { Value = 3 });
|
||||
CollectionAssert.AreEqual(new[] { 1, 2 }, received,
|
||||
"上下文回滚后订阅必须自动退订(IShrinkEventSubscription.Dispose 即逆操作)");
|
||||
"上下文回滚后 EventBus binding 必须通过 IDisposable.Dispose 自动退订");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EffectSubscribe_ManualDispose_OnlyUnsubscribesOwn()
|
||||
public void EffectAttach_ManualDispose_OnlyUnsubscribesOwn()
|
||||
{
|
||||
var first = new List<int>();
|
||||
var second = new List<int>();
|
||||
_ctx.EffectSubscribe<PingEvent>(e => first.Add(e.Value));
|
||||
var handle = _ctx.EffectSubscribe<PingEvent>(e => second.Add(e.Value));
|
||||
_ctx.EffectAttach(new InstanceSubscriber(first));
|
||||
var handle = _ctx.EffectAttach(new InstanceSubscriber(second));
|
||||
|
||||
handle.DisposeAsync().GetAwaiter().GetResult();
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 7 });
|
||||
EventBus.Post(new PingEvent { Value = 7 });
|
||||
CollectionAssert.AreEqual(new[] { 7 }, first, "手动退订只影响自己的订阅");
|
||||
Assert.IsEmpty(second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EffectRegister_ObjectSubscribers_AutoUnregisterOnContextDispose()
|
||||
public void EffectAttach_ObjectSubscribers_AutoUnregisterOnContextDispose()
|
||||
{
|
||||
var subscriber = new InstanceSubscriber();
|
||||
_ctx.EffectRegister(subscriber);
|
||||
var values = new List<int>();
|
||||
var subscriber = new InstanceSubscriber(values);
|
||||
_ctx.EffectAttach(subscriber);
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 5 });
|
||||
EventBus.Post(new PingEvent { Value = 5 });
|
||||
Assert.AreEqual(5, subscriber.LastValue);
|
||||
|
||||
_ctx.DisposeAsync().GetAwaiter().GetResult();
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 6 });
|
||||
EventBus.Post(new PingEvent { Value = 6 });
|
||||
Assert.AreEqual(5, subscriber.LastValue, "实例注册随上下文回滚自动注销");
|
||||
}
|
||||
|
||||
[ShrinkEventSubscriber(DefaultBus = "game")]
|
||||
private sealed class InstanceSubscriber
|
||||
{
|
||||
private readonly List<int> _values;
|
||||
|
||||
public InstanceSubscriber(List<int> values)
|
||||
{
|
||||
_values = values;
|
||||
}
|
||||
|
||||
public int LastValue { get; private set; }
|
||||
|
||||
[EventSubscribe]
|
||||
[ShrinkSubscribe]
|
||||
public void OnPing(PingEvent e)
|
||||
{
|
||||
LastValue = e.Value;
|
||||
_values.Add(e.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user