using Avalonia.Media.Imaging; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Avalar.Models { public class AvalarPipeline { private readonly IEnumerable m_steps; public AvalarPipeline(IEnumerable steps) { m_steps = steps ?? throw new ArgumentNullException(nameof(steps)); foreach (var step in m_steps) { step.PropertyChanged += OnPipelineComponentChanged; } } private void OnPipelineComponentChanged(object sender, PropertyChangedEventArgs e) { foreach (var step in m_steps.SkipWhile(step => step != sender)) { step.InvalidateResult(); } PipelineChanged?.Invoke(this, EventArgs.Empty); } public event EventHandler PipelineChanged; public async Task Run(IBitmap bitmap, CancellationToken token) { var input = bitmap; foreach(var step in m_steps) { token.ThrowIfCancellationRequested(); input = await step.Run(input, token).ConfigureAwait(false); } return input; } } }