tapWithIndex<S> static method

FlowHandle tapWithIndex<S>(
  1. Cell source, {
  2. required void onValue(
    1. S value,
    2. int index
    ),
  3. TapErrorHandler? onError,
})

Tap with a 0-based index.

tapWithIndex is similar to tap but the callback receives the index of each value in the sequence.

When to use

Use tapWithIndex when you need the index of each value in your side effect.

  • Position Tracking: Tracking the position of values.
  • Progress Reporting: Reporting progress through a sequence.
  • Index-Based Logging: Logging with index information.
  • Sequence Debugging: Debugging sequence order.
  • Batch Processing: Tracking batch position.

How it works

  1. Each incoming pulse is type-checked.
  2. If the type matches, onValue is called with the payload and index.
  3. The index starts at 0 and increments on each typed pulse.
  4. If onValue throws an error, the pulse is dropped.
  5. If onValue succeeds, the pulse is passed through unchanged.
  6. The index is incremented after the callback.

Parameters:

  • source: The Source Cell. The cell providing the input values.
  • onValue: Indexed Side Effect Callback. Called with each typed payload and its index. If it throws, the pulse is dropped.
  • onError: Error Handler. Optional callback for handling errors.

Type Parameters:

  • S: The type of the payload.

Returns:

A FlowHandle that can be used to observe the tapped values.

Example

final items = Cell.ingress<Item>();

final handle = Flow.tapWithIndex<Item>(
  items.cell,
  onValue: (item, index) {
    print('Processing item ${index + 1}');
  },
);

See Also:

  • tap: For simple side effects.
  • tapAll: For side effects on all pulses.

Implementation

static FlowHandle tapWithIndex<S>(
  Cell source, {
  required void Function(S value, int index) onValue,
  TapErrorHandler? onError,
}) {
  return TapWithIndex<S>(onValue, onError: onError).toHandle(source: source);
}