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
-
-
Constructor Summary
Constructors Constructor Description DefaultEventEmitter(Boolean failFast)
-
Method Summary
Modifier and Type Method Description BooleanhasListeners(EventKey<?> key)Checks whether there are listeners (both persistent and once) for the specified event key. <T extends Any> Unitemit(EventKey<T> key, T payload)Emits the event identified by the event key with the specified payload <T extends Any> Unitemit(EventKey<T> key, Function0<T> payloadBuilder)Emits the event identified by the event with the payload created by the specified builder. <T extends Any> Uniton(EventKey<T> key, SuspendFunction1<T, Unit> block)Registers a listener for the specified event key that is called everytime <T extends Any> Unitonce(EventKey<T> key, SuspendFunction1<T, Unit> block)Registers a listener for the specified event key that is called only once and then discarded. Unitclear(EventKey<?> key)Discards all listenerl for the specified event key. <T extends Any> Unitremove(SuspendFunction1<T, Unit> listener)Removes the specified listener -
-
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 eventpayload- 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 eventpayloadBuilder- 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 keyblock- 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 keyblock- 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
-
-
-
-