#nullable enable
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace ShrinkContext
{
public enum ShrinkFiberState
{
Inactive = 0,
Loading = 1,
Active = 2,
Unloading = 3,
}
///
/// 纤程(论文定义 44):组件的一次运行时实例化。
/// 生命周期由 Target(期望提供者集合,null 即 ⊥)与惯性状态机驱动:
/// 一旦进入转换即运行至完成,之后才响应新的目标变化(算法 5)。
///
public sealed class ShrinkFiber
{
internal ShrinkFiber(long uid, IShrinkComponent component, ShrinkFiber? parent, object? config)
{
Uid = uid;
Component = component;
Parent = parent;
Config = config;
InjectSet = new HashSet(component.Inject ?? Array.Empty(), StringComparer.Ordinal);
ProvideSet = new HashSet(component.Provide ?? Array.Empty(), StringComparer.Ordinal);
}
public long Uid { get; }
public string Name => Component.Name;
public IShrinkComponent Component { get; }
/// 实例化该纤程时的父纤程(根级为 null);父纤程卸载会级联到本纤程。
public ShrinkFiber? Parent { get; }
/// 本纤程运行所在的派生子上下文。
public ShrinkCtx Ctx { get; internal set; } = null!;
public object? Config { get; }
public ShrinkFiberState State { get; internal set; } = ShrinkFiberState.Inactive;
/// 是否到达过 Active(用于断言“从未激活”)。
public bool EverActive { get; internal set; }
/// 最近一次 apply 抛出的异常;成功激活后清空。
public Exception? LastError { get; internal set; }
/// 已提交视图(算法 5 reload 提交 / 算法 6 读取):依赖键 → 绑定快照。
public IReadOnlyDictionary? Committed => _committed;
/// 进行中转换的句柄;任务在纤程到达静止态(Active 或 Inactive)时完成。
public UniTask Inertia { get; internal set; }
internal Dictionary? _committed;
internal long[]? Target;
internal bool InTransition;
internal bool Retired;
internal HashSet InjectSet { get; }
internal HashSet ProvideSet { get; }
}
}