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:
@@ -7,48 +7,25 @@ using ShrinkEventBus;
|
||||
namespace ShrinkContext.EventBusAdapter
|
||||
{
|
||||
/// <summary>
|
||||
/// 把 ShrinkEventBus 的订阅/注册包装为可逆效应:
|
||||
/// <c>IShrinkEventSubscription.Dispose</c> 天然就是订阅的逆操作——
|
||||
/// 组件在 apply 里订阅,停用时随上下文自动退订,无需手写卸载路径。
|
||||
/// 把生成的 ShrinkEventBus 绑定包装为可逆效应。
|
||||
/// </summary>
|
||||
public static class ShrinkEventBusEffects
|
||||
{
|
||||
/// <summary>订阅事件即效应:ctx 卸载或手动 Dispose 句柄时自动退订。</summary>
|
||||
public static ShrinkEffectHandle EffectSubscribe<TEvent>(
|
||||
this ShrinkCtx ctx,
|
||||
Action<TEvent> handler,
|
||||
EventPriority priority = EventPriority.NORMAL,
|
||||
bool receiveCanceled = false)
|
||||
where TEvent : EventBase
|
||||
{
|
||||
if (ctx == null)
|
||||
throw new ArgumentNullException(nameof(ctx));
|
||||
if (handler == null)
|
||||
throw new ArgumentNullException(nameof(handler));
|
||||
|
||||
var subscription = EventBus.SubscribeEvent(handler, priority, receiveCanceled);
|
||||
return ctx.EffectInverse(() =>
|
||||
{
|
||||
subscription.Dispose();
|
||||
return UniTask.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实例对象整体注册即效应:扫描对象上的 [EventSubscribe] 方法(含未织入的非 MonoBehaviour 类),
|
||||
/// 逆操作为 Unregister。
|
||||
/// 激活生成的 [ShrinkSubscribe] 绑定,并在 Context 回滚时统一释放。
|
||||
/// </summary>
|
||||
public static ShrinkEffectHandle EffectRegister(this ShrinkCtx ctx, object target)
|
||||
public static ShrinkEffectHandle EffectAttach(this ShrinkCtx ctx, object target,
|
||||
ShrinkBusKey? defaultBus = null)
|
||||
{
|
||||
if (ctx == null)
|
||||
throw new ArgumentNullException(nameof(ctx));
|
||||
if (target == null)
|
||||
throw new ArgumentNullException(nameof(target));
|
||||
|
||||
EventBus.Register(target);
|
||||
var binding = EventBus.Attach(target, defaultBus);
|
||||
return ctx.EffectInverse(() =>
|
||||
{
|
||||
EventBus.Unregister(target);
|
||||
binding.Dispose();
|
||||
return UniTask.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"unity": "2022.3",
|
||||
"dependencies": {
|
||||
"com.cneicy.shrink-context-core": "0.1.0",
|
||||
"com.cneicy.shrink-eventbus": "1.3.0",
|
||||
"com.cneicy.shrink-eventbus": "2.0.0",
|
||||
"com.cysharp.unitask": "2.5.10"
|
||||
},
|
||||
"keywords": [
|
||||
@@ -19,4 +19,4 @@
|
||||
"name": "cneicy",
|
||||
"url": "https://github.com/cneicy"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user