using Serein.Library.Api; using Serein.Library.Enums; using Serein.Library.Utils; using System; using System.Collections.Concurrent; using System.Security.Claims; using System.Threading.Tasks; namespace Serein.Library.Framework.NodeFlow { /// /// 动态流程上下文 /// public class DynamicContext : IDynamicContext { public DynamicContext(/*ISereinIOC sereinIoc,*/ IFlowEnvironment flowEnvironment) { // SereinIoc = sereinIoc; Env = flowEnvironment; RunState = RunState.Running; } /// /// 运行环境 /// public IFlowEnvironment Env { get; } /// /// 运行状态 /// public RunState RunState { get; set; } = RunState.NoStart; /// /// 每个上下文分别存放节点的当前数据 /// private readonly ConcurrentDictionary dictNodeFlowData = new ConcurrentDictionary(); /// /// 获取节点当前数据 /// /// /// public object GetFlowData(string nodeGuid) { if (dictNodeFlowData.TryGetValue(nodeGuid, out var data)) { return data; } { return null; } } /// /// 添加或更新当前节点数据 /// /// 节点Guid /// 新的数据 public void AddOrUpdate(string nodeGuid, object flowData) { // this.dictNodeFlowData.TryGetValue(nodeGuid, out var oldFlowData); this.dictNodeFlowData[nodeGuid] = flowData; } /// /// 结束流程 /// public void EndCurrentBranch() { this.dictNodeFlowData?.Clear(); RunState = RunState.Completion; } // public NodeRunCts NodeRunCts { get; set; } // public ISereinIOC SereinIoc { get; } //public Task CreateTimingTask(Action action, int time = 100, int count = -1) //{ // if(NodeRunCts == null) // { // NodeRunCts = Env.IOC.Get(); // } // // 使用局部变量,避免捕获外部的 `action` // Action localAction = action; // return Task.Run(async () => // { // for (int i = 0; i < count && !NodeRunCts.IsCancellationRequested; i++) // { // await Task.Delay(time); // if (NodeRunCts.IsCancellationRequested) { break; } // //if (FlowEnvironment.IsGlobalInterrupt) // //{ // // await FlowEnvironment.GetOrCreateGlobalInterruptAsync(); // //} // // 确保对局部变量的引用 // localAction?.Invoke(); // } // // 清理引用,避免闭包导致的内存泄漏 // localAction = null; // }); //} } }