groupCollect<S, K> static method

FlowHandle groupCollect<S, K>(
  1. Cell source, {
  2. required K keyOf(
    1. S value
    ),
  3. Map<K, List<S>>? groups,
  4. GroupErrorHandler? onError,
})

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

  1. Each incoming pulse is type-checked.
  2. If the type matches, keyOf is called with the payload.
  3. The value is added to the group's list in the internal map.
  4. The current map is emitted as a snapshot.
  5. If keyOf throws 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:

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);
}