2021-02-08 16:51:19 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-06-25 17:31:21 +08:00
|
|
|
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
|
|
|
|
{
|
2022-06-25 17:31:21 +08:00
|
|
|
public IServiceCollection ServiceCollection { get; }
|
2021-02-08 16:51:19 +08:00
|
|
|
|
2022-06-25 17:31:21 +08:00
|
|
|
private readonly List<Func<ProcessingDelegate, IServiceProvider, ProcessingDelegate>> rules = new();
|
2021-02-08 16:51:19 +08:00
|
|
|
|
2022-06-25 17:31:21 +08:00
|
|
|
public ProcessingPipelineBuilder() : this(new ServiceCollection())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public ProcessingPipelineBuilder(IServiceCollection servicesCollection)
|
2021-02-08 16:51:19 +08:00
|
|
|
{
|
2022-06-25 17:31:21 +08:00
|
|
|
this.ServiceCollection = servicesCollection;
|
2021-02-08 16:51:19 +08:00
|
|
|
}
|
|
|
|
|
2022-06-25 17:31:21 +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()
|
2022-06-25 17:31:21 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|