AsyncAnimationSink

class AsyncAnimationSink(capacity: Int = DEFAULT_CAPACITY, overflowPolicy: AsyncAnimationSink.OverflowPolicy = OverflowPolicy.BLOCK, consume: (AnimationEvent) -> Unit) : AnimationSink(source)

An AnimationSink that hands events to a dedicated background writer thread through a bounded queue, so serialization and disk I/O happen off the simulation thread. This is the sink for watching a long run as it executes, where buffering the whole run in memory (as MemoryBufferedAnimationSink does) is not viable.

The simulation thread (producer) calls emit; a single writer thread (consumer) takes events in order and passes each to consume (which serializes/writes it). A bounded BlockingQueue sits between them and absorbs bursts. What happens when the producer outruns the writer and the queue fills is governed by overflowPolicy:

  • OverflowPolicy.BLOCK (default) — the producer waits for space. Lossless; may briefly slow the simulation under a sustained burst. Preferred for animation because events are state deltas: dropping one corrupts the renderer's reconstructed state downstream.

  • OverflowPolicy.DROP_NEWEST — discard the incoming event; never slows the sim.

  • OverflowPolicy.DROP_OLDEST — evict the oldest queued event to make room.

Shutdown is via onExperimentEnd, which enqueues a sentinel and joins the writer thread, guaranteeing every already-emitted event is written before it returns (no tail loss).

Parameters

capacity

the bounded queue size (number of events); must be >= 1

overflowPolicy

what emit does when the queue is full

consume

the write action invoked on the writer thread for each event, in emission order (e.g. { event -> output.write(event) })

Constructors

Link copied to clipboard
constructor(capacity: Int = DEFAULT_CAPACITY, overflowPolicy: AsyncAnimationSink.OverflowPolicy = OverflowPolicy.BLOCK, consume: (AnimationEvent) -> Unit)

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Policy applied by emit when the bounded queue is full.

Properties

Link copied to clipboard

Number of events discarded under a drop policy (0 under BLOCK).

Link copied to clipboard
open override val isActive: Boolean

Whether this sink is collecting events. Emission sites must check this before building and emitting an event. A false value must be a cheap, branch-predictable constant.

Link copied to clipboard

Number of events whose consume call threw and was swallowed.

Functions

Link copied to clipboard
open override fun emit(event: AnimationEvent)

Records event. Called only when isActive is true. Must not throw on the simulation thread; implementations should fail soft (e.g. drop and log) rather than disrupt the run.

Link copied to clipboard
open override fun onExperimentEnd()

Drains all emitted events and stops the writer thread. Idempotent.