Files
KJD/Store.Product/Publish/PublishPipeline.cs
T
2026-09-14 09:16:59 +08:00

54 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Store.Product.Publish;
public class PublishPipeline<TContext> where TContext : PublishContextBase
{
private readonly IEnumerable<IPublishStep<TContext>> _steps;
public PublishPipeline(IEnumerable<IPublishStep<TContext>> steps)
{
_steps = steps;
}
public async Task ExecuteAsync(
TContext context,
CancellationToken token = default)
{
try
{
foreach (var step in _steps)
{
token.ThrowIfCancellationRequested();
if (await step.CanExecuteAsync(context, token))
{
await step.ExecuteAsync(context, token);
}
// 步骤正常提前结束(提交成功/上传到草稿箱)时会写入 Result 并标记完成,此处跳出后续步骤。
if (context.Parameters?.IsCompleted == true)
{
break;
}
}
}
catch (Store.Product.Publish.Steps.Wx.PublishCompletedException ex)
{
context.Parameters.Result = ex.Result;
context.Parameters.IsCompleted = true;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception)
{
throw;
}
}
}