Class SignalBus

java.lang.Object
me.tamkungz.signals.SignalBus

public final class SignalBus extends Object
Minecraft-friendly facade over GEB.

Typed signals live here; custom GEB events, @Listen listeners and sub-buses are reached through geb().

  • Constructor Details

    • SignalBus

      public SignalBus()
      Creates a bus on top of a new GEB instance.
    • SignalBus

      public SignalBus(foo.zaaarf.geb.api.IBus gebBus)
      Creates a bus on top of an existing GEB bus.

      The bus registers the dispatcher it needs for its own events on the given GEB bus. Dispatchers for your own events are not touched; see GebBridge.loadDispatchers(ClassLoader).

      Every value fired on a bus travels through GEB as an event, so two SignalBus instances on the same GEB bus see each other's values, just like a bus and its sub-buses do. Give each bus its own GEB bus (the default) unless that is what you want.

      Parameters:
      gebBus - GEB bus to use
  • Method Details

    • create

      public static SignalBus create()
      Creates a bus on top of a new GEB instance.
      Returns:
      a new bus
    • geb

      public GebBridge geb()
      Gives direct access to GEB: custom events, @Listen listeners and sub-buses.
      Returns:
      the GEB bridge of this bus
    • signal

      public <T> Signal<T> signal(Class<T> eventType)
      Returns the signal on the default channel of a payload type.
      Type Parameters:
      T - payload type
      Parameters:
      eventType - payload type
      Returns:
      the signal for SignalKey.of(eventType)
    • signal

      public <T> Signal<T> signal(Class<T> eventType, String name)
      Returns the signal on a named channel of a payload type.
      Type Parameters:
      T - payload type
      Parameters:
      eventType - payload type
      name - channel name
      Returns:
      the signal for SignalKey.named(eventType, name)
    • signal

      public <T> Signal<T> signal(SignalKey<T> key)
      Returns the signal on a channel.

      Signals are cheap views: every Signal for the same key shares the same listeners.

      Type Parameters:
      T - payload type
      Parameters:
      key - channel key
      Returns:
      the signal for the key
    • connect

      public <T> Connection connect(Class<T> eventType, Consumer<T> listener)
      Connects a listener to the default channel of a payload type.
      Type Parameters:
      T - payload type
      Parameters:
      eventType - payload type
      listener - listener to call
      Returns:
      the connection for this listener
    • connect

      public <T> Connection connect(SignalKey<T> key, Consumer<T> listener)
      Connects a listener to a channel.
      Type Parameters:
      T - payload type
      Parameters:
      key - channel key
      listener - listener to call
      Returns:
      the connection for this listener
    • connect

      public <T> Connection connect(ConnectionGroup group, Class<T> eventType, Consumer<T> listener)
      Connects a listener to the default channel of a payload type and tracks it in a group.
      Type Parameters:
      T - payload type
      Parameters:
      group - group that will disconnect the listener
      eventType - payload type
      listener - listener to call
      Returns:
      the connection for this listener
    • connect

      public <T> Connection connect(ConnectionGroup group, SignalKey<T> key, Consumer<T> listener)
      Connects a listener to a channel and tracks it in a group.
      Type Parameters:
      T - payload type
      Parameters:
      group - group that will disconnect the listener
      key - channel key
      listener - listener to call
      Returns:
      the connection for this listener
    • once

      public <T> Connection once(Class<T> eventType, Consumer<T> listener)
      Connects a listener to the default channel of a payload type that is disconnected after its first value.
      Type Parameters:
      T - payload type
      Parameters:
      eventType - payload type
      listener - listener to call at most once
      Returns:
      the connection for this listener
    • once

      public <T> Connection once(SignalKey<T> key, Consumer<T> listener)
      Connects a listener to a channel that is disconnected after its first value.
      Type Parameters:
      T - payload type
      Parameters:
      key - channel key
      listener - listener to call at most once
      Returns:
      the connection for this listener
    • once

      public <T> Connection once(ConnectionGroup group, Class<T> eventType, Consumer<T> listener)
      Connects a one-shot listener to the default channel of a payload type and tracks it in a group.
      Type Parameters:
      T - payload type
      Parameters:
      group - group that will disconnect the listener
      eventType - payload type
      listener - listener to call at most once
      Returns:
      the connection for this listener
    • once

      public <T> Connection once(ConnectionGroup group, SignalKey<T> key, Consumer<T> listener)
      Connects a one-shot listener to a channel and tracks it in a group.
      Type Parameters:
      T - payload type
      Parameters:
      group - group that will disconnect the listener
      key - channel key
      listener - listener to call at most once
      Returns:
      the connection for this listener
    • fire

      public <T> void fire(Class<T> eventType, T value)
      Fires a value on the default channel of a payload type.
      Type Parameters:
      T - payload type
      Parameters:
      eventType - payload type
      value - value to deliver
    • fire

      public <T> void fire(SignalKey<T> key, T value)
      Fires a value on a channel.

      Listeners run on the calling thread, in the order they were connected. A throwing listener does not stop the others; once all have run, the first exception is rethrown with the rest attached as suppressed exceptions. GEB does not catch it, so it reaches the caller of fire.

      Type Parameters:
      T - payload type
      Parameters:
      key - channel key
      value - value to deliver
    • listenerCount

      public int listenerCount(Class<?> eventType)
      Counts the listeners connected to the default channel of a payload type.
      Parameters:
      eventType - payload type
      Returns:
      number of connected listeners
    • listenerCount

      public int listenerCount(SignalKey<?> key)
      Counts the listeners connected to a channel.
      Parameters:
      key - channel key
      Returns:
      number of connected listeners
    • hasReceivers

      public boolean hasReceivers(Class<?> eventType)
      Tells whether a value fired on the default channel of a payload type could reach anything.
      Parameters:
      eventType - payload type
      Returns:
      see hasReceivers(SignalKey)
    • hasReceivers

      public boolean hasReceivers(SignalKey<?> key)
      Tells whether a value fired on a channel could reach anything: a listener connected to it, or a sub-bus registered through geb(). A sub-bus cannot be inspected, so one counts as a possible receiver for every channel.

      Platform bridges use this to skip building and dispatching an event nobody can receive. Prefer it to comparing listenerCount(SignalKey) with zero, which ignores sub-buses.

      Parameters:
      key - channel key
      Returns:
      false only if firing on the channel could not reach any listener
    • clear

      public void clear(Class<?> eventType)
      Disconnects every listener of the default channel of a payload type.
      Parameters:
      eventType - payload type
    • clear

      public void clear(SignalKey<?> key)
      Disconnects every listener of a channel.
      Parameters:
      key - channel key
    • clear

      public void clear()
      Disconnects every listener of every channel on this bus.