Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Created February 18, 2011 10:07
Show Gist options
  • Save grumpydev/833489 to your computer and use it in GitHub Desktop.
Save grumpydev/833489 to your computer and use it in GitHub Desktop.
namespace Pipelines
{
using System;
using System.Collections.Generic;
using System.Linq;
public class CancellablePipeline<TPipelineDelegateType, TReturnType>
where TReturnType : class
{
public CancellablePipeline()
{
if (!typeof(Delegate).IsAssignableFrom(typeof(TPipelineDelegateType)))
{
throw new ArgumentException("TPipelineDelegateType must be of a delegate type", "TPipelineDelegateType");
}
this.pipelineItems = new List<TPipelineDelegateType>();
}
private List<TPipelineDelegateType> pipelineItems;
public IEnumerable<TPipelineDelegateType> PipelineItems
{
get
{
return this.pipelineItems.AsReadOnly();
}
}
public TReturnType Invoke(params object[] args)
{
TReturnType returnValue = null;
using (var enumerator = this.PipelineItems.Select(i => i as Delegate).GetEnumerator())
{
while (returnValue == null && enumerator.MoveNext())
{
returnValue = enumerator.Current.DynamicInvoke(args) as TReturnType;
}
}
return returnValue;
}
public void AddItemToStartOfPipeline(TPipelineDelegateType item)
{
this.InsertItemAtPipelineIndex(0, item);
}
public void AddItemToEndOfPipeline(TPipelineDelegateType item)
{
this.pipelineItems.Add(item);
}
public void InsertItemAtPipelineIndex(int index, TPipelineDelegateType item)
{
this.pipelineItems.Insert(index, item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment