BililiveRecorder/BililiveRecorder.Flv/Pipeline/ProcessingPipelineBuilder.cs

37 lines
1.2 KiB
C#
Raw Normal View History

2021-02-08 16:51:19 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2021-02-08 16:51:19 +08:00
namespace BililiveRecorder.Flv.Pipeline
{
public class ProcessingPipelineBuilder : IProcessingPipelineBuilder
{
public IServiceCollection ServiceCollection { get; }
2021-02-08 16:51:19 +08:00
private readonly List<Func<ProcessingDelegate, IServiceProvider, ProcessingDelegate>> rules = new();
2021-02-08 16:51:19 +08:00
public ProcessingPipelineBuilder() : this(new ServiceCollection())
{ }
public ProcessingPipelineBuilder(IServiceCollection servicesCollection)
2021-02-08 16:51:19 +08:00
{
this.ServiceCollection = servicesCollection;
2021-02-08 16:51:19 +08:00
}
public IProcessingPipelineBuilder AddRule(Func<ProcessingDelegate, IServiceProvider, ProcessingDelegate> rule)
2021-02-08 16:51:19 +08:00
{
this.rules.Add(rule);
return this;
}
public ProcessingDelegate Build()
{
this.ServiceCollection.TryAddSingleton(_ => new ProcessingPipelineSettings());
var provider = this.ServiceCollection.BuildServiceProvider();
return this.rules.AsEnumerable().Reverse().Aggregate((ProcessingDelegate)(_ => { }), (i, o) => o(i, provider));
}
2021-02-08 16:51:19 +08:00
}
}