using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace BililiveRecorder.Flv.Pipeline { public class ProcessingPipelineBuilder : IProcessingPipelineBuilder { public IServiceProvider ServiceProvider { get; } private readonly List> rules = new List>(); public ProcessingPipelineBuilder(IServiceProvider serviceProvider) { this.ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public IProcessingPipelineBuilder Add(Func rule) { this.rules.Add(rule); return this; } public ProcessingDelegate Build() => this.rules.AsEnumerable().Reverse().Aggregate((ProcessingDelegate)(_ => Task.CompletedTask), (i, o) => o(i)); } }