diff options
Diffstat (limited to 'Models/PipelineStep.cs')
| -rw-r--r-- | Models/PipelineStep.cs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Models/PipelineStep.cs b/Models/PipelineStep.cs new file mode 100644 index 0000000..e8a4bf1 --- /dev/null +++ b/Models/PipelineStep.cs @@ -0,0 +1,65 @@ +using Avalar.Services; +using Avalonia.Media.Imaging; +using ReactiveUI; +using System; +using System.ComponentModel; +using System.Threading; +using System.Threading.Tasks; + +namespace Avalar.Models +{ + public class PipelineStep<T> : IPipelineStep where T : INotifyPropertyChanged, IProcessor + { + public PipelineStep(T processor) + { + m_Processor = processor ?? throw new ArgumentNullException(nameof(processor)); + m_Processor.PropertyChanged += OnPropertyChanged; + } + + private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Result))); + } + + public void InvalidateResult() + { + Result = null; + } + + private T m_Processor; + + public event PropertyChangedEventHandler PropertyChanged; + + private IBitmap Result { get; set; } + + public Task<IBitmap> Run(IBitmap bitmap, CancellationToken token) + { + if (Result != null) + { + return Task.FromResult(Result); + } + else + { + return Task.Run(() => + { + Result = m_Processor.Process(bitmap, token); + token.ThrowIfCancellationRequested(); + return Result; + }); + } + } + } + + public static class PipelineStep + { + public static PipelineStep<T> Create<T>(T processor) where T : INotifyPropertyChanged, IProcessor + { + return new PipelineStep<T>(processor); + } + + public static PipelineStep<T> AsPipelineStep<T>(this T processor) where T : INotifyPropertyChanged, IProcessor + { + return Create(processor); + } + } +} |
