Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created August 23, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pzixel/50ab4a4bb723bca075225eaa9631429b to your computer and use it in GitHub Desktop.
Save Pzixel/50ab4a4bb723bca075225eaa9631429b to your computer and use it in GitHub Desktop.
public class ActionBatcher<T>
{
private readonly Action<IReadOnlyList<T>> action;
private List<T> batch;
public ActionBatcher(Action<IReadOnlyList<T>> action)
{
this.action = action;
batch = new List<T>();
}
public bool IsRunning { get; private set; }
public IReadOnlyList<T> MostRecentBatch { get; private set; }
public int BatchSize => batch.Count;
public void RunOrBatch(T item)
{
batch.Add(item);
if (!IsRunning)
{
IsRunning = true;
RunNextBatch();
}
}
public void RunNextBatch()
{
if (BatchSize > 0)
{
action(batch);
MostRecentBatch = batch;
batch = new List<T>();
}
else
{
IsRunning = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment