50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
#nullable enable
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using Cysharp.Threading.Tasks;
|
||
using ShrinkApp;
|
||
using ShrinkContext;
|
||
using ShrinkCommand;
|
||
|
||
namespace ShrinkCommand.Integration.App
|
||
{
|
||
/// <summary>
|
||
/// ShrinkCommand 的原生 Cordis 组件(阶段 3):
|
||
/// 提供 <c>shrink.service.command</c> 余效应键(默认命令服务),依赖者按键注入;
|
||
/// 与经典 ShrinkCommandAppInstaller(ModuleId "shrink.command")二选一使用(同键供给冲突保护)。
|
||
/// </summary>
|
||
public sealed class ShrinkCommandAppComponent : IShrinkComponent
|
||
{
|
||
public const string ServiceKey = "shrink.service.command";
|
||
public const string ModuleKey = "app.module.shrink.command";
|
||
|
||
private static readonly string[] ProvideKeys = { ModuleKey, ServiceKey };
|
||
|
||
public string Name => "shrink.command";
|
||
|
||
public IReadOnlyList<string> Inject => Array.Empty<string>();
|
||
|
||
public IReadOnlyList<string> Provide => ProvideKeys;
|
||
|
||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||
{
|
||
var service = ShrinkCommandRuntime.Default;
|
||
ctx.Set(ServiceKey, service);
|
||
ctx.Set(ModuleKey, Name);
|
||
|
||
if (config is ShrinkAppServices appServices)
|
||
{
|
||
var facade = new ShrinkCommandAppService(service);
|
||
appServices.Register(facade);
|
||
ctx.EffectInverse(() =>
|
||
{
|
||
appServices.TryUnregister(facade);
|
||
return UniTask.CompletedTask;
|
||
});
|
||
}
|
||
|
||
return UniTask.CompletedTask;
|
||
}
|
||
}
|
||
}
|