- Type Parameters:
T- the type of data propagated with this signal
A signal, notifying its listeners and consumers each time it occurs, carrying data. For an event without data,
see Event.
A signal is triggered with accept(Object), notifying its listeners and consumers in the order they were
added, the consumers receiving the data.
Signals provide access to their Observer via observer(), which can be used to add listeners
and consumers, but can not be used to trigger the signal.
Unhandled exceptions occurring in a listener or consumer will prevent further ones from being notified.
Thread Safety: Listener and consumer management (add/remove) is thread-safe.
However, triggering via accept(Object) is NOT thread-safe
and should be performed from a single thread (such as an application UI thread).
Signal<Boolean> signal = Signal.signal();
signal.addListener(this::doSomething);
signal.addConsumer(this::onBoolean);
signal.accept(true);
Observer<Boolean> observer = signal.observer();
observer.addConsumer(this::onBooleanElsewhere);
Listeners and consumers can be added using a WeakReference, so they must be strongly
referenced elsewhere, a lambda or method reference only referenced by the observer being garbage collected right away.
// listener and consumer are fields
observer.addWeakListener(listener);
observer.addWeakConsumer(consumer);
Any weak references that no longer refer to a listener or consumer instance are cleared when listeners or consumers are added or removed.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface is.codion.common.reactive.observer.Observer
Observer.Builder<T,B extends Observer.Builder<T, B>> -
Method Summary
Methods inherited from interface is.codion.common.reactive.observer.Observer
addConsumer, addWeakConsumer, removeConsumer, removeWeakConsumer, sensor, when, whenMethods inherited from interface is.codion.common.reactive.sensor.Sensor
addListener, addWeakListener, removeListener, removeWeakListener
-
Method Details
-
accept
Triggers this signal, notifying its listeners, and its consumers with the given data. -
observer
Description copied from interface:ObserverReturns the underlyingObserverto which listener and consumer operations are delegated.For
AbstractObserversubclasses this method returnsthis. For wrapper types, this returns the delegate observer. -
signal
Creates a newSignal.- Type Parameters:
T- the type of data propagated with the signal- Returns:
- a new Signal
-