An event, notifying its listeners each time it occurs, without data. For an event carrying data, see Signal.
An event is triggered with run(), notifying its listeners in the order they were added.
Events provide access to their Sensor via sensor(), which can be used to add listeners,
but can not be used to trigger the event.
Unhandled exceptions occurring in a listener will prevent further listeners from being notified.
Thread Safety: Listener management (add/remove) is thread-safe.
However, triggering via run() is NOT thread-safe
and should be performed from a single thread (such as an application UI thread).
Event event = Event.event();
event.addListener(this::doSomething);
event.run();
Sensor sensor = event.sensor();
sensor.addListener(this::doSomethingElse);
Listeners can be added using a WeakReference, so they must be strongly
referenced elsewhere, a lambda or method reference only referenced by the sensor being garbage collected right away.
// listener is a field
sensor.addWeakListener(listener);
Any weak references that no longer refer to a listener instance are cleared when listeners are added or removed.
- See Also:
-
Method Summary
Methods inherited from interface is.codion.common.reactive.sensor.Sensor
addListener, addWeakListener, removeListener, removeWeakListener
-
Method Details
-
run
void run()Triggers this event, notifying its listeners. -
sensor
Sensor sensor()Description copied from interface:SensorReturns the underlyingSensorto which listener operations are delegated.For
AbstractSensorandAbstractObserversubclasses this method returnsthis. For wrapper types, this returns the delegate sensor, and for anObserverimplemented viaObserver.observer(), its observer. -
event
Creates a newEvent.- Returns:
- a new Event
-