feat(cordis): 完成阶段5访问介导与诊断
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# ShrinkContext.Core
|
||||
|
||||
《Cordis: A Programming Paradigm for Spatiotemporal Composability》核心机制的 Unity/C# 原型(改造方案见仓库根 `CORDIS_MIGRATION.md` 的阶段 0 产物)。
|
||||
《Cordis: A Programming Paradigm for Spatiotemporal Composability》核心机制的 Unity/C# 实现(改造方案与当前边界见仓库根 `CORDIS_MIGRATION.md`)。
|
||||
|
||||
## 定位
|
||||
|
||||
@@ -9,16 +9,17 @@
|
||||
- **可逆效应**(时间可组合性,论文 3.1 / 算法 1):`ctx.Effect / EffectAsync`,前向执行时产出逆操作,运行时按 LIFO 累积,卸载即回滚;
|
||||
- **响应式余效应**(空间可组合性,论文 3.2 / 算法 2、3):`ctx.Set / Get`,依赖键的安装/撤回自动通知依赖者,组件在依赖全部可用时激活、失效时停用、换提供者时重载;
|
||||
- **纤程生命周期**(论文 4 / 算法 4、5):`runtime.Use(component)` 实例化组件为纤程,惯性状态机 `Loading → Active → Unloading → Inactive`,卸载先等待依赖者排空再回滚自身效应,父卸载级联子卸载。
|
||||
- **声明式协调与访问介导**:加载器以事务更新期望组合,失败时恢复旧组合;`isolate / intercept` 控制解析域与服务视图,组件供给受 `Provide` 声明约束。
|
||||
|
||||
## 目录结构
|
||||
|
||||
| 目录 | 说明 |
|
||||
|---|---|
|
||||
| `Runtime/Context/` | `ShrinkCtx`(一等上下文、隔离派生、介导访问)与 `ShrinkEffectHandle`(算法 1) |
|
||||
| `Runtime/Coeffects/` | 绑定与异常类型(供给冲突、未声明/未激活访问) |
|
||||
| `Runtime/Coeffects/` | 绑定、强类型版本键、访问策略与异常类型 |
|
||||
| `Runtime/Fibers/` | `IShrinkComponent`(inject/provide/apply)、`ShrinkFiber`、分步效应接口 |
|
||||
| `Runtime/Loader/` | 声明式加载器(论文 5.2 原型子集):`ShrinkLoaderEntry` / `ShrinkComponentCatalog` / `ShrinkContextLoader` 增量协调 |
|
||||
| `Runtime/` | `ShrinkContextRuntime`(注册表 + 算法 2-5)、`ShrinkContextDefaults`(Domain Reload 重置) |
|
||||
| `Runtime/Loader/` | 声明式加载器:`ShrinkLoaderEntry` / `ShrinkComponentCatalog` / `ShrinkContextLoader` 增量协调、事务恢复与条目诊断 |
|
||||
| `Runtime/` | `ShrinkContextRuntime`(注册表 + 算法 2-5 + notify 倒排索引)、诊断快照与 Domain Reload 重置 |
|
||||
| `Tests/` | EditMode 测试:对照论文定理 7/16/20/63/64 与算法 1/2/3/4/5/6 的可观察行为 + 加载器协调行为 |
|
||||
|
||||
## 论文 → API 对照
|
||||
@@ -28,12 +29,13 @@
|
||||
| `ctx.effect(callback)` → dispose | `ShrinkCtx.Effect / EffectAsync` → `ShrinkEffectHandle.DisposeAsync`(armed 单次执行,等待进行中前向) |
|
||||
| `ctx.get/set`(算法 2) | `ShrinkCtx.Set<T>`(可逆,安装+通知 / 撤回+通知) |
|
||||
| `ctx.isolate(key, realm)` | `ShrinkCtx.Isolate`(派生子上下文,丢弃即恢复) |
|
||||
| 代理介导访问(算法 6) | `ShrinkCtx.Get<T>/TryGet`(沿纤程链解析已提交视图;未声明/未激活访问抛异常) |
|
||||
| `ctx.intercept(key, metadata)` | `ShrinkCtx.Intercept`(派生访问元数据;provider 通过 `IShrinkCoeffectAccessPolicy` 返回受限视图或拒绝访问) |
|
||||
| 代理介导访问(算法 6) | `ShrinkCtx.Get<T>/TryGet`(沿纤程链解析已提交视图;未声明/未激活/策略拒绝分别抛出不同异常) |
|
||||
| `ctx.use(component)`(算法 4) | `ShrinkContextRuntime.Use`(实例化是父上下文的可逆效应 → 级联卸载;支持条目级 isolate 叠加) |
|
||||
| 声明式配置 + 协调(论文 5.2) | `ShrinkContextLoader.ApplyAsync`(条目消失→退役;disabled→卸载;组件/配置变化→重建;无变化→幂等;依赖缺失→条目保持被管理并等待) |
|
||||
| 声明式配置 + 协调(论文 5.2) | `ShrinkContextLoader.ApplyAsync`(预校验期望组合;条目消失→退役;组件/配置变化→重建;intercept 变化→原位更新;失败→恢复旧组合) |
|
||||
| refresh/reload/unload(算法 5) | `RunTransitionAsync`(惯性链式转换;卸载先 drain 依赖者) |
|
||||
| notify(算法 3) | `ShrinkContextRuntime.Notify`(realm 一致的依赖者刷新) |
|
||||
| target = 提供者 uid 集合 | `ShrinkFiber.Target`(uid 永不复用:同值不同提供者也触发重载) |
|
||||
| notify(算法 3) | `ShrinkContextRuntime.Notify`(`key → inject fibers` 倒排索引选候选,再按 realm 过滤) |
|
||||
| target = 按 inject 顺序解析的提供者 uid | `ShrinkFiber.Target`(保留键顺序与重复依赖;uid 永不复用,因此同值换提供者也会重载) |
|
||||
| `𝔈iterΓ` 分步效应 | `IShrinkIterativeComponent.ApplySteps`(自定义 UniTask 步进器,步进边界守卫 → 部分回滚;不用 IAsyncEnumerable,规避 Mono ValueTask 迭代器的非内联调度) |
|
||||
| fiber.committed 提交视图 | `ShrinkFiber.Committed`(装载前提交,拆除完才丢弃) |
|
||||
|
||||
@@ -41,6 +43,7 @@
|
||||
|
||||
- 定理 7(恢复初始上下文)/ 定理 16(LIFO)/ 选择性撤销 / armed 幂等 / dispose 等待进行中前向
|
||||
- set 可逆性、介导访问纪律(UNDECLARED / INACTIVE)、隔离域派生与丢弃恢复
|
||||
- 未声明供给回滚、访问策略拒绝、intercept 原位更新与只读服务视图
|
||||
- 依赖驱动激活、缺依赖静默非活动、晚到提供者激活
|
||||
- 退役次序:依赖者先于提供者逆操作排空(drain-before-inverse)
|
||||
- 提供者替换重载(含同值替换,uid 语义)
|
||||
@@ -48,14 +51,16 @@
|
||||
- 惯性:装载中途退役永不 Active、链式卸载(论文 4.3.3)
|
||||
- 分步效应部分回滚(定理 64)
|
||||
- apply 失败的部分效应回滚(L-Raise)
|
||||
- 加载器失败恢复旧组合、跨 realm 替换隔离、notify 倒排索引候选计数与运行时诊断
|
||||
- Domain Reload 重置
|
||||
|
||||
## 明确不在本原型内(后续阶段)
|
||||
## 当前边界
|
||||
|
||||
- 声明式配置树加载器与增量协调(阶段 2)
|
||||
- 热模块替换 / ModFramework 合流(阶段 4,受 Unity 程序集不可卸载约束,回滚单位是纤程实例)
|
||||
- 拦截(intercept)与细粒度访问控制(阶段 5)
|
||||
- 主线程泵与 MonoBehaviour 宿主、notify 的 key→fibers 倒排索引(性能)
|
||||
- `intercept` 是经 `ShrinkCtx.Get` 的能力介导,不是对不可信 DLL 的内存、反射、文件或网络沙箱。
|
||||
- loader 的 isolate 变化仍通过重建条目生效;尚未提供运行中 fiber 的原位 realm 迁移。
|
||||
- 强类型版本键是新增契约,现有字符串键不会在阶段 5 被一次性重写。
|
||||
- 尚未提供 ScriptableObject/JSON 配置资产、Editor 调试面和正式容量基准。
|
||||
- 主线程宿主与 Unity PlayerLoop 调度仍由上层应用负责。
|
||||
- 跨进程/独立服务器上下文
|
||||
|
||||
## 约定
|
||||
|
||||
@@ -1,21 +1,92 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ShrinkContext
|
||||
{
|
||||
/// <summary>共享存储中的一条依赖绑定:键解析到 realm 后对应的值与提供者纤程。</summary>
|
||||
public sealed class ShrinkBinding
|
||||
{
|
||||
public ShrinkBinding(object? value, ShrinkFiber? provider, string key)
|
||||
public ShrinkBinding(object? value, ShrinkFiber? provider, string key,
|
||||
IShrinkCoeffectAccessPolicy? accessPolicy = null)
|
||||
{
|
||||
Value = value;
|
||||
Provider = provider;
|
||||
Key = key;
|
||||
AccessPolicy = accessPolicy;
|
||||
}
|
||||
|
||||
public object? Value { get; }
|
||||
public ShrinkFiber? Provider { get; }
|
||||
public string Key { get; }
|
||||
public IShrinkCoeffectAccessPolicy? AccessPolicy { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带命名空间与主版本的类型化余效应键。现有字符串声明可继续使用 <see cref="Id"/>,
|
||||
/// Set/Get 重载则在调用点固定值类型,逐步收口字符串键的碰撞与接口漂移。
|
||||
/// </summary>
|
||||
public sealed class ShrinkKey<T>
|
||||
{
|
||||
public ShrinkKey(string packageName, string name, int majorVersion = 1)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(packageName))
|
||||
throw new ArgumentException("Package name must not be empty.", nameof(packageName));
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Key name must not be empty.", nameof(name));
|
||||
if (majorVersion <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(majorVersion), "Major version must be positive.");
|
||||
|
||||
PackageName = packageName.Trim();
|
||||
Name = name.Trim();
|
||||
MajorVersion = majorVersion;
|
||||
Id = $"{PackageName}/{Name}@v{MajorVersion}";
|
||||
}
|
||||
|
||||
public string PackageName { get; }
|
||||
public string Name { get; }
|
||||
public int MajorVersion { get; }
|
||||
public string Id { get; }
|
||||
public override string ToString() => Id;
|
||||
}
|
||||
|
||||
/// <summary>依赖值被读取时的访问上下文;metadata 来自访问方上下文上的 intercept 合并结果。</summary>
|
||||
public sealed class ShrinkCoeffectAccessContext
|
||||
{
|
||||
internal ShrinkCoeffectAccessContext(string key, Type requestedType, ShrinkFiber? consumer,
|
||||
IReadOnlyDictionary<string, object?> metadata)
|
||||
{
|
||||
Key = key;
|
||||
RequestedType = requestedType;
|
||||
Consumer = consumer;
|
||||
Metadata = metadata;
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public Type RequestedType { get; }
|
||||
public ShrinkFiber? Consumer { get; }
|
||||
public IReadOnlyDictionary<string, object?> Metadata { get; }
|
||||
|
||||
public bool TryGetMetadata<T>(string name, out T value)
|
||||
{
|
||||
if (Metadata.TryGetValue(name, out var raw) && raw is T typed)
|
||||
{
|
||||
value = typed;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = default!;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// provider 侧访问策略。它可以依据 intercept metadata 返回降权包装或拒绝访问,
|
||||
/// 但不会改变依赖满足关系,也不会构成不可信代码沙箱。
|
||||
/// </summary>
|
||||
public interface IShrinkCoeffectAccessPolicy
|
||||
{
|
||||
object? Resolve(ShrinkCoeffectAccessContext context, object? value);
|
||||
}
|
||||
|
||||
/// <summary>访问了纤程链上没有任何一方声明的依赖键(论文算法 6 的 UNDECLARED_ACCESS)。</summary>
|
||||
@@ -42,6 +113,32 @@ namespace ShrinkContext
|
||||
public string Key { get; }
|
||||
}
|
||||
|
||||
/// <summary>组件写入了未在 Provide 中声明的键。</summary>
|
||||
public sealed class ShrinkUndeclaredSupplyException : Exception
|
||||
{
|
||||
public ShrinkUndeclaredSupplyException(string key, string fiberName)
|
||||
: base($"Undeclared coeffect supply: '{key}' is not listed in fiber '{fiberName}' Provide keys.")
|
||||
{
|
||||
Key = key;
|
||||
FiberName = fiberName;
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string FiberName { get; }
|
||||
}
|
||||
|
||||
/// <summary>intercept 访问策略拒绝了本次依赖读取。</summary>
|
||||
public sealed class ShrinkCoeffectAccessDeniedException : Exception
|
||||
{
|
||||
public ShrinkCoeffectAccessDeniedException(string key, string message)
|
||||
: base($"Coeffect access denied for '{key}': {message}")
|
||||
{
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
}
|
||||
|
||||
/// <summary>两个活跃纤程试图供给同一个 realm(论文定义 45 的供给不相交约束)。</summary>
|
||||
public sealed class ShrinkSupplyConflictException : Exception
|
||||
{
|
||||
|
||||
@@ -15,14 +15,17 @@ namespace ShrinkContext
|
||||
{
|
||||
private readonly List<ShrinkEffectHandle> _effects = new();
|
||||
private readonly Dictionary<string, string>? _isolateOverlay;
|
||||
private Dictionary<string, IReadOnlyDictionary<string, object?>>? _interceptOverlay;
|
||||
|
||||
internal ShrinkCtx(ShrinkContextRuntime runtime, ShrinkCtx? parent, ShrinkFiber? ownerFiber,
|
||||
Dictionary<string, string>? isolateOverlay)
|
||||
Dictionary<string, string>? isolateOverlay,
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? interceptOverlay = null)
|
||||
{
|
||||
Runtime = runtime;
|
||||
Parent = parent;
|
||||
OwnerFiber = ownerFiber;
|
||||
_isolateOverlay = isolateOverlay;
|
||||
ReplaceIntercept(interceptOverlay);
|
||||
}
|
||||
|
||||
public ShrinkContextRuntime Runtime { get; }
|
||||
@@ -106,7 +109,17 @@ namespace ShrinkContext
|
||||
/// 提供依赖(set 是可逆效应):立即安装绑定并通知依赖者;
|
||||
/// 逆操作撤回绑定并再次通知。句柄同时挂在当前上下文上,随上下文卸载自动撤回。
|
||||
/// </summary>
|
||||
public ShrinkEffectHandle Set<T>(string key, T value) => Runtime.SetBinding(this, key, value);
|
||||
public ShrinkEffectHandle Set<T>(string key, T value) => Runtime.SetBinding(this, key, value, null);
|
||||
|
||||
/// <summary>提供带访问策略的依赖;策略只在介导 Get 时运行,原始编排查找不会运行策略。</summary>
|
||||
public ShrinkEffectHandle Set<T>(string key, T value, IShrinkCoeffectAccessPolicy accessPolicy) =>
|
||||
Runtime.SetBinding(this, key, value, accessPolicy ?? throw new ArgumentNullException(nameof(accessPolicy)));
|
||||
|
||||
public ShrinkEffectHandle Set<T>(ShrinkKey<T> key, T value) =>
|
||||
Set((key ?? throw new ArgumentNullException(nameof(key))).Id, value);
|
||||
|
||||
public ShrinkEffectHandle Set<T>(ShrinkKey<T> key, T value, IShrinkCoeffectAccessPolicy accessPolicy) =>
|
||||
Set((key ?? throw new ArgumentNullException(nameof(key))).Id, value, accessPolicy);
|
||||
|
||||
/// <summary>
|
||||
/// 代理介导的依赖访问(论文算法 6):沿纤程链解析已提交视图;
|
||||
@@ -119,7 +132,7 @@ namespace ShrinkContext
|
||||
while (fiber != null)
|
||||
{
|
||||
if (fiber.Committed != null && fiber.Committed.TryGetValue(key, out var binding))
|
||||
return (T)binding.Value!;
|
||||
return Runtime.ResolveAccess<T>(this, key, binding);
|
||||
if (fiber.InjectSet.Contains(key))
|
||||
throw new ShrinkInactiveAccessException(key, fiber.Name);
|
||||
fiber = fiber.Parent;
|
||||
@@ -128,6 +141,9 @@ namespace ShrinkContext
|
||||
throw new ShrinkUndeclaredAccessException(key);
|
||||
}
|
||||
|
||||
public T Get<T>(ShrinkKey<T> key) => Get<T>(
|
||||
(key ?? throw new ArgumentNullException(nameof(key))).Id);
|
||||
|
||||
/// <summary>与 <see cref="Get{T}"/> 相同的介导规则;未激活返回 false 而不抛异常。</summary>
|
||||
public bool TryGet<T>(string key, out T value)
|
||||
{
|
||||
@@ -136,7 +152,7 @@ namespace ShrinkContext
|
||||
{
|
||||
if (fiber.Committed != null && fiber.Committed.TryGetValue(key, out var binding))
|
||||
{
|
||||
value = (T)binding.Value!;
|
||||
value = Runtime.ResolveAccess<T>(this, key, binding);
|
||||
return true;
|
||||
}
|
||||
if (fiber.InjectSet.Contains(key))
|
||||
@@ -148,6 +164,9 @@ namespace ShrinkContext
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGet<T>(ShrinkKey<T> key, out T value) => TryGet(
|
||||
(key ?? throw new ArgumentNullException(nameof(key))).Id, out value);
|
||||
|
||||
/// <summary>
|
||||
/// 派生隔离子上下文(论文定义 28/29 的派生实现):
|
||||
/// 覆盖 key 的 realm 解析;父上下文不受影响;丢弃子上下文即隐式恢复。
|
||||
@@ -162,6 +181,33 @@ namespace ShrinkContext
|
||||
return new ShrinkCtx(Runtime, this, OwnerFiber, new Dictionary<string, string> { [key] = realm });
|
||||
}
|
||||
|
||||
public ShrinkCtx Isolate<T>(ShrinkKey<T> key, string realm) => Isolate(
|
||||
(key ?? throw new ArgumentNullException(nameof(key))).Id, realm);
|
||||
|
||||
/// <summary>
|
||||
/// 派生带拦截元数据的子上下文。子级同名 metadata 覆盖父级;丢弃子上下文即恢复。
|
||||
/// 该操作不改变 realm、provider 或 target,因此不会触发 fiber 重载。
|
||||
/// </summary>
|
||||
public ShrinkCtx Intercept(string key, IReadOnlyDictionary<string, object?> metadata)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentException("Key must not be null or empty.", nameof(key));
|
||||
if (metadata == null)
|
||||
throw new ArgumentNullException(nameof(metadata));
|
||||
|
||||
var metadataCopy = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
foreach (var pair in metadata)
|
||||
metadataCopy[pair.Key] = pair.Value;
|
||||
var overlay = new Dictionary<string, IReadOnlyDictionary<string, object?>>(StringComparer.Ordinal)
|
||||
{
|
||||
[key] = metadataCopy
|
||||
};
|
||||
return new ShrinkCtx(Runtime, this, OwnerFiber, null, overlay);
|
||||
}
|
||||
|
||||
public ShrinkCtx Intercept<T>(ShrinkKey<T> key, IReadOnlyDictionary<string, object?> metadata) =>
|
||||
Intercept((key ?? throw new ArgumentNullException(nameof(key))).Id, metadata);
|
||||
|
||||
internal ShrinkCtx CreateChildForFiber(ShrinkFiber fiber) => new(Runtime, this, fiber, null);
|
||||
|
||||
/// <summary>在派生子上下文中叠加隔离覆盖(同键后者优先),供加载器为条目配置隔离域。</summary>
|
||||
@@ -180,6 +226,38 @@ namespace ShrinkContext
|
||||
return new ShrinkCtx(Runtime, this, OwnerFiber, overlay);
|
||||
}
|
||||
|
||||
/// <summary>在当前纤程上下文上叠加 loader 条目配置的 intercept。</summary>
|
||||
internal ShrinkCtx WithIntercept(
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>> intercept) =>
|
||||
new(Runtime, this, OwnerFiber, null, interceptOverlay: intercept);
|
||||
|
||||
/// <summary>原地替换本层 intercept,供 loader 在不重载 fiber 的情况下更新访问策略。</summary>
|
||||
internal void ReplaceIntercept(
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? intercept)
|
||||
{
|
||||
if (intercept == null || intercept.Count == 0)
|
||||
{
|
||||
_interceptOverlay = null;
|
||||
return;
|
||||
}
|
||||
|
||||
var copy = new Dictionary<string, IReadOnlyDictionary<string, object?>>(StringComparer.Ordinal);
|
||||
foreach (var pair in intercept)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pair.Key))
|
||||
throw new ArgumentException("Intercept key must not be empty.", nameof(intercept));
|
||||
if (pair.Value == null)
|
||||
throw new ArgumentException($"Intercept metadata for '{pair.Key}' must not be null.",
|
||||
nameof(intercept));
|
||||
var metadataCopy = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
foreach (var metadataPair in pair.Value)
|
||||
metadataCopy[metadataPair.Key] = metadataPair.Value;
|
||||
copy[pair.Key] = metadataCopy;
|
||||
}
|
||||
|
||||
_interceptOverlay = copy;
|
||||
}
|
||||
|
||||
/// <summary>解析 key 在本上下文中归属的 realm:沿父链找最近的隔离覆盖,默认 realm 为 key 自身。</summary>
|
||||
internal string ResolveRealm(string key)
|
||||
{
|
||||
@@ -193,5 +271,30 @@ namespace ShrinkContext
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>按根到叶顺序合并拦截元数据;越接近访问方的上下文优先。</summary>
|
||||
internal IReadOnlyDictionary<string, object?> ResolveInterceptMetadata(string key)
|
||||
{
|
||||
var chain = new Stack<ShrinkCtx>();
|
||||
for (var current = this; current != null; current = current.Parent)
|
||||
chain.Push(current);
|
||||
|
||||
var merged = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
while (chain.Count > 0)
|
||||
{
|
||||
var current = chain.Pop();
|
||||
if (current._interceptOverlay == null ||
|
||||
!current._interceptOverlay.TryGetValue(key, out var metadata))
|
||||
continue;
|
||||
|
||||
foreach (var pair in metadata)
|
||||
merged[pair.Key] = pair.Value;
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
internal IReadOnlyCollection<string> GetLocalInterceptKeys() =>
|
||||
_interceptOverlay?.Keys ?? (IReadOnlyCollection<string>)Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace ShrinkContext
|
||||
Parent = parent;
|
||||
Config = config;
|
||||
InjectSet = new HashSet<string>(component.Inject ?? Array.Empty<string>(), StringComparer.Ordinal);
|
||||
ProvideSet = new HashSet<string>(component.Provide ?? Array.Empty<string>(), StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
public long Uid { get; }
|
||||
@@ -62,5 +63,6 @@ namespace ShrinkContext
|
||||
internal bool InTransition;
|
||||
internal bool Retired;
|
||||
internal HashSet<string> InjectSet { get; }
|
||||
internal HashSet<string> ProvideSet { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace ShrinkContext
|
||||
@@ -9,7 +10,8 @@ namespace ShrinkContext
|
||||
public sealed class ShrinkLoaderEntry
|
||||
{
|
||||
public ShrinkLoaderEntry(string id, string component, object? config = null, bool disabled = false,
|
||||
IReadOnlyDictionary<string, string>? isolate = null)
|
||||
IReadOnlyDictionary<string, string>? isolate = null,
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? intercept = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentException("Entry id must not be null or empty.", nameof(id));
|
||||
@@ -21,6 +23,7 @@ namespace ShrinkContext
|
||||
Config = config;
|
||||
Disabled = disabled;
|
||||
Isolate = isolate;
|
||||
Intercept = intercept;
|
||||
}
|
||||
|
||||
/// <summary>稳定协调键:条目增删与更新的 diff 依据。</summary>
|
||||
@@ -37,6 +40,9 @@ namespace ShrinkContext
|
||||
|
||||
/// <summary>条目级隔离域:键 → realm,叠加到该纤程自身的解析。</summary>
|
||||
public IReadOnlyDictionary<string, string>? Isolate { get; }
|
||||
|
||||
/// <summary>条目级访问元数据:依赖键 → metadata。更新时不改变 target,也不重载 fiber。</summary>
|
||||
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? Intercept { get; }
|
||||
}
|
||||
|
||||
/// <summary>组件目录:条目的组件名 → 组件工厂。装配期由宿主/CodeGen 注册,加载器不做全域反射扫描。</summary>
|
||||
@@ -69,23 +75,68 @@ namespace ShrinkContext
|
||||
component = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Contains(string name) => _factories.ContainsKey(name);
|
||||
}
|
||||
|
||||
/// <summary>加载器配置错误(重复条目 id、未知组件名等)。</summary>
|
||||
public sealed class ShrinkLoaderException : Exception
|
||||
public class ShrinkLoaderException : Exception
|
||||
{
|
||||
public ShrinkLoaderException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public enum ShrinkLoaderTransactionPhase
|
||||
{
|
||||
Idle = 0,
|
||||
Validating = 1,
|
||||
Removing = 2,
|
||||
Applying = 3,
|
||||
Restoring = 4,
|
||||
Completed = 5,
|
||||
Failed = 6,
|
||||
}
|
||||
|
||||
/// <summary>最近一次声明式协调的稳定诊断结果。</summary>
|
||||
public sealed class ShrinkLoaderTransactionDiagnostic
|
||||
{
|
||||
internal ShrinkLoaderTransactionDiagnostic(long generation)
|
||||
{
|
||||
Generation = generation;
|
||||
}
|
||||
|
||||
public long Generation { get; }
|
||||
public ShrinkLoaderTransactionPhase Phase { get; internal set; }
|
||||
public string? CurrentEntryId { get; internal set; }
|
||||
public string? ErrorType { get; internal set; }
|
||||
public string? ErrorMessage { get; internal set; }
|
||||
public bool RestoreAttempted { get; internal set; }
|
||||
public bool PreviousCompositionRestored { get; internal set; }
|
||||
public string? RestoreErrorType { get; internal set; }
|
||||
public string? RestoreErrorMessage { get; internal set; }
|
||||
}
|
||||
|
||||
public sealed class ShrinkLoaderRestoreException : ShrinkLoaderException
|
||||
{
|
||||
public ShrinkLoaderRestoreException(Exception applyError, Exception restoreError)
|
||||
: base("Loader apply failed and restoring the previous composition also failed.")
|
||||
{
|
||||
ApplyError = applyError;
|
||||
RestoreError = restoreError;
|
||||
}
|
||||
|
||||
public Exception ApplyError { get; }
|
||||
public Exception RestoreError { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 声明式组件加载器(论文 5.2 的原型子集):
|
||||
/// 编排者把期望组合表达为条目列表,加载器将其增量协调为纤程操作——
|
||||
/// 条目消失 → 退役;disabled → 卸载;组件/配置变化 → 重建;其余保持不动(幂等)。
|
||||
///
|
||||
/// 与完整实现的差异(后续阶段补齐):config 变化当前统一走重建而非组件自决 diff;
|
||||
/// isolate 变化走重建而非领域原地重分配;intercept 尚未支持。
|
||||
/// 与完整实现的差异:config 变化当前统一走重建而非组件自决 diff;
|
||||
/// isolate 变化仍走重建而非领域原地重分配。intercept metadata 可原地更新,不触发重载。
|
||||
/// </summary>
|
||||
public sealed class ShrinkContextLoader
|
||||
{
|
||||
@@ -95,12 +146,15 @@ namespace ShrinkContext
|
||||
public object? Config;
|
||||
public bool Disabled;
|
||||
public IReadOnlyDictionary<string, string>? Isolate;
|
||||
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? Intercept;
|
||||
public ShrinkFiber? Fiber;
|
||||
}
|
||||
|
||||
private readonly ShrinkContextRuntime _runtime;
|
||||
private readonly ShrinkComponentCatalog _catalog;
|
||||
private readonly Dictionary<string, ManagedEntry> _entries = new(StringComparer.Ordinal);
|
||||
private long _transactionGeneration;
|
||||
private bool _applying;
|
||||
|
||||
public ShrinkContextLoader(ShrinkContextRuntime runtime, ShrinkComponentCatalog catalog)
|
||||
{
|
||||
@@ -111,6 +165,8 @@ namespace ShrinkContext
|
||||
/// <summary>当前托管条目 id(含 disabled 但仍被管理的条目)。</summary>
|
||||
public IEnumerable<string> ManagedEntryIds => _entries.Keys;
|
||||
|
||||
public ShrinkLoaderTransactionDiagnostic? LastTransaction { get; private set; }
|
||||
|
||||
/// <summary>查询条目当前纤程(可能处于 Inactive——依赖缺失时被响应式停用但仍被管理)。</summary>
|
||||
public bool TryGetFiber(string entryId, out ShrinkFiber fiber)
|
||||
{
|
||||
@@ -132,15 +188,71 @@ namespace ShrinkContext
|
||||
{
|
||||
if (desired == null)
|
||||
throw new ArgumentNullException(nameof(desired));
|
||||
if (_applying)
|
||||
throw new InvalidOperationException("A loader composition transaction is already running.");
|
||||
|
||||
var previous = CaptureEntries();
|
||||
var transaction = new ShrinkLoaderTransactionDiagnostic(++_transactionGeneration);
|
||||
LastTransaction = transaction;
|
||||
_applying = true;
|
||||
try
|
||||
{
|
||||
await ApplyCoreAsync(desired, transaction, restoring: false);
|
||||
transaction.Phase = ShrinkLoaderTransactionPhase.Completed;
|
||||
transaction.CurrentEntryId = null;
|
||||
}
|
||||
catch (Exception applyError)
|
||||
{
|
||||
transaction.ErrorType = applyError.GetType().FullName;
|
||||
transaction.ErrorMessage = applyError.Message;
|
||||
transaction.RestoreAttempted = true;
|
||||
transaction.Phase = ShrinkLoaderTransactionPhase.Restoring;
|
||||
transaction.CurrentEntryId = null;
|
||||
|
||||
try
|
||||
{
|
||||
await ApplyCoreAsync(previous, transaction, restoring: true);
|
||||
transaction.PreviousCompositionRestored = true;
|
||||
}
|
||||
catch (Exception restoreError)
|
||||
{
|
||||
transaction.RestoreErrorType = restoreError.GetType().FullName;
|
||||
transaction.RestoreErrorMessage = restoreError.Message;
|
||||
transaction.Phase = ShrinkLoaderTransactionPhase.Failed;
|
||||
transaction.CurrentEntryId = null;
|
||||
throw new ShrinkLoaderRestoreException(applyError, restoreError);
|
||||
}
|
||||
|
||||
transaction.Phase = ShrinkLoaderTransactionPhase.Failed;
|
||||
transaction.CurrentEntryId = null;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_applying = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask ApplyCoreAsync(IReadOnlyList<ShrinkLoaderEntry> desired,
|
||||
ShrinkLoaderTransactionDiagnostic transaction, bool restoring)
|
||||
{
|
||||
transaction.Phase = restoring
|
||||
? ShrinkLoaderTransactionPhase.Restoring
|
||||
: ShrinkLoaderTransactionPhase.Validating;
|
||||
|
||||
var desiredIds = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var entry in desired)
|
||||
{
|
||||
if (!desiredIds.Add(entry.Id))
|
||||
throw new ShrinkLoaderException($"Duplicate loader entry id: '{entry.Id}'.");
|
||||
if (!entry.Disabled && !_catalog.Contains(entry.Component))
|
||||
throw new ShrinkLoaderException(
|
||||
$"Unknown component '{entry.Component}' requested by entry '{entry.Id}'.");
|
||||
}
|
||||
|
||||
// 1) 消失的条目退役(其依赖者由响应式通知自动停用,条目本身仍被管理)
|
||||
if (!restoring)
|
||||
transaction.Phase = ShrinkLoaderTransactionPhase.Removing;
|
||||
var removedIds = new List<string>();
|
||||
foreach (var id in _entries.Keys)
|
||||
{
|
||||
@@ -150,6 +262,7 @@ namespace ShrinkContext
|
||||
|
||||
foreach (var id in removedIds)
|
||||
{
|
||||
transaction.CurrentEntryId = id;
|
||||
var managed = _entries[id];
|
||||
if (managed.Fiber != null)
|
||||
await _runtime.RetireAsync(managed.Fiber);
|
||||
@@ -157,8 +270,11 @@ namespace ShrinkContext
|
||||
}
|
||||
|
||||
// 2) 按声明顺序逐条分派
|
||||
if (!restoring)
|
||||
transaction.Phase = ShrinkLoaderTransactionPhase.Applying;
|
||||
foreach (var entry in desired)
|
||||
{
|
||||
transaction.CurrentEntryId = entry.Id;
|
||||
_entries.TryGetValue(entry.Id, out var managed);
|
||||
|
||||
if (entry.Disabled)
|
||||
@@ -170,7 +286,8 @@ namespace ShrinkContext
|
||||
Component = entry.Component,
|
||||
Config = entry.Config,
|
||||
Disabled = true,
|
||||
Isolate = entry.Isolate
|
||||
Isolate = entry.Isolate,
|
||||
Intercept = entry.Intercept
|
||||
};
|
||||
}
|
||||
else
|
||||
@@ -179,6 +296,10 @@ namespace ShrinkContext
|
||||
await _runtime.RetireAsync(managed.Fiber);
|
||||
managed.Fiber = null;
|
||||
managed.Disabled = true;
|
||||
managed.Component = entry.Component;
|
||||
managed.Config = entry.Config;
|
||||
managed.Isolate = entry.Isolate;
|
||||
managed.Intercept = entry.Intercept;
|
||||
}
|
||||
|
||||
continue;
|
||||
@@ -189,28 +310,61 @@ namespace ShrinkContext
|
||||
Equals(managed.Config, entry.Config) &&
|
||||
IsolateEquals(managed.Isolate, entry.Isolate))
|
||||
{
|
||||
if (!InterceptEquals(managed.Intercept, entry.Intercept))
|
||||
{
|
||||
managed.Fiber.Ctx.ReplaceIntercept(entry.Intercept);
|
||||
managed.Intercept = entry.Intercept;
|
||||
}
|
||||
continue; // 幂等:无变化不打扰(被响应式停用的纤程保持管理,等待依赖回归)
|
||||
}
|
||||
|
||||
if (managed?.Fiber != null)
|
||||
{
|
||||
await _runtime.RetireAsync(managed.Fiber);
|
||||
managed.Fiber = null;
|
||||
}
|
||||
|
||||
if (!_catalog.TryCreate(entry.Component, out var component))
|
||||
throw new ShrinkLoaderException(
|
||||
$"Unknown component '{entry.Component}' requested by entry '{entry.Id}'.");
|
||||
throw new ShrinkLoaderException($"Component factory '{entry.Component}' returned no component.");
|
||||
|
||||
var fiber = _runtime.Use(component, entry.Config, _runtime.RootContext, entry.Isolate);
|
||||
var fiber = _runtime.Use(component, entry.Config, _runtime.RootContext, entry.Isolate, entry.Intercept);
|
||||
if (fiber.LastError != null)
|
||||
{
|
||||
var failure = fiber.LastError;
|
||||
await _runtime.RetireAsync(fiber);
|
||||
throw new ShrinkLoaderException(
|
||||
$"Component '{entry.Component}' for entry '{entry.Id}' failed during apply: {failure.Message}");
|
||||
}
|
||||
_entries[entry.Id] = new ManagedEntry
|
||||
{
|
||||
Component = entry.Component,
|
||||
Config = entry.Config,
|
||||
Disabled = false,
|
||||
Isolate = entry.Isolate,
|
||||
Intercept = entry.Intercept,
|
||||
Fiber = fiber
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<ShrinkLoaderEntry> CaptureEntries()
|
||||
{
|
||||
var entries = new List<ShrinkLoaderEntry>(_entries.Count);
|
||||
foreach (var pair in _entries.OrderBy(item => item.Key, StringComparer.Ordinal))
|
||||
{
|
||||
var managed = pair.Value;
|
||||
entries.Add(new ShrinkLoaderEntry(
|
||||
pair.Key,
|
||||
managed.Component,
|
||||
managed.Config,
|
||||
managed.Disabled,
|
||||
managed.Isolate,
|
||||
managed.Intercept));
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
private static bool IsolateEquals(IReadOnlyDictionary<string, string>? a, IReadOnlyDictionary<string, string>? b)
|
||||
{
|
||||
if (a == null && b == null)
|
||||
@@ -228,5 +382,31 @@ namespace ShrinkContext
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool InterceptEquals(
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? a,
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? b)
|
||||
{
|
||||
if (a == null && b == null)
|
||||
return true;
|
||||
if (a == null || b == null || a.Count != b.Count)
|
||||
return false;
|
||||
|
||||
foreach (var keyPair in a)
|
||||
{
|
||||
if (!b.TryGetValue(keyPair.Key, out var otherMetadata) ||
|
||||
keyPair.Value.Count != otherMetadata.Count)
|
||||
return false;
|
||||
|
||||
foreach (var metadataPair in keyPair.Value)
|
||||
{
|
||||
if (!otherMetadata.TryGetValue(metadataPair.Key, out var otherValue) ||
|
||||
!Equals(metadataPair.Value, otherValue))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace ShrinkContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认运行时入口 + Domain Reload 安全重置。
|
||||
/// 原型阶段不自动启动任何组件;编排方式(配置树加载器)属于后续阶段。
|
||||
/// 不自动启动任何组件;上层 composition root 通过 ShrinkContextLoader 提交期望组合。
|
||||
/// </summary>
|
||||
public static class ShrinkContextDefaults
|
||||
{
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ShrinkContext
|
||||
{
|
||||
public enum ShrinkFiberDiagnosticStatus
|
||||
{
|
||||
Inactive = 0,
|
||||
Waiting = 1,
|
||||
Loading = 2,
|
||||
Active = 3,
|
||||
Unloading = 4,
|
||||
Failed = 5,
|
||||
Retired = 6,
|
||||
}
|
||||
|
||||
/// <summary>单个依赖键在快照时刻的解析、提交与潜在提供者。</summary>
|
||||
public sealed class ShrinkDependencyDiagnostic
|
||||
{
|
||||
public ShrinkDependencyDiagnostic(string key, string realm, long? currentProviderUid,
|
||||
long? committedProviderUid, IReadOnlyList<long> potentialProviderUids)
|
||||
{
|
||||
Key = key;
|
||||
Realm = realm;
|
||||
CurrentProviderUid = currentProviderUid;
|
||||
CommittedProviderUid = committedProviderUid;
|
||||
PotentialProviderUids = potentialProviderUids;
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string Realm { get; }
|
||||
public long? CurrentProviderUid { get; }
|
||||
public long? CommittedProviderUid { get; }
|
||||
public IReadOnlyList<long> PotentialProviderUids { get; }
|
||||
public bool IsSatisfied => CurrentProviderUid.HasValue;
|
||||
}
|
||||
|
||||
/// <summary>一个 fiber 的稳定只读诊断投影,不暴露可变运行时对象。</summary>
|
||||
public sealed class ShrinkFiberDiagnostic
|
||||
{
|
||||
public ShrinkFiberDiagnostic(long uid, string name, ShrinkFiberDiagnosticStatus status,
|
||||
ShrinkFiberState lifecycleState, bool retired, bool inTransition,
|
||||
IReadOnlyList<string> inject, IReadOnlyList<string> provide,
|
||||
IReadOnlyList<long> targetProviderUids,
|
||||
IReadOnlyList<ShrinkDependencyDiagnostic> dependencies,
|
||||
IReadOnlyList<string> interceptKeys,
|
||||
string? errorType, string? errorMessage)
|
||||
{
|
||||
Uid = uid;
|
||||
Name = name;
|
||||
Status = status;
|
||||
LifecycleState = lifecycleState;
|
||||
Retired = retired;
|
||||
InTransition = inTransition;
|
||||
Inject = inject;
|
||||
Provide = provide;
|
||||
TargetProviderUids = targetProviderUids;
|
||||
Dependencies = dependencies;
|
||||
InterceptKeys = interceptKeys;
|
||||
ErrorType = errorType;
|
||||
ErrorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public long Uid { get; }
|
||||
public string Name { get; }
|
||||
public ShrinkFiberDiagnosticStatus Status { get; }
|
||||
public ShrinkFiberState LifecycleState { get; }
|
||||
public bool Retired { get; }
|
||||
public bool InTransition { get; }
|
||||
public IReadOnlyList<string> Inject { get; }
|
||||
public IReadOnlyList<string> Provide { get; }
|
||||
public IReadOnlyList<long> TargetProviderUids { get; }
|
||||
public IReadOnlyList<ShrinkDependencyDiagnostic> Dependencies { get; }
|
||||
public IReadOnlyList<string> InterceptKeys { get; }
|
||||
public string? ErrorType { get; }
|
||||
public string? ErrorMessage { get; }
|
||||
}
|
||||
|
||||
public sealed class ShrinkNotificationDiagnostic
|
||||
{
|
||||
public ShrinkNotificationDiagnostic(long dispatchCount, long candidateVisitCount,
|
||||
int lastCandidateCount, int indexedKeyCount)
|
||||
{
|
||||
DispatchCount = dispatchCount;
|
||||
CandidateVisitCount = candidateVisitCount;
|
||||
LastCandidateCount = lastCandidateCount;
|
||||
IndexedKeyCount = indexedKeyCount;
|
||||
}
|
||||
|
||||
public long DispatchCount { get; }
|
||||
public long CandidateVisitCount { get; }
|
||||
public int LastCandidateCount { get; }
|
||||
public int IndexedKeyCount { get; }
|
||||
}
|
||||
|
||||
/// <summary>运行时整体快照:fiber、绑定与 notify 索引状态。</summary>
|
||||
public sealed class ShrinkContextRuntimeDiagnostic
|
||||
{
|
||||
public ShrinkContextRuntimeDiagnostic(IReadOnlyList<ShrinkFiberDiagnostic> fibers,
|
||||
int bindingCount, ShrinkNotificationDiagnostic notifications)
|
||||
{
|
||||
Fibers = fibers;
|
||||
BindingCount = bindingCount;
|
||||
Notifications = notifications;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ShrinkFiberDiagnostic> Fibers { get; }
|
||||
public int BindingCount { get; }
|
||||
public ShrinkNotificationDiagnostic Notifications { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ee8044af3885c84a9b86b101af8ae28
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -13,14 +13,20 @@ namespace ShrinkContext
|
||||
/// 约定:
|
||||
/// - 仅在主线程使用;
|
||||
/// - 异步转换在同步可完成路径上内联跑完(无需 PlayerLoop 泵即可确定性测试);
|
||||
/// - 目标(Target)以提供者 uid 集合表示,uid 永不复用,因此“同值不同提供者”的替换也会触发重载。
|
||||
/// - 目标(Target)按 inject 声明顺序记录 provider uid 视图,uid 永不复用,
|
||||
/// 因此“同值不同提供者”的替换及多键 provider 映射变化都能触发重载。
|
||||
/// </summary>
|
||||
public sealed class ShrinkContextRuntime
|
||||
{
|
||||
private readonly ShrinkCtx _rootCtx;
|
||||
private readonly List<ShrinkFiber> _fibers = new();
|
||||
private readonly Dictionary<string, ShrinkBinding> _store = new();
|
||||
private readonly Dictionary<string, HashSet<ShrinkFiber>> _injectIndex =
|
||||
new(StringComparer.Ordinal);
|
||||
private long _uidCounter;
|
||||
private long _notificationDispatchCount;
|
||||
private long _notificationCandidateVisitCount;
|
||||
private int _lastNotificationCandidateCount;
|
||||
|
||||
public ShrinkContextRuntime()
|
||||
{
|
||||
@@ -39,7 +45,8 @@ namespace ShrinkContext
|
||||
/// isolate 为该纤程自身的键解析叠加隔离域(不影响父上下文)。
|
||||
/// </summary>
|
||||
public ShrinkFiber Use(IShrinkComponent component, object? config = null, ShrinkCtx? parentCtx = null,
|
||||
IReadOnlyDictionary<string, string>? isolate = null)
|
||||
IReadOnlyDictionary<string, string>? isolate = null,
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? intercept = null)
|
||||
{
|
||||
if (component == null)
|
||||
throw new ArgumentNullException(nameof(component));
|
||||
@@ -49,8 +56,11 @@ namespace ShrinkContext
|
||||
var fiberCtx = parent.CreateChildForFiber(fiber);
|
||||
if (isolate != null && isolate.Count > 0)
|
||||
fiberCtx = fiberCtx.WithIsolate(isolate);
|
||||
if (intercept != null && intercept.Count > 0)
|
||||
fiberCtx = fiberCtx.WithIntercept(intercept);
|
||||
fiber.Ctx = fiberCtx;
|
||||
_fibers.Add(fiber);
|
||||
IndexFiber(fiber);
|
||||
|
||||
var instantiationHandle = new ShrinkEffectHandle();
|
||||
instantiationHandle.AttachInverse(() => RetireCoreAsync(fiber));
|
||||
@@ -68,6 +78,7 @@ namespace ShrinkContext
|
||||
{
|
||||
if (fiber.Retired)
|
||||
return;
|
||||
UnindexFiber(fiber);
|
||||
if (fiber.State == ShrinkFiberState.Inactive && !fiber.InTransition)
|
||||
{
|
||||
fiber.Retired = true;
|
||||
@@ -97,7 +108,7 @@ namespace ShrinkContext
|
||||
// ---------------- 生命周期(论文算法 5) ----------------
|
||||
|
||||
/// <summary>
|
||||
/// 重算目标:任一依赖键不可解析 → ⊥;否则为全部提供者 uid 的有序集合。
|
||||
/// 重算目标:任一依赖键不可解析 → ⊥;否则为按 inject 键顺序记录的 provider uid 视图。
|
||||
/// 目标变化即启动(或在惯性边界链式触发)reload / unload 转换。
|
||||
/// </summary>
|
||||
internal bool Refresh(ShrinkFiber fiber)
|
||||
@@ -222,19 +233,38 @@ namespace ShrinkContext
|
||||
internal IReadOnlyList<ShrinkFiber> Notify(ShrinkCtx sourceCtx, IReadOnlyCollection<string> keys)
|
||||
{
|
||||
var affected = new List<ShrinkFiber>();
|
||||
foreach (var fiber in _fibers.ToArray())
|
||||
if (keys.Count == 0)
|
||||
return affected;
|
||||
|
||||
var candidates = new HashSet<ShrinkFiber>();
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (_injectIndex.TryGetValue(key, out var indexed))
|
||||
candidates.UnionWith(indexed);
|
||||
}
|
||||
|
||||
_notificationDispatchCount++;
|
||||
_lastNotificationCandidateCount = candidates.Count;
|
||||
_notificationCandidateVisitCount += candidates.Count;
|
||||
|
||||
foreach (var fiber in candidates.OrderBy(candidate => candidate.Uid))
|
||||
{
|
||||
if (fiber.Retired)
|
||||
continue;
|
||||
|
||||
var realmMatches = false;
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (!fiber.InjectSet.Contains(key))
|
||||
continue;
|
||||
if (fiber.Ctx.ResolveRealm(key) != sourceCtx.ResolveRealm(key))
|
||||
continue;
|
||||
|
||||
if (Refresh(fiber))
|
||||
affected.Add(fiber);
|
||||
realmMatches = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (realmMatches && Refresh(fiber))
|
||||
affected.Add(fiber);
|
||||
}
|
||||
|
||||
return affected;
|
||||
@@ -242,12 +272,18 @@ namespace ShrinkContext
|
||||
|
||||
// ---------------- 余效应存储(论文算法 2) ----------------
|
||||
|
||||
internal ShrinkEffectHandle SetBinding(ShrinkCtx ctx, string key, object? value)
|
||||
internal ShrinkEffectHandle SetBinding(ShrinkCtx ctx, string key, object? value,
|
||||
IShrinkCoeffectAccessPolicy? accessPolicy)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentException("Key must not be null or empty.", nameof(key));
|
||||
|
||||
var realm = ctx.ResolveRealm(key);
|
||||
var setter = ctx.OwnerFiber;
|
||||
if (setter != null && !setter.ProvideSet.Contains(key))
|
||||
throw new ShrinkUndeclaredSupplyException(key, setter.Name);
|
||||
|
||||
InstallBinding(realm, key, value, setter, ctx);
|
||||
InstallBinding(realm, key, value, setter, ctx, accessPolicy);
|
||||
|
||||
var handle = new ShrinkEffectHandle();
|
||||
handle.AttachInverse(() =>
|
||||
@@ -259,12 +295,13 @@ namespace ShrinkContext
|
||||
return handle;
|
||||
}
|
||||
|
||||
private void InstallBinding(string realm, string key, object? value, ShrinkFiber? setter, ShrinkCtx ctx)
|
||||
private void InstallBinding(string realm, string key, object? value, ShrinkFiber? setter, ShrinkCtx ctx,
|
||||
IShrinkCoeffectAccessPolicy? accessPolicy)
|
||||
{
|
||||
if (_store.TryGetValue(realm, out var existing) && !IsTakeoverAllowed(existing, setter))
|
||||
throw new ShrinkSupplyConflictException(key, existing.Provider, setter);
|
||||
|
||||
_store[realm] = new ShrinkBinding(value, setter, key);
|
||||
_store[realm] = new ShrinkBinding(value, setter, key, accessPolicy);
|
||||
Notify(ctx, new[] { key });
|
||||
}
|
||||
|
||||
@@ -310,6 +347,33 @@ namespace ShrinkContext
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetRaw<T>(ShrinkCtx ctx, ShrinkKey<T> key, out T value) => TryGetRaw(
|
||||
ctx, (key ?? throw new ArgumentNullException(nameof(key))).Id, out value);
|
||||
|
||||
/// <summary>执行 provider 绑定的访问策略;无策略时直接返回原始值。</summary>
|
||||
internal T ResolveAccess<T>(ShrinkCtx ctx, string key, ShrinkBinding binding)
|
||||
{
|
||||
object? value = binding.Value;
|
||||
if (binding.AccessPolicy != null)
|
||||
{
|
||||
var access = new ShrinkCoeffectAccessContext(
|
||||
key,
|
||||
typeof(T),
|
||||
ctx.OwnerFiber,
|
||||
ctx.ResolveInterceptMetadata(key));
|
||||
value = binding.AccessPolicy.Resolve(access, value);
|
||||
}
|
||||
|
||||
if (value is T typed)
|
||||
return typed;
|
||||
if (value == null && (!typeof(T).IsValueType || Nullable.GetUnderlyingType(typeof(T)) != null))
|
||||
return default!;
|
||||
|
||||
throw new InvalidCastException(
|
||||
$"Coeffect '{key}' resolved value of type '{value?.GetType().FullName ?? "<null>"}' " +
|
||||
$"but consumer requested '{typeof(T).FullName}'.");
|
||||
}
|
||||
|
||||
// ---------------- 目标计算 ----------------
|
||||
|
||||
private long[]? ComputeTarget(ShrinkFiber fiber)
|
||||
@@ -323,7 +387,9 @@ namespace ShrinkContext
|
||||
uids.Add(binding.Provider?.Uid ?? 0);
|
||||
}
|
||||
|
||||
return uids.Distinct().OrderBy(uid => uid).ToArray();
|
||||
// target 是 key -> provider uid 的视图,不是 uid 集合。保留 inject 声明顺序与重复 uid,
|
||||
// 才能观察两个键在相同提供者集合之间互换的变化。
|
||||
return uids.ToArray();
|
||||
}
|
||||
|
||||
private Dictionary<string, ShrinkBinding> ResolveView(ShrinkFiber fiber)
|
||||
@@ -361,5 +427,102 @@ namespace ShrinkContext
|
||||
return;
|
||||
await RetireAsync(fiber);
|
||||
}
|
||||
|
||||
private void IndexFiber(ShrinkFiber fiber)
|
||||
{
|
||||
foreach (var key in fiber.InjectSet)
|
||||
{
|
||||
if (!_injectIndex.TryGetValue(key, out var fibers))
|
||||
{
|
||||
fibers = new HashSet<ShrinkFiber>();
|
||||
_injectIndex.Add(key, fibers);
|
||||
}
|
||||
|
||||
fibers.Add(fiber);
|
||||
}
|
||||
}
|
||||
|
||||
private void UnindexFiber(ShrinkFiber fiber)
|
||||
{
|
||||
foreach (var key in fiber.InjectSet)
|
||||
{
|
||||
if (!_injectIndex.TryGetValue(key, out var fibers))
|
||||
continue;
|
||||
fibers.Remove(fiber);
|
||||
if (fibers.Count == 0)
|
||||
_injectIndex.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>捕获不含可变运行时引用的诊断快照。</summary>
|
||||
public ShrinkContextRuntimeDiagnostic CaptureDiagnostic()
|
||||
{
|
||||
var diagnostics = new List<ShrinkFiberDiagnostic>(_fibers.Count);
|
||||
foreach (var fiber in _fibers.OrderBy(item => item.Uid))
|
||||
{
|
||||
var dependencies = new List<ShrinkDependencyDiagnostic>();
|
||||
foreach (var key in fiber.Component.Inject ?? Array.Empty<string>())
|
||||
{
|
||||
var realm = fiber.Ctx.ResolveRealm(key);
|
||||
var current = ResolveBinding(fiber.Ctx, key);
|
||||
ShrinkBinding? committed = null;
|
||||
fiber.Committed?.TryGetValue(key, out committed);
|
||||
var potential = _fibers
|
||||
.Where(candidate => !candidate.Retired && candidate.ProvideSet.Contains(key) &&
|
||||
candidate.Ctx.ResolveRealm(key) == realm)
|
||||
.Select(candidate => candidate.Uid)
|
||||
.OrderBy(uid => uid)
|
||||
.ToArray();
|
||||
|
||||
dependencies.Add(new ShrinkDependencyDiagnostic(
|
||||
key,
|
||||
realm,
|
||||
current?.Provider?.Uid,
|
||||
committed?.Provider?.Uid,
|
||||
potential));
|
||||
}
|
||||
|
||||
diagnostics.Add(new ShrinkFiberDiagnostic(
|
||||
fiber.Uid,
|
||||
fiber.Name,
|
||||
GetDiagnosticStatus(fiber),
|
||||
fiber.State,
|
||||
fiber.Retired,
|
||||
fiber.InTransition,
|
||||
(fiber.Component.Inject ?? Array.Empty<string>()).ToArray(),
|
||||
(fiber.Component.Provide ?? Array.Empty<string>()).ToArray(),
|
||||
fiber.Target?.ToArray() ?? Array.Empty<long>(),
|
||||
dependencies,
|
||||
fiber.Ctx.GetLocalInterceptKeys().OrderBy(key => key, StringComparer.Ordinal).ToArray(),
|
||||
fiber.LastError?.GetType().FullName,
|
||||
fiber.LastError?.Message));
|
||||
}
|
||||
|
||||
return new ShrinkContextRuntimeDiagnostic(
|
||||
diagnostics,
|
||||
_store.Count,
|
||||
new ShrinkNotificationDiagnostic(
|
||||
_notificationDispatchCount,
|
||||
_notificationCandidateVisitCount,
|
||||
_lastNotificationCandidateCount,
|
||||
_injectIndex.Count));
|
||||
}
|
||||
|
||||
private static ShrinkFiberDiagnosticStatus GetDiagnosticStatus(ShrinkFiber fiber)
|
||||
{
|
||||
if (fiber.Retired)
|
||||
return ShrinkFiberDiagnosticStatus.Retired;
|
||||
if (fiber.LastError != null)
|
||||
return ShrinkFiberDiagnosticStatus.Failed;
|
||||
|
||||
return fiber.State switch
|
||||
{
|
||||
ShrinkFiberState.Loading => ShrinkFiberDiagnosticStatus.Loading,
|
||||
ShrinkFiberState.Active => ShrinkFiberDiagnosticStatus.Active,
|
||||
ShrinkFiberState.Unloading => ShrinkFiberDiagnosticStatus.Unloading,
|
||||
ShrinkFiberState.Inactive when fiber.InjectSet.Count > 0 => ShrinkFiberDiagnosticStatus.Waiting,
|
||||
_ => ShrinkFiberDiagnosticStatus.Inactive,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +95,40 @@ namespace ShrinkContext.Tests
|
||||
Assert.AreEqual(1, rootValueFinal);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypedKey_PreservesVersionedIdentityAcrossMediatedAccess()
|
||||
{
|
||||
var key = new ShrinkKey<string>("test.package", "typed-service", 2);
|
||||
Assert.AreEqual("test.package/typed-service@v2", key.Id);
|
||||
|
||||
_runtime.Use(new PolicyProviderComponent(key.Id), "typed-value");
|
||||
var consumer = _runtime.Use(new PolicyConsumerComponent(key.Id));
|
||||
|
||||
Assert.AreEqual(ShrinkFiberState.Active, consumer.State);
|
||||
Assert.AreEqual("typed-value", consumer.Ctx.Get(key));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Intercept_MergesRootToLeafAndChildMetadataWins()
|
||||
{
|
||||
const string key = "policy.service";
|
||||
_runtime.Use(new PolicyProviderComponent(key), "value");
|
||||
var consumer = _runtime.Use(new PolicyConsumerComponent(key));
|
||||
Assert.AreEqual(ShrinkFiberState.Active, consumer.State);
|
||||
|
||||
var parent = consumer.Ctx.Intercept(key, new Dictionary<string, object?>
|
||||
{
|
||||
["prefix"] = "parent-",
|
||||
["suffix"] = "-parent"
|
||||
});
|
||||
var child = parent.Intercept(key, new Dictionary<string, object?>
|
||||
{
|
||||
["suffix"] = "-child"
|
||||
});
|
||||
|
||||
Assert.AreEqual("parent-value-child", child.Get<string>(key));
|
||||
}
|
||||
|
||||
/// <summary>声明一个键却访问另一个未声明键的组件(验证能力介导纪律)。</summary>
|
||||
private sealed class GreedyConsumerComponent : IShrinkComponent
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using ShrinkContext;
|
||||
@@ -155,6 +156,57 @@ namespace ShrinkContext.Tests
|
||||
Assert.AreEqual(ShrinkFiberState.Active, firstFiber.State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UndeclaredSupply_FailsFiberAndLeavesNoBinding()
|
||||
{
|
||||
var fiber = _runtime.Use(new UndeclaredProviderComponent());
|
||||
|
||||
Assert.AreEqual(ShrinkFiberState.Inactive, fiber.State);
|
||||
Assert.IsInstanceOf<ShrinkUndeclaredSupplyException>(fiber.LastError);
|
||||
Assert.IsFalse(_runtime.TryGetRaw<int>(_runtime.RootContext, "not-declared", out _));
|
||||
|
||||
var snapshot = _runtime.CaptureDiagnostic();
|
||||
var diagnostic = snapshot.Fibers.Single(item => item.Uid == fiber.Uid);
|
||||
Assert.AreEqual(ShrinkFiberDiagnosticStatus.Failed, diagnostic.Status);
|
||||
StringAssert.Contains("not-declared", diagnostic.ErrorMessage);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DiagnosticSnapshot_ExplainsWaitingDependencyAndPotentialProvider()
|
||||
{
|
||||
var waiting = _runtime.Use(new ConsumerComponent("waiting", "svc"));
|
||||
var blockedProvider = _runtime.Use(new ProviderComponent("blocked-provider", "svc")
|
||||
{
|
||||
Inject = new[] { "missing" }
|
||||
});
|
||||
|
||||
var snapshot = _runtime.CaptureDiagnostic();
|
||||
var waitingDiagnostic = snapshot.Fibers.Single(item => item.Uid == waiting.Uid);
|
||||
Assert.AreEqual(ShrinkFiberDiagnosticStatus.Waiting, waitingDiagnostic.Status);
|
||||
Assert.AreEqual(1, waitingDiagnostic.Dependencies.Count);
|
||||
Assert.IsFalse(waitingDiagnostic.Dependencies[0].IsSatisfied);
|
||||
CollectionAssert.Contains(waitingDiagnostic.Dependencies[0].PotentialProviderUids,
|
||||
blockedProvider.Uid,
|
||||
"等待链应显示声明了该键、但自身依赖未满足的潜在提供者");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NotifyIndex_VisitsOnlyConsumersOfChangedKey()
|
||||
{
|
||||
var target = new ConsumerComponent("target", "target-key");
|
||||
_runtime.Use(target);
|
||||
for (var i = 0; i < 40; i++)
|
||||
_runtime.Use(new ConsumerComponent("unrelated-" + i, "other-" + i));
|
||||
|
||||
_runtime.Use(new ProviderComponent("provider", "target-key", "value"));
|
||||
|
||||
var notifications = _runtime.CaptureDiagnostic().Notifications;
|
||||
Assert.AreEqual(1, notifications.LastCandidateCount,
|
||||
"notify 应从 key 倒排索引取得候选,不扫描 40 个无关 fiber");
|
||||
Assert.AreEqual(41, notifications.IndexedKeyCount);
|
||||
Assert.AreEqual("value", target.LastSeen);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularDependencies_BothStayInactiveWithoutError()
|
||||
{
|
||||
|
||||
@@ -24,6 +24,9 @@ namespace ShrinkContext.Tests
|
||||
_catalog.Register("provider", () => new ConfigProviderComponent("provider", "svc"));
|
||||
_catalog.Register("provider.alt", () => new ConfigProviderComponent("provider.alt", "svc"));
|
||||
_catalog.Register("consumer", () => new ConsumerComponent("consumer", "svc"));
|
||||
_catalog.Register("failing", () => new FailingComponent("failing", "svc"));
|
||||
_catalog.Register("policy.provider", () => new PolicyProviderComponent("policy.service"));
|
||||
_catalog.Register("policy.consumer", () => new PolicyConsumerComponent("policy.service"));
|
||||
_loader = new ShrinkContextLoader(_runtime, _catalog);
|
||||
}
|
||||
|
||||
@@ -223,5 +226,87 @@ namespace ShrinkContext.Tests
|
||||
Assert.AreEqual("from-r2", ((ConsumerComponent)consumerFiber.Component).LastSeen,
|
||||
"消费者按自身隔离域解析到 r2 的绑定");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsolatedProviderReplacement_DoesNotReloadOtherRealmConsumer()
|
||||
{
|
||||
var isolateR1 = new Dictionary<string, string> { ["svc"] = "r1" };
|
||||
var isolateR2 = new Dictionary<string, string> { ["svc"] = "r2" };
|
||||
TestAwait.Run(_loader.ApplyAsync(new List<ShrinkLoaderEntry>
|
||||
{
|
||||
new("p1", "provider", "r1-v1", isolate: isolateR1),
|
||||
new("p2", "provider", "r2-v1", isolate: isolateR2),
|
||||
new("c2", "consumer", isolate: isolateR2),
|
||||
}));
|
||||
Assert.IsTrue(_loader.TryGetFiber("c2", out var consumerFiber));
|
||||
var consumer = (ConsumerComponent)consumerFiber.Component;
|
||||
Assert.AreEqual(1, consumer.LoadCount);
|
||||
|
||||
TestAwait.Run(_loader.ApplyAsync(new List<ShrinkLoaderEntry>
|
||||
{
|
||||
new("p1", "provider", "r1-v2", isolate: isolateR1),
|
||||
new("p2", "provider", "r2-v1", isolate: isolateR2),
|
||||
new("c2", "consumer", isolate: isolateR2),
|
||||
}));
|
||||
|
||||
Assert.AreEqual(1, consumer.LoadCount,
|
||||
"r1 提供者替换不得触发只解析 r2 的消费者重载");
|
||||
Assert.AreEqual("r2-v1", consumer.LastSeen);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InterceptUpdate_ChangesAccessWithoutReloadingFiber()
|
||||
{
|
||||
var suffixA = new Dictionary<string, IReadOnlyDictionary<string, object?>>
|
||||
{
|
||||
["policy.service"] = new Dictionary<string, object?> { ["suffix"] = "-a" }
|
||||
};
|
||||
var suffixB = new Dictionary<string, IReadOnlyDictionary<string, object?>>
|
||||
{
|
||||
["policy.service"] = new Dictionary<string, object?> { ["suffix"] = "-b" }
|
||||
};
|
||||
|
||||
TestAwait.Run(_loader.ApplyAsync(new List<ShrinkLoaderEntry>
|
||||
{
|
||||
new("policy-provider", "policy.provider", "base"),
|
||||
new("policy-consumer", "policy.consumer", intercept: suffixA),
|
||||
}));
|
||||
Assert.IsTrue(_loader.TryGetFiber("policy-consumer", out var firstFiber));
|
||||
var consumer = (PolicyConsumerComponent)firstFiber.Component;
|
||||
Assert.AreEqual("base-a", consumer.Read());
|
||||
Assert.AreEqual(1, consumer.ApplyCount);
|
||||
|
||||
TestAwait.Run(_loader.ApplyAsync(new List<ShrinkLoaderEntry>
|
||||
{
|
||||
new("policy-provider", "policy.provider", "base"),
|
||||
new("policy-consumer", "policy.consumer", intercept: suffixB),
|
||||
}));
|
||||
|
||||
Assert.IsTrue(_loader.TryGetFiber("policy-consumer", out var sameFiber));
|
||||
Assert.AreSame(firstFiber, sameFiber);
|
||||
Assert.AreEqual(1, consumer.ApplyCount, "intercept 元数据变化不应触发 reload");
|
||||
Assert.AreEqual("base-b", consumer.Read(), "访问时应读取更新后的 metadata");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FailedReplacement_RestoresPreviousLoaderCompositionAndReportsTransaction()
|
||||
{
|
||||
TestAwait.Run(_loader.ApplyAsync(new List<ShrinkLoaderEntry>
|
||||
{
|
||||
new("provider-entry", "provider", "stable")
|
||||
}));
|
||||
|
||||
Assert.Throws<ShrinkLoaderException>(() => TestAwait.Run(_loader.ApplyAsync(
|
||||
new List<ShrinkLoaderEntry> { new("provider-entry", "failing") })));
|
||||
|
||||
Assert.IsTrue(_loader.TryGetFiber("provider-entry", out var restored));
|
||||
Assert.AreEqual(ShrinkFiberState.Active, restored.State);
|
||||
Assert.AreEqual("stable", restored.Config);
|
||||
Assert.IsNotNull(_loader.LastTransaction);
|
||||
Assert.AreEqual(ShrinkLoaderTransactionPhase.Failed, _loader.LastTransaction!.Phase);
|
||||
Assert.IsTrue(_loader.LastTransaction.RestoreAttempted);
|
||||
Assert.IsTrue(_loader.LastTransaction.PreviousCompositionRestored);
|
||||
StringAssert.Contains("failed during apply", _loader.LastTransaction.ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,6 +250,81 @@ namespace ShrinkContext.Tests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>故意写入未声明键,验证 Provide 运行时契约。</summary>
|
||||
public sealed class UndeclaredProviderComponent : IShrinkComponent
|
||||
{
|
||||
public string Name => "undeclared-provider";
|
||||
public IReadOnlyList<string> Inject => Array.Empty<string>();
|
||||
public IReadOnlyList<string> Provide => Array.Empty<string>();
|
||||
|
||||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
ctx.Set("not-declared", 1);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MetadataSuffixPolicy : IShrinkCoeffectAccessPolicy
|
||||
{
|
||||
public object? Resolve(ShrinkCoeffectAccessContext context, object? value)
|
||||
{
|
||||
var text = value?.ToString() ?? string.Empty;
|
||||
if (context.TryGetMetadata<string>("prefix", out var prefix))
|
||||
text = prefix + text;
|
||||
return context.TryGetMetadata<string>("suffix", out var suffix) ? text + suffix : text;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class PolicyProviderComponent : IShrinkComponent
|
||||
{
|
||||
private readonly string[] _provide;
|
||||
|
||||
public PolicyProviderComponent(string key)
|
||||
{
|
||||
Key = key;
|
||||
_provide = new[] { key };
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string Name => "policy-provider";
|
||||
public IReadOnlyList<string> Inject => Array.Empty<string>();
|
||||
public IReadOnlyList<string> Provide => _provide;
|
||||
|
||||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
ctx.Set(Key, config?.ToString() ?? "value", new MetadataSuffixPolicy());
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class PolicyConsumerComponent : IShrinkComponent
|
||||
{
|
||||
private readonly string[] _inject;
|
||||
private ShrinkCtx? _ctx;
|
||||
|
||||
public PolicyConsumerComponent(string key)
|
||||
{
|
||||
Key = key;
|
||||
_inject = new[] { key };
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string Name => "policy-consumer";
|
||||
public int ApplyCount { get; private set; }
|
||||
public IReadOnlyList<string> Inject => _inject;
|
||||
public IReadOnlyList<string> Provide => Array.Empty<string>();
|
||||
|
||||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
_ctx = ctx;
|
||||
ApplyCount++;
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public string Read() => (_ctx ?? throw new InvalidOperationException("Consumer is not active."))
|
||||
.Get<string>(Key);
|
||||
}
|
||||
|
||||
/// <summary>测试辅助:同步等待 UniTask(测试保证全部延续同步完成,无死锁风险)。</summary>
|
||||
public static class TestAwait
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user