36 lines
1.3 KiB
C#
36 lines
1.3 KiB
C#
#nullable enable
|
|
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using ShrinkContext;
|
|
|
|
namespace ShrinkNetwork.Integration
|
|
{
|
|
/// <summary>通过网络服务键管理 Network ↔ EventBus 桥的可逆生命周期。</summary>
|
|
public sealed class ShrinkNetworkEventBusComponent : IShrinkComponent
|
|
{
|
|
public const string NetworkServiceKey = "shrink.service.network";
|
|
public const string ProvideKey = "shrink.integration.network-eventbus";
|
|
|
|
private static readonly string[] InjectKeys = { NetworkServiceKey };
|
|
private static readonly string[] ProvideKeys = { ProvideKey };
|
|
|
|
public string Name => "shrink.integration.network-eventbus";
|
|
public IReadOnlyList<string> Inject => InjectKeys;
|
|
public IReadOnlyList<string> Provide => ProvideKeys;
|
|
|
|
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
|
{
|
|
var service = ctx.Get<ShrinkNetworkService>(NetworkServiceKey);
|
|
service.UseEventBusBridge(config as ShrinkNetworkEventBusBridgeOptions);
|
|
ctx.EffectInverse(() =>
|
|
{
|
|
ShrinkNetworkEventBusBridge.UnregisterService(service);
|
|
return UniTask.CompletedTask;
|
|
});
|
|
ctx.Set(ProvideKey, Name);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
}
|
|
}
|