37 lines
1.4 KiB
C#
37 lines
1.4 KiB
C#
#nullable enable
|
|
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using ShrinkContext;
|
|
|
|
namespace ShrinkCommand.Integration
|
|
{
|
|
/// <summary>通过命令服务键管理 Command ↔ EventBus 桥的可逆生命周期。</summary>
|
|
public sealed class ShrinkCommandEventBusComponent : IShrinkComponent
|
|
{
|
|
public const string CommandServiceKey = "shrink.service.command";
|
|
public const string ProvideKey = "shrink.integration.command-eventbus";
|
|
|
|
private static readonly string[] InjectKeys = { CommandServiceKey };
|
|
private static readonly string[] ProvideKeys = { ProvideKey };
|
|
|
|
public string Name => "shrink.integration.command-eventbus";
|
|
public IReadOnlyList<string> Inject => InjectKeys;
|
|
public IReadOnlyList<string> Provide => ProvideKeys;
|
|
|
|
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
|
{
|
|
var service = ctx.Get<ShrinkCommandService>(CommandServiceKey);
|
|
var options = config as ShrinkCommandEventBusBridgeOptions;
|
|
ShrinkCommandEventBusBridge.RegisterService(service, options);
|
|
ctx.EffectInverse(() =>
|
|
{
|
|
ShrinkCommandEventBusBridge.UnregisterService(service);
|
|
return UniTask.CompletedTask;
|
|
});
|
|
ctx.Set(ProvideKey, Name);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
}
|
|
}
|