bufferWithTimeAndCount<S> static method

FlowHandle bufferWithTimeAndCount<S>(
  1. Cell source, {
  2. required Duration duration,
  3. required int count,
  4. bool emitEmpty = false,
  5. BufferErrorHandler? onError,
})

Flush on count or duration.

bufferWithTimeAndCount combines time-based and count-based buffering. The buffer flushes when either condition is met.

When to use

Use bufferWithTimeAndCount when you need to flush on time or count, whichever comes first.

  • Maximum Latency: Ensuring buffers don't get too old.
  • Maximum Size: Ensuring buffers don't get too large.
  • Hybrid Batching: Batching with both time and size constraints.
  • Resource Management: Managing buffer memory and latency.
  • Performance: Balancing throughput and latency.

How it works

  1. The first typed pulse starts the timer.
  2. Each pulse's payload is added to the buffer.
  3. If the buffer reaches count, it flushes immediately.
  4. If duration elapses, it flushes (even if count not reached).
  5. The buffer is cleared after each flush.
  6. The timer resets after each flush.

Parameters:

  • source: The Source Cell. The cell providing the input values.
  • duration: Time Limit. The maximum time between flushes.
  • count: Size Limit. The maximum number of items in a buffer.
  • emitEmpty: Emit Empty Buffers. If true, empty buffers are emitted as empty lists. Defaults to false.
  • 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 buffered values.

Example

final input = Cell.ingress<int>();

final handle = Flow.bufferWithTimeAndCount<int>(
  input.cell,
  duration: Duration(seconds: 1),
  count: 10,
);

// Flushes every second OR when 10 items accumulate

See Also:

Implementation

static FlowHandle bufferWithTimeAndCount<S>(
  Cell source, {
  required Duration duration,
  required int count,
  bool emitEmpty = false,
  BufferErrorHandler? onError,
}) {
  return BufferWithTimeAndCount<S>(
    duration: duration,
    count: count,
    emitEmpty: emitEmpty,
    onError: onError,
  ).toHandle(source: source);
}