Files
serein-flow/NodeFlow/Model/SingleFlipflopNode.cs

102 lines
3.7 KiB
C#
Raw Normal View History

using Serein.Library.Api;
using Serein.Library.Entity;
using Serein.Library.Enums;
2024-09-15 22:07:10 +08:00
using Serein.Library.Ex;
2024-10-11 16:46:16 +08:00
using Serein.Library.NodeFlow.Tool;
using Serein.Library.Utils;
using Serein.NodeFlow.Base;
using static Serein.Library.Utils.ChannelFlowInterrupt;
2024-08-06 16:09:46 +08:00
namespace Serein.NodeFlow.Model
{
public class SingleFlipflopNode : NodeModelBase
2024-08-06 16:09:46 +08:00
{
//public override async Task<object?> Executing(IDynamicContext context)
//public override Task<object?> ExecutingAsync(IDynamicContext context)
//{
// NextOrientation = Library.Enums.ConnectionType.IsError;
// RuningException = new FlipflopException ("无法以非await/async的形式调用触发器");
// return null;
//}
2024-09-28 23:55:19 +08:00
public override async Task<object?> ExecutingAsync(IDynamicContext context)
2024-08-06 16:09:46 +08:00
{
#region
2024-09-22 17:37:32 +08:00
if (DebugSetting.InterruptClass != InterruptClass.None) // 执行触发前
{
string guid = this.Guid.ToString();
2024-09-22 17:37:32 +08:00
var cancelType = await this.DebugSetting.GetInterruptTask();
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{cancelType},开始执行后继分支");
}
#endregion
MethodDetails md = MethodDetails;
if (!context.Env.TryGetDelegateDetails(md.MethodName, out var dd))
{
throw new Exception("不存在对应委托");
}
object instance = md.ActingInstance;
try
{
var args = GetParameters(context, this, md);
2024-10-10 20:52:19 +08:00
var result = await dd.Invoke(md.ActingInstance, args);
2024-10-11 16:46:16 +08:00
dynamic flipflopContext = result;
FlipflopStateType flipflopStateType = flipflopContext.State;
NextOrientation = flipflopStateType.ToContentType();
if (flipflopContext.Type == TriggerType.Overtime)
{
2024-10-11 16:46:16 +08:00
throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
2024-09-28 23:55:19 +08:00
}
2024-10-11 16:46:16 +08:00
return flipflopContext.Value;
}
catch (FlipflopException ex)
{
2024-10-11 19:31:34 +08:00
if(ex.Type == FlipflopException.CancelClass.Flow)
2024-10-07 15:15:18 +08:00
{
throw;
}
2024-10-11 16:46:16 +08:00
await Console.Out.WriteLineAsync($"触发器[{this.MethodDetails.MethodName}]异常:" + ex);
NextOrientation = ConnectionType.None;
RuningException = ex;
return null;
}
catch (Exception ex)
{
2024-10-11 16:46:16 +08:00
await Console.Out.WriteLineAsync($"触发器[{this.MethodDetails.MethodName}]异常:" + ex);
NextOrientation = ConnectionType.IsError;
RuningException = ex;
return null;
}
finally
{
// flipflopTask?.Dispose();
}
2024-08-06 16:09:46 +08:00
}
2024-10-11 16:46:16 +08:00
public static object GetContextValueDynamic(dynamic context)
{
return context.Value; // dynamic 会在运行时处理类型
}
internal override Parameterdata[] GetParameterdatas()
{
if (base.MethodDetails.ExplicitDatas.Length > 0)
{
return MethodDetails.ExplicitDatas
.Select(it => new Parameterdata
{
State = it.IsExplicitData,
Value = it.DataValue
})
.ToArray();
}
else
{
return [];
}
}
2024-08-06 16:09:46 +08:00
}
}