Class Batcher
Provides utility methods for processing items in batches with support for callbacks and error handling.
Inheritance
Inherited Members
Namespace: MGeLabs.Utils.Data
Assembly: cs.temp.dll.dll
Syntax
public static class Batcher
Methods
ProcessInBatches<T>(IReadOnlyList<T>, Action<T>, Int32, Single, Action<T>, Action<T, Boolean>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<T, Exception>)
Executes an action on a list of items in batches, with optional callbacks for various stages of processing.
Declaration
public static IEnumerator ProcessInBatches<T>(IReadOnlyList<T> itemsList, Action<T> actionOnItem, int itemsPerBatch = 5, float batchDelay = 0.1F, Action<T> onItemProcessStart = null, Action<T, bool> onItemProcessFinished = null, Action<IReadOnlyList<T>> onBatchStart = null, Action<IReadOnlyList<T>> onBatchFinished = null, Action<IReadOnlyList<T>> onFinished = null, Action<T, Exception> onError = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<T> | itemsList | The list of items to process. |
| Action<T> | actionOnItem | The action to execute on each item. |
| System.Int32 | itemsPerBatch | The number of items to process per batch. Defaults to 5. |
| System.Single | batchDelay | The delay (in seconds) between processing batches. Defaults to 0.1f. |
| Action<T> | onItemProcessStart | Optional callback invoked before processing each item;
receives the item as |
| Action<T, System.Boolean> | onItemProcessFinished | Optional callback invoked after processing each item;
receives the item as |
| Action<IReadOnlyList<T>> | onBatchStart | Optional callback invoked before processing each batch;
receives the batch as |
| Action<IReadOnlyList<T>> | onBatchFinished | Optional callback invoked when a batch is completed;
receives the batch as |
| Action<IReadOnlyList<T>> | onFinished | Optional callback invoked when all items have been processed;
receives the full list of processed items as |
| Action<T, Exception> | onError | Optional callback invoked when an error occurs during item processing;
receives the item that failed ( |
Returns
| Type | Description |
|---|---|
| IEnumerator | An enumerator that can be used to execute the batches over time. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Examples
IReadOnlyList<int> numbers = new IReadOnlyList<int>() { 1, 2, 3, 4, 5, 6, 7 };
StartCoroutine(Batcher.ProcessInBatches(
numbers,
n => Debug.Log($"Processing: {n}"),
itemsPerBatch: 3,
batchDelay: 0.5f,
onBatchStart: batch => Debug.Log($"Batch starting: {string.Join(", ", batch)}"),
onBatchFinished: batch => Debug.Log($"Batch finished: {string.Join(", ", batch)}"),
onFinished: all => Debug.Log("All items processed.")
));
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
ProcessInBatches<T>(IReadOnlyList<T>, Func<T, IEnumerator>, Int32, Single, Action<T>, Action<T, Boolean>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>)
Processes a list of items in batches, where each item is processed using a coroutine (IEnumerator).
Declaration
public static IEnumerator ProcessInBatches<T>(IReadOnlyList<T> itemsList, Func<T, IEnumerator> routineOnItem, int itemsPerBatch = 5, float batchDelay = 0.1F, Action<T> onItemProcessStart = null, Action<T, bool> onItemProcessFinished = null, Action<IReadOnlyList<T>> onBatchStart = null, Action<IReadOnlyList<T>> onBatchFinished = null, Action<IReadOnlyList<T>> onFinished = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<T> | itemsList | The list of items to process. |
| Func<T, IEnumerator> | routineOnItem | The coroutine to execute for each item. |
| System.Int32 | itemsPerBatch | The number of items to process per batch. Defaults to 5. |
| System.Single | batchDelay | The delay (in seconds) between processing batches. Defaults to 0.1f. |
| Action<T> | onItemProcessStart | Optional callback invoked before processing each item;
receives the item as |
| Action<T, System.Boolean> | onItemProcessFinished | Optional callback invoked after processing each item;
receives the item as |
| Action<IReadOnlyList<T>> | onBatchStart | Optional callback invoked before processing each batch;
receives the batch as |
| Action<IReadOnlyList<T>> | onBatchFinished | Optional callback invoked when a batch is completed;
receives the batch as |
| Action<IReadOnlyList<T>> | onFinished | Optional callback invoked when all items have been processed;
receives the full list of processed items as |
Returns
| Type | Description |
|---|---|
| IEnumerator | An enumerator that can be used to execute the batches over time. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the list. |
Examples
public class AudioLoaderExample : MonoBehaviour
{
void Start()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
StartCoroutine(Batcher.ProcessInBatches(
numbers,
number => ProcessNumberCoroutine(number),
itemsPerBatch: 3,
batchDelay: 0.5f,
onBatchStart: batch => Debug.Log($"Batch starting: {string.Join(\", \", batch)}"),
onBatchFinished: batch => Debug.Log($"Batch finished: {string.Join(\", \", batch)}"),
onFinished: all => Debug.Log("All items processed")
));
}
private IEnumerator ProcessNumberCoroutine(int number)
{
Debug.Log($"Processing {number}");
// Simulate async work
yield return null;
}
}
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
ProcessPreBatched<T>(IReadOnlyList<IReadOnlyList<T>>, Action<T>, Single, Action<T>, Action<T, Boolean>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<T, Exception>)
Executes an action on pre-batched lists of items, with optional callbacks for various stages of processing.
Declaration
public static IEnumerator ProcessPreBatched<T>(IReadOnlyList<IReadOnlyList<T>> preBatchedLists, Action<T> actionOnItem, float batchDelay = 0.1F, Action<T> onItemProcessStart = null, Action<T, bool> onItemProcessFinished = null, Action<IReadOnlyList<T>> onBatchStart = null, Action<IReadOnlyList<T>> onBatchFinished = null, Action<IReadOnlyList<T>> onFinished = null, Action<T, Exception> onError = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<IReadOnlyList<T>> | preBatchedLists | The pre-batched lists of items to process. |
| Action<T> | actionOnItem | The action to execute on each item. |
| System.Single | batchDelay | The delay (in seconds) between processing batches. Defaults to 0.1f. |
| Action<T> | onItemProcessStart | Optional callback invoked before processing each item;
receives the item as |
| Action<T, System.Boolean> | onItemProcessFinished | Optional callback invoked after processing each item;
receives the item as |
| Action<IReadOnlyList<T>> | onBatchStart | Optional callback invoked before processing each batch;
receives the batch as |
| Action<IReadOnlyList<T>> | onBatchFinished | Optional callback invoked when a batch is completed;
receives the batch as |
| Action<IReadOnlyList<T>> | onFinished | Optional callback invoked when all items have been processed;
receives the full list of processed items as |
| Action<T, Exception> | onError | Optional callback invoked when an error occurs during item processing;
receives the item that failed ( |
Returns
| Type | Description |
|---|---|
| IEnumerator | An enumerator that can be used to execute the batches over time. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the batches. |
Examples
IReadOnlyList<IReadOnlyList<int>> batches = new IReadOnlyList<IReadOnlyList<int>>()
{
new IReadOnlyList<int>() { 1, 2, 3 },
new IReadOnlyList<int>() { 4, 5, 6 },
new IReadOnlyList<int>() { 7 }
};
StartCoroutine(Batcher.ProcessPreBatched(
batches,
n => Debug.Log($"Processing: {n}"),
batchDelay: 0.5f,
onBatchStart: batch => Debug.Log($"Batch starting: {string.Join(", ", batch)}"),
onBatchFinished: batch => Debug.Log($"Batch finished: {string.Join(", ", batch)}"),
onFinished: all => Debug.Log("All items processed.")
));
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
ProcessPreBatched<T>(IReadOnlyList<IReadOnlyList<T>>, Func<T, IEnumerator>, Single, Action<T>, Action<T, Boolean>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>, Action<IReadOnlyList<T>>)
Processes pre-batched lists of items, where each item is processed using a coroutine (IEnumerator).
Declaration
public static IEnumerator ProcessPreBatched<T>(IReadOnlyList<IReadOnlyList<T>> preBatchedLists, Func<T, IEnumerator> routineOnItem, float batchDelay = 0.1F, Action<T> onItemProcessStart = null, Action<T, bool> onItemProcessFinished = null, Action<IReadOnlyList<T>> onBatchStart = null, Action<IReadOnlyList<T>> onBatchFinished = null, Action<IReadOnlyList<T>> onFinished = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<IReadOnlyList<T>> | preBatchedLists | The pre-batched lists of items to process. |
| Func<T, IEnumerator> | routineOnItem | The coroutine to execute for each item. |
| System.Single | batchDelay | The delay (in seconds) between processing batches. Defaults to 0.1f. |
| Action<T> | onItemProcessStart | Optional callback invoked before processing each item;
receives the item as |
| Action<T, System.Boolean> | onItemProcessFinished | Optional callback invoked after processing each item;
receives the item as |
| Action<IReadOnlyList<T>> | onBatchStart | Optional callback invoked before processing each batch;
receives the batch as |
| Action<IReadOnlyList<T>> | onBatchFinished | Optional callback invoked when a batch is completed;
receives the batch as |
| Action<IReadOnlyList<T>> | onFinished | Optional callback invoked when all items have been processed;
receives the full list of processed items as |
Returns
| Type | Description |
|---|---|
| IEnumerator | An enumerator that can be used to execute the batches over time. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the batches. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |