AsyncPublisherWithBuffer<Log extends CustomLog> class final

An asynchronous publisher that buffers log events and processes them in batches.

Instead of processing each log event individually, this publisher collects logs into a buffer and flushes them as a list to its handler. This is optimal for high-throughput environments where batching I/O operations (like database inserts or network uploads) avoids performance bottlenecks.

Example usage:

final asyncPublisher = AsyncPublisherWithBuffer<Log>((logs, retryBuffer) async {
  try {
    await db.insertBatch(logs);
  } on Object catch (error, stackTrace) {
    // Return unhandled logs to retry them next time
    retryBuffer.addAll(logs);
    errorReport(error, stackTrace);
  }
});
Implemented types

Constructors

AsyncPublisherWithBuffer(FutureOr<void> handler(List<Log> logs, List<Log> retryBuffer), {bool sync = false, void onError(Object error, StackTrace stackTrace)?, void onDropped(List<Log> entries)?, Duration retryDelay = Duration.zero, int maxRetries = 100, int? maxQueueSize = 100000})
Creates a publisher backed by handler.

Properties

handler FutureOr<void> Function(List<Log> logs, List<Log> retryBuffer)
The function that processes a batch of buffered logs.
final
hashCode int
The hash code for this object.
no setterinherited
isClosed bool
Whether close has been called.
no setterinherited
maxQueueSize int?
The most entries the queue accepts before it starts refusing them.
finalinherited
maxRetries int
How many times a batch handed back through the retry buffer is retried before it is dropped.
finalinherited
onDropped → void Function(List<Log> entries)?
Called with entries that will never be delivered.
finalinherited
onError → void Function(Object error, StackTrace stackTrace)?
Called when handle throws.
finalinherited
retryDelay Duration
How long to wait before retrying a batch that was handed back through the retry buffer.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sync bool
Whether the queue hands an entry to handle on the stack of the call that published it.
finalinherited

Methods

close() Future<void>
Closes the publisher after draining the queue: every entry accepted before closing is processed, including entries published while a batch was in flight. Entries returned to the retry buffer after closing are dropped and handed to onDropped.
inherited
flush() Future<void>
Completes when the queue has been fully drained.
inherited
handle(List<Log> logs, List<Log> retryBuffer) FutureOr<void>
Processes a batch of buffered logs.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
publish(Log log) → void
Publishes the given log event.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited