Package io.v47.events

Class DefaultEventEmitter

  • All Implemented Interfaces:
    io.v47.events.EventEmitter

    
    public class DefaultEventEmitter
     implements EventEmitter
                        

    This is the default implementation of an event emitter which can be extended or delegated to.

    It provides the option to fail fast which means that exceptions thrown by event listeners aren't logged, but rethrown immediately and further listeners are not called.

    Listeners are called concurrently, so the order of execution doesn't necessarily match the order of registration.

    Since:

    Events 2.0

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      Boolean hasListeners(EventKey<?> key) Checks whether there are listeners (both persistent and once) for the specified event key.
      <T extends Any> Unit emit(EventKey<T> key, T payload) Emits the event identified by the event key with the specified payload
      <T extends Any> Unit emit(EventKey<T> key, Function0<T> payloadBuilder) Emits the event identified by the event with the payload created by the specified builder.
      <T extends Any> Unit on(EventKey<T> key, SuspendFunction1<T, Unit> block) Registers a listener for the specified event key that is called everytime
      <T extends Any> Unit once(EventKey<T> key, SuspendFunction1<T, Unit> block) Registers a listener for the specified event key that is called only once and then discarded.
      Unit clear(EventKey<?> key) Discards all listenerl for the specified event key.
      <T extends Any> Unit remove(SuspendFunction1<T, Unit> listener) Removes the specified listener
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DefaultEventEmitter

        DefaultEventEmitter(Boolean failFast)
        Parameters:
        failFast - Indicates whether to rethrow exceptions caused by event handlers
    • Method Detail

      • hasListeners

         Boolean hasListeners(EventKey<?> key)

        Checks whether there are listeners (both persistent and once) for the specified event key.

        Parameters:
        key - The key uniquely identifying an event
        Since:

        Events 2.1

      • emit

         <T extends Any> Unit emit(EventKey<T> key, T payload)

        Emits the event identified by the event key with the specified payload

        Parameters:
        key - The key uniquely identifying the event
        payload - Data that is passed to the listeners
        Since:

        Events 1.0

      • emit

         <T extends Any> Unit emit(EventKey<T> key, Function0<T> payloadBuilder)

        Emits the event identified by the event with the payload created by the specified builder.

        The emitter may choose to not even call the builder if there are no listeners for the specified event key.

        This is useful when the payload is rather expensive to build, and it's not certain there will be a listener for that particular event key

        Parameters:
        key - The key uniquely identifying the event
        payloadBuilder - The function building the actual payload
        Since:

        Events 2.1

      • on

         <T extends Any> Unit on(EventKey<T> key, SuspendFunction1<T, Unit> block)

        Registers a listener for the specified event key that is called everytime

        Parameters:
        key - The event key
        block - The listener for the event's payload
        Since:

        Events 1.0

      • once

         <T extends Any> Unit once(EventKey<T> key, SuspendFunction1<T, Unit> block)

        Registers a listener for the specified event key that is called only once and then discarded.

        Parameters:
        key - The event key
        block - The listener for the event's payload
        Since:

        Events 1.0

      • clear

         Unit clear(EventKey<?> key)

        Discards all listenerl for the specified event key.

        If not key is specified all listeners are discarded.

        Parameters:
        key - The event key
        Since:

        Events 1.0