AsyncDispatcher

class AsyncDispatcher(router: Router, tracer: Tracer = NullTracer, batchSize: Int = 5, pollDelayMs: Long = 10)

Coroutine-driven event dispatcher.

Events are pushed onto a FIFO queue with dispatch, the dispatcher loop picks them up in batches of batchSize, routes each event through the Router, and feeds any produced events back onto the queue. A TerminateEvent anywhere in the produced output stops the loop.

Call start with a caller-owned CoroutineScope to launch the loop, and stop (or cancel the scope) to shut it down. Tests can use waitForEmptyQueue to settle the queue before asserting.

Constructors

Link copied to clipboard
constructor(router: Router, tracer: Tracer = NullTracer, batchSize: Int = 5, pollDelayMs: Long = 10)

Functions

Link copied to clipboard
suspend fun dispatch(event: Event)

Push event onto the queue. Assigns a fresh correlation id when missing.

Link copied to clipboard
suspend fun queueSize(): Int

Read the current queue depth — exposed for diagnostics and tests.

Link copied to clipboard
fun start(scope: CoroutineScope): Job

Launch the dispatch loop on the caller-supplied scope and return the Job.

Link copied to clipboard
suspend fun stop()

Set the stop flag and join the running dispatch loop.

Link copied to clipboard
suspend fun waitForEmptyQueue(timeoutMs: Long? = null): Boolean

Suspend until the queue is empty AND no agent is currently being awaited, or until timeoutMs elapses. Returns true if the queue drained, false on timeout.