bufferWithTimeAndCount<S> static method
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
- The first typed pulse starts the timer.
- Each pulse's payload is added to the buffer.
- If the buffer reaches
count, it flushes immediately. - If
durationelapses, it flushes (even if count not reached). - The buffer is cleared after each flush.
- 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. Iftrue, empty buffers are emitted as empty lists. Defaults tofalse.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:
- bufferCount: For count-based buffering.
- bufferTime: For time-based buffering.
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);
}