mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
95 lines
2.8 KiB
C#
95 lines
2.8 KiB
C#
using Serein.Library.Api;
|
|
using Serein.Library.Utils;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace Serein.Library.Core.NodeFlow
|
|
{
|
|
|
|
/// <summary>
|
|
/// 动态流程上下文
|
|
/// </summary>
|
|
public class DynamicContext: IDynamicContext
|
|
{
|
|
/// <summary>
|
|
/// 动态流程上下文
|
|
/// </summary>
|
|
/// <param name="flowEnvironment"></param>
|
|
public DynamicContext(IFlowEnvironment flowEnvironment)
|
|
{
|
|
Env = flowEnvironment;
|
|
RunState = RunState.Running;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 运行环境
|
|
/// </summary>
|
|
public IFlowEnvironment Env { get; }
|
|
|
|
/// <summary>
|
|
/// 运行状态
|
|
/// </summary>
|
|
public RunState RunState { get; set; } = RunState.NoStart;
|
|
|
|
/// <summary>
|
|
/// 当前节点执行完成后,设置该属性,让运行环境判断接下来要执行哪个分支的节点。
|
|
/// </summary>
|
|
public ConnectionInvokeType NextOrientation { get; set; }
|
|
|
|
/// <summary>
|
|
/// 每个上下文分别存放节点的当前数据
|
|
/// </summary>
|
|
private readonly ConcurrentDictionary<string,object?> dictNodeFlowData = new ConcurrentDictionary<string, object?>();
|
|
|
|
/// <summary>
|
|
/// 获取节点当前数据
|
|
/// </summary>
|
|
/// <param name="nodeGuid"></param>
|
|
/// <returns></returns>
|
|
public object? GetFlowData(string nodeGuid)
|
|
{
|
|
if (string.IsNullOrEmpty(nodeGuid))
|
|
{
|
|
return null;
|
|
}
|
|
if(dictNodeFlowData.TryGetValue(nodeGuid,out var data))
|
|
{
|
|
return data;
|
|
}
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加或更新当前节点数据
|
|
/// </summary>
|
|
/// <param name="nodeGuid">节点Guid</param>
|
|
/// <param name="flowData">新的数据</param>
|
|
public void AddOrUpdate(string nodeGuid,object? flowData)
|
|
{
|
|
// this.dictNodeFlowData.TryGetValue(nodeGuid, out var oldFlowData);
|
|
this.dictNodeFlowData.AddOrUpdate(nodeGuid, _ => flowData, (_, _) => flowData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 结束流程
|
|
/// </summary>
|
|
public void Exit()
|
|
{
|
|
foreach (var nodeObj in dictNodeFlowData.Values)
|
|
{
|
|
if (nodeObj is not null)
|
|
{
|
|
if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
|
|
{
|
|
disposable?.Dispose();
|
|
}
|
|
}
|
|
}
|
|
this.dictNodeFlowData?.Clear();
|
|
RunState = RunState.Completion;
|
|
}
|
|
|
|
}
|
|
}
|