feat(cordis): 接入上下文组合与模组事务热替换
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edcdd81338c534f4295d12a1db2cb2bc
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "ShrinkContext.EventBusAdapter.Runtime",
|
||||
"rootNamespace": "ShrinkContext.EventBusAdapter",
|
||||
"references": [
|
||||
"ShrinkContext.Core.Runtime",
|
||||
"ShrinkEventBus.Runtime",
|
||||
"UniTask"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"autoReferenced": false
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edea41497d7034e4c970a7368a156da4
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,56 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkContext;
|
||||
using ShrinkEventBus;
|
||||
|
||||
namespace ShrinkContext.EventBusAdapter
|
||||
{
|
||||
/// <summary>
|
||||
/// 把 ShrinkEventBus 的订阅/注册包装为可逆效应:
|
||||
/// <c>IShrinkEventSubscription.Dispose</c> 天然就是订阅的逆操作——
|
||||
/// 组件在 apply 里订阅,停用时随上下文自动退订,无需手写卸载路径。
|
||||
/// </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。
|
||||
/// </summary>
|
||||
public static ShrinkEffectHandle EffectRegister(this ShrinkCtx ctx, object target)
|
||||
{
|
||||
if (ctx == null)
|
||||
throw new ArgumentNullException(nameof(ctx));
|
||||
if (target == null)
|
||||
throw new ArgumentNullException(nameof(target));
|
||||
|
||||
EventBus.Register(target);
|
||||
return ctx.EffectInverse(() =>
|
||||
{
|
||||
EventBus.Unregister(target);
|
||||
return UniTask.CompletedTask;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e897f289a1563c34eb3c992463f66fbc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bede8304f5d8c2488e6c0a4c3fce9a8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,97 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using ShrinkContext;
|
||||
using ShrinkEventBus;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ShrinkContext.EventBusAdapter.Tests
|
||||
{
|
||||
public sealed class PingEvent : EventBase
|
||||
{
|
||||
public int Value { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EventBus 订阅/注册即效应:组件停用时自动退订,无需手写卸载路径。
|
||||
/// </summary>
|
||||
public class EventBusEffectTests
|
||||
{
|
||||
private ShrinkContextRuntime _runtime = null!;
|
||||
private ShrinkCtx _ctx = null!;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_runtime = new ShrinkContextRuntime();
|
||||
_ctx = _runtime.RootContext;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_runtime.ShutdownAsync().GetAwaiter().GetResult();
|
||||
EventBus.UnregisterAllEvents();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EffectSubscribe_ReceivesEvents_AndAutoUnsubscribesOnContextDispose()
|
||||
{
|
||||
var received = new List<int>();
|
||||
_ctx.EffectSubscribe<PingEvent>(e => received.Add(e.Value));
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 1 });
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 2 });
|
||||
CollectionAssert.AreEqual(new[] { 1, 2 }, received);
|
||||
|
||||
_ctx.DisposeAsync().GetAwaiter().GetResult();
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 3 });
|
||||
CollectionAssert.AreEqual(new[] { 1, 2 }, received,
|
||||
"上下文回滚后订阅必须自动退订(IShrinkEventSubscription.Dispose 即逆操作)");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EffectSubscribe_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));
|
||||
|
||||
handle.DisposeAsync().GetAwaiter().GetResult();
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 7 });
|
||||
CollectionAssert.AreEqual(new[] { 7 }, first, "手动退订只影响自己的订阅");
|
||||
Assert.IsEmpty(second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EffectRegister_ObjectSubscribers_AutoUnregisterOnContextDispose()
|
||||
{
|
||||
var subscriber = new InstanceSubscriber();
|
||||
_ctx.EffectRegister(subscriber);
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 5 });
|
||||
Assert.AreEqual(5, subscriber.LastValue);
|
||||
|
||||
_ctx.DisposeAsync().GetAwaiter().GetResult();
|
||||
|
||||
EventBus.TriggerEvent(new PingEvent { Value = 6 });
|
||||
Assert.AreEqual(5, subscriber.LastValue, "实例注册随上下文回滚自动注销");
|
||||
}
|
||||
|
||||
private sealed class InstanceSubscriber
|
||||
{
|
||||
public int LastValue { get; private set; }
|
||||
|
||||
[EventSubscribe]
|
||||
public void OnPing(PingEvent e)
|
||||
{
|
||||
LastValue = e.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 675a7f1a5e7d2974d81a29504efd1367
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "ShrinkContext.EventBusAdapter.Tests",
|
||||
"rootNamespace": "ShrinkContext.EventBusAdapter.Tests",
|
||||
"references": [
|
||||
"ShrinkContext.EventBusAdapter.Runtime",
|
||||
"ShrinkContext.Core.Runtime",
|
||||
"ShrinkEventBus.Runtime",
|
||||
"UniTask",
|
||||
"UnityEngine.TestRunner",
|
||||
"UnityEditor.TestRunner"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca6bd51b2cb359840a7d8cc5ceb8831b
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "com.cneicy.shrink-context-eventbus-adapter",
|
||||
"version": "0.1.0",
|
||||
"displayName": "ShrinkContext - EventBus Adapter",
|
||||
"description": "把 ShrinkEventBus 订阅/注册包装为 ShrinkContext 可逆效应:订阅即效应,停用自动退订。",
|
||||
"unity": "2022.3",
|
||||
"dependencies": {
|
||||
"com.cneicy.shrink-context-core": "0.1.0",
|
||||
"com.cneicy.shrink-eventbus": "1.3.0",
|
||||
"com.cysharp.unitask": "2.5.10"
|
||||
},
|
||||
"keywords": [
|
||||
"context",
|
||||
"cordis",
|
||||
"eventbus",
|
||||
"effect"
|
||||
],
|
||||
"author": {
|
||||
"name": "cneicy",
|
||||
"url": "https://github.com/cneicy"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3da06949de60a124e8b50d3ed14401ed
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user