Created
August 23, 2019 10:20
-
-
Save Pzixel/50ab4a4bb723bca075225eaa9631429b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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