groupByCount<S, K> static method

FlowHandle groupByCount<S, K>(
  1. Cell source, {
  2. required K keyOf(
    1. S value
    ),
  3. required int size,
  4. GroupErrorHandler? onError,
})

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

  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.
  4. If the list reaches size, it's emitted as a Grouped record.
  5. 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:

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