groupCollect<S, K> static method
Running Map<K, List<S>>.
groupCollect accumulates values into groups and emits the complete map after every pulse.
When to use
Use groupCollect when you need a running snapshot of all grouped values.
- Real-time Dashboard: Showing real-time group summaries.
- Aggregation: Aggregating values by group over time.
- State Monitoring: Monitoring grouped state.
- Caching: Maintaining a cache of grouped values.
- Batch Processing: Preparing batches by group.
- Reporting: Generating reports on 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 in the internal map.
- The current map is emitted as a snapshot.
- If
keyOfthrows an error, the pulse is dropped.
Parameters:
source: The Source Cell. The cell providing the input values.keyOf: Key Extraction Function. Called with each typed payload, returns the group key.groups: Initial Groups Map. Optional. Use this to seed the groups or to access the map externally.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 maps.
Example
final input = Cell.ingress<int>();
final handle = Flow.groupCollect<int, String>(
input.cell,
keyOf: (n) => n.isEven ? 'even' : 'odd',
);
input.emit(1); // -> {odd: [1]}
input.emit(2); // -> {odd: [1], even: [2]}
See Also:
- groupBy: For tagging values with group keys.
- groupByCount: For batching values by group.
Implementation
static FlowHandle groupCollect<S, K>(
Cell source, {
required K Function(S value) keyOf,
Map<K, List<S>>? groups,
GroupErrorHandler? onError,
}) {
return GroupCollect<S, K>(
keyOf,
groups: groups,
onError: onError,
).toHandle(source: source);
}