groupByCount<S, K> static method
Emit a group's list when it hits size.
groupByCount accumulates values by group and emits the list when a group reaches the specified size. The group is then cleared.
When to use
Use groupByCount when you need to batch values by group and emit when a batch is full.
- Batch Processing: Processing items in batches by group.
- Chunking: Chunking values by group.
- Pagination: Paginating grouped items.
- Bulk Operations: Performing bulk operations by group.
- Rate Limiting: Limiting processing by group.
- Windowed Processing: Processing windows of grouped data.
How it works
- Each incoming pulse is type-checked.
- If the type matches,
keyOfis called with the payload. - The value is added to the group's list.
- If the list reaches
size, it's emitted as aGroupedrecord. - The group is cleared from the internal map.
Parameters:
source: The Source Cell. The cell providing the input values.keyOf: Key Extraction Function. Called with each typed payload, returns the group key.size: Batch Size. The number of items required to emit.onError: Error Handler. Optional callback for handling errors.
Type Parameters:
S: The type of the input payload.K: The type of the group key.
Returns:
A FlowHandle that can be used to observe the grouped batches.
Example
final words = Cell.ingress<String>();
final handle = Flow.groupByCount<String, String>(
words.cell,
keyOf: (word) => word[0].toUpperCase(),
size: 3,
);
words.emit('apple'); // no output
words.emit('apricot'); // no output
words.emit('avocado'); // -> Grouped(A, [apple, apricot, avocado])
See Also:
- groupBy: For tagging values with group keys.
- groupCollect: For accumulating values by group.
Implementation
static FlowHandle groupByCount<S, K>(
Cell source, {
required K Function(S value) keyOf,
required int size,
GroupErrorHandler? onError,
}) {
return GroupByCount<S, K>(keyOf, size, onError: onError)
.toHandle(source: source);
}