mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
2. [Script]脚本节点移除了“getFlowContext”内置方法,改为自动识别入参名称为“context""flowContext""flow_context",如果是,将自动使用 IFlowContext 类型参数(运行时自动给定) 3. NodeFlow项目中,FlowLibraryService添加了GetType(string)以及TryGetType(string,Type?)方法,用于流程环境搜索外部加载的程序集类型。
91 lines
2.4 KiB
C#
91 lines
2.4 KiB
C#
using Serein.Library.Api;
|
||
|
||
namespace Serein.Script
|
||
{
|
||
public sealed class ScriptInvokeContext : IScriptInvokeContext
|
||
{
|
||
/// <summary>
|
||
/// 不使用流程上下文
|
||
/// </summary>
|
||
public ScriptInvokeContext()
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 定义的变量
|
||
/// </summary>
|
||
private Dictionary<string, object?> _variables = new Dictionary<string, object?>();
|
||
|
||
/// <summary>
|
||
/// 取消令牌源,用于控制脚本的执行
|
||
/// </summary>
|
||
private CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
||
|
||
/// <summary>
|
||
/// 是否该退出了
|
||
/// </summary>
|
||
public bool IsReturn => _tokenSource.IsCancellationRequested;
|
||
|
||
/// <summary>
|
||
/// 是否严格检查 Null 值 (禁止使用 Null)
|
||
/// </summary>
|
||
public bool IsCheckNullValue { get; set; }
|
||
|
||
/// <summary>
|
||
/// 是否需要提前返回(用于脚本中提前结束)
|
||
/// </summary>
|
||
public bool IsNeedReturn { get; set; }
|
||
|
||
|
||
/// <summary>
|
||
/// 获取变量的值
|
||
/// </summary>
|
||
/// <param name="varName"></param>
|
||
/// <returns></returns>
|
||
object? IScriptInvokeContext.GetVarValue(string varName)
|
||
{
|
||
_variables.TryGetValue(varName, out var value);
|
||
return value;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 设置变量的值
|
||
/// </summary>
|
||
/// <param name="varName"></param>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
bool IScriptInvokeContext.SetVarValue(string varName, object? value)
|
||
{
|
||
if (!_variables.TryAdd(varName, value))
|
||
{
|
||
_variables[varName] = value;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
void IScriptInvokeContext.OnExit()
|
||
{
|
||
// 清理脚本中加载的非托管资源
|
||
foreach (var nodeObj in _variables.Values)
|
||
{
|
||
if (nodeObj is not null)
|
||
{
|
||
if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
|
||
{
|
||
disposable?.Dispose();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
}
|
||
_tokenSource.Cancel();
|
||
_variables.Clear();
|
||
}
|
||
|
||
}
|
||
}
|