Class TransactionBus

java.lang.Object
dev.engine.core.transaction.TransactionBus

public class TransactionBus extends Object
Multi-consumer transaction bus with per-subscriber filtering and double-buffered swap.

Logic thread emits transactions via emit(Transaction). Each subscriber registers interest in specific Component types. On drain(Object), the subscriber's buffer is swapped and returned — one lightweight lock per subscriber, minimal contention between emit and drain.

For Transaction.ComponentChanged transactions, the bus filters by the component's Component.slotType() against the subscriber's registered types. All other transaction types (e.g., Transaction.EntityAdded) are delivered to all subscribers.

Thread safety: emit() and drain() are synchronized per-subscriber via the SubscriberState lock. This ensures the logic thread (emit) and render thread (drain/swap) never access the write buffer concurrently.

  • Constructor Details

    • TransactionBus

      public TransactionBus()
  • Method Details

    • subscribe

      @SafeVarargs public final void subscribe(Object subscriberKey, Class<? extends Component>... componentTypes)
      Registers a subscriber with interest in the given component types. ComponentChanged transactions are filtered by these types. EntityAdded/EntityRemoved and all other types are always delivered. If no component types are specified, all transactions are delivered.
    • unsubscribe

      public void unsubscribe(Object subscriberKey)
      Removes a subscriber, freeing its buffers.
    • emit

      public void emit(Transaction txn)
      Emits a transaction. For ComponentChanged, only subscribers interested in that component's slotType receive it (unless the subscriber has no filter). All other types go to all subscribers.
    • drain

      public List<Transaction> drain(Object subscriberKey)
      Drains all pending transactions for the given subscriber. Swaps the double buffer under a lock, returns the read buffer. The returned list is safe to iterate while the logic thread continues emitting.