Signal
class Signal<T>
Implements: Supplier<T>
Known subtypes: TweenAnimator, ComputedSignal, ListSignal, MapSignal, SetSignal, BooleanSignal, DoubleSignal, FloatSignal, IntegerSignal, LongSignal, StringSignal
Package: dev.joid.lib.signal · Module: core · Source: Signal.java
Holds a value that nodes, derived signals and listeners follow, so the interface shows each change without rebuilding.
Description
Section titled “Description”A Signal<T> holds one value that changes while a UI is open: a count, a name, a list, a setting.
A node property set from an expression that reads the signal with get() follows it: JOID computes the expression again after each change and the node shows the result.
Use a signal for every value that the interface must show while it changes; keep plain fields for values that never change while the UI is open.
Hold a value that changes shows a counter that follows its clicks.
The typed signals add operations for their type: IntegerSignal, LongSignal, FloatSignal, DoubleSignal, BooleanSignal, StringSignal, ListSignal, SetSignal and MapSignal.
map(...) and Signal.from(...) give a read-only ComputedSignal computed from other signals.
A write made on the render thread applies at once; a write made on another thread applies on the render thread, at the next frame. Listeners always run on the render thread.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
| Signal() | Creates a signal whose value and default value are null. | |
| Signal(T value) | Creates a signal whose value and default value are value; reset() comes back to this value. |
Methods
Section titled “Methods”Reactivity
Section titled “Reactivity”| Returns | Signature | Summary |
|---|---|---|
void | batch(Runnable runnable) static | Runs runnable and holds back the notifications of every signal it changes until it ends, so each listener runs once, with the final value. |
void | silent(Runnable runnable) static | Runs runnable without calling the onChange listeners of the signals it changes. |
this | silent(Consumer | Runs action with this signal, without calling the onChange listeners of the signals it changes, then returns this signal. |
Queries
Section titled “Queries”| Returns | Signature | Summary |
|---|---|---|
T | get() | Returns the value and records the read, so the node property, the ComputedSignal or the expression that is being computed follows this signal. |
T | peek() | Returns the value without recording the read: nothing follows the signal through this call. |
boolean | isPresent() | If true, the value is not null. |
boolean | isWritable() | If true, set(...) and reset() change the value; a ComputedSignal and a TweenAnimator return false. |
String | toString() | Returns the simple name of the class and the value, as in IntegerSignal{3}. |
Actions
Section titled “Actions”| Returns | Signature | Summary |
|---|---|---|
ComputedSignal<T> | from(T value) static | Wraps the value of an expression in a read-only ComputedSignal that follows the signals the expression read. |
ComputedSignal<T> | from(Supplier<? extends T> supplier) static | Creates a read-only ComputedSignal whose value is the result of supplier. |
this | set(T value) fluent | Sets the value, then returns this signal. |
this | reset() fluent | Sets the value back to the default value given to the constructor, then returns this signal. |
this | removeOnChange(SignalListener<? super T> listener) fluent | Removes a listener given to onChange(...), then returns this signal. |
ComputedSignal<R> | map(Function<? super T,? extends R> function) | Returns a read-only ComputedSignal whose value is function applied to the value of this signal. |
For subclasses
Section titled “For subclasses”| Returns | Signature | Summary |
|---|---|---|
this | fireChange() fluent | Notifies the listeners and the derived signals with the current value, without changing it, then returns this signal. |
String | refuse(String action) hook protected | Returns the message of the exception that set(...) and reset() throw when the signal is read-only; action describes the refused write. |
T | seal(T value) hook protected | Returns the value to store for each write; the signal stores value itself. |
void | refresh() hook protected | Brings the value up to date before a read; a plain signal does nothing. |
void | dispatch() hook protected | Runs once per change when the notifications are delivered, and calls the listeners with the value. |
int | getVersion() protected | Returns a counter that grows on each change; derived signals compare it to know whether they are outdated. |
boolean | isObserved() hook protected | If true, a derived signal or a listener follows this signal. |
void | addObserver(ComputedSignal<?> observer) hook protected | Registers a derived signal to mark as outdated when this signal changes. |
void | removeObserver(ComputedSignal<?> observer) hook protected | Removes a derived signal registered by addObserver(...). |
this | write(Runnable task) fluent protected | Runs task now on the render thread, or queues it for the render thread when it is called from another thread, then returns this signal. |
S | transform(UnaryOperator | Writes the result of operator applied to the value, then returns this signal. |
boolean | hasListeners() protected | If true, at least one onChange listener is registered. |
void | copyListeners(Signal | Adds the listeners of this signal to signal, for a subclass that copies itself, as TweenAnimator.copy does. |
void | invalidate(SignalContext context) protected | Marks every derived signal that follows this signal as outdated. |
void | nextVersion() protected | Increases the version counter without notifying anyone. |
void | fireChange(T value) protected | Calls every listener with value; when a listener throws, the others still run and the first exception is thrown at the end. |
void | fire() protected | Increases the version, marks the derived signals as outdated and schedules the listeners, as a change does. |
Events
Section titled “Events”| Event | Listener | Summary |
|---|---|---|
| onChange | SignalListener<? super T> | Adds a listener that runs with the value after each change, on the render thread, then returns this signal. |
Creation details
Section titled “Creation details”Signal
Section titled “Signal”Signal()
Creates a signal whose value and default value are null.
Signal(T value)
Creates a signal whose value and default value are value; reset() comes back to this value.
Method details
Section titled “Method details”void batch(Runnable runnable) static
Runs runnable and holds back the notifications of every signal it changes until it ends, so each listener runs once, with the final value.
Batches nest: the notifications run when the outermost batch ends.
Writes made from another thread inside a batch reach the render thread together.
silent
Section titled “silent”void silent(Runnable runnable) static
Runs runnable without calling the onChange listeners of the signals it changes.
The values still change, and the derived signals compute the new result when they are read.
this silent(Consumer<S> action) fluent
Runs action with this signal, without calling the onChange listeners of the signals it changes, then returns this signal.
It is the fluent form of Signal.silent(Runnable).
T get()
Returns the value and records the read, so the node property, the ComputedSignal or the expression that is being computed follows this signal.
T peek()
Returns the value without recording the read: nothing follows the signal through this call. Use it in listeners and in code that must not depend on the signal.
isPresent
Section titled “isPresent”boolean isPresent()
If true, the value is not null.
The read is recorded like get().
isWritable
Section titled “isWritable”boolean isWritable()
If true, set(...) and reset() change the value; a ComputedSignal and a TweenAnimator return false.
toString
Section titled “toString”String toString()
Returns the simple name of the class and the value, as in IntegerSignal{3}.
<T> ComputedSignal<T> from(T value) static
Wraps the value of an expression in a read-only ComputedSignal that follows the signals the expression read.
When the expression read signals with get() right before this call, JOID reads the bytecode of the calling line and computes the expression again after each change of those signals.
When it read none, the result is a constant signal. When JOID cannot follow it, the result is constant too, and dev mode prints a warning that names the line and the fix. Signals lists the expressions that JOID follows.
<T> ComputedSignal<T> from(Supplier<? extends T> supplier) static
Creates a read-only ComputedSignal whose value is the result of supplier.
JOID records the signals that the supplier reads with get() and calls it again only after one of them changed.
In dev mode, a supplier that reads no signal prints a warning, since its value never changes.
this set(T value) fluent
Throws:
IllegalStateException: …
Sets the value, then returns this signal.
A value equal to the current one (by equals) changes nothing and notifies no one.
null is a value like any other: the listeners receive null and isPresent() returns false.
Called from another thread, the write applies on the render thread at the next frame.
Throws an IllegalStateException when the signal is read-only (isWritable() is false).
this reset() fluent
Throws:
IllegalStateException: …
Sets the value back to the default value given to the constructor, then returns this signal.
Throws an IllegalStateException when the signal is read-only.
removeOnChange
Section titled “removeOnChange”this removeOnChange(SignalListener<? super T> listener) fluent
Removes a listener given to onChange(...), then returns this signal.
<R> ComputedSignal<R> map(Function<? super T,? extends R> function)
Returns a read-only ComputedSignal whose value is function applied to the value of this signal.
The function runs again only after this signal changed, and only when the result is read.
fireChange
Section titled “fireChange”this fireChange() fluent
Notifies the listeners and the derived signals with the current value, without changing it, then returns this signal. Use it after changing the inside of a mutable value that the signal holds.
void fireChange(T value) protected
Calls every listener with value; when a listener throws, the others still run and the first exception is thrown at the end.
For subclasses: it does not touch the value nor the derived signals.
refuse
Section titled “refuse”String refuse(String action) hook protected
Returns the message of the exception that set(...) and reset() throw when the signal is read-only; action describes the refused write.
Override it to explain how to change the value of a read-only subclass.
T seal(T value) hook protected
Returns the value to store for each write; the signal stores value itself.
The collection signals override it to store a read-only copy.
refresh
Section titled “refresh”void refresh() hook protected
Brings the value up to date before a read; a plain signal does nothing.
ComputedSignal overrides it to compute its value again when a signal it read changed.
dispatch
Section titled “dispatch”void dispatch() hook protected
Runs once per change when the notifications are delivered, and calls the listeners with the value. Override it to filter or delay what the listeners receive.
getVersion
Section titled “getVersion”int getVersion() protected
Returns a counter that grows on each change; derived signals compare it to know whether they are outdated.
isObserved
Section titled “isObserved”boolean isObserved() hook protected
If true, a derived signal or a listener follows this signal.
addObserver
Section titled “addObserver”void addObserver(ComputedSignal<?> observer) hook protected
Registers a derived signal to mark as outdated when this signal changes.
removeObserver
Section titled “removeObserver”void removeObserver(ComputedSignal<?> observer) hook protected
Removes a derived signal registered by addObserver(...).
this write(Runnable task) fluent protected
Runs task now on the render thread, or queues it for the render thread when it is called from another thread, then returns this signal.
Every write of a subclass goes through it.
transform
Section titled “transform”<S extends Signal<T>> S transform(UnaryOperator<T> operator) protected
Writes the result of operator applied to the value, then returns this signal.
The operator reads the value when the write runs, so increments queued from another thread add up instead of overwriting each other.
hasListeners
Section titled “hasListeners”boolean hasListeners() protected
If true, at least one onChange listener is registered.
copyListeners
Section titled “copyListeners”void copyListeners(Signal<T> signal) protected
Adds the listeners of this signal to signal, for a subclass that copies itself, as TweenAnimator.copy does.
invalidate
Section titled “invalidate”void invalidate(SignalContext context) protected
Marks every derived signal that follows this signal as outdated.
nextVersion
Section titled “nextVersion”void nextVersion() protected
Increases the version counter without notifying anyone.
void fire() protected
Increases the version, marks the derived signals as outdated and schedules the listeners, as a change does.
Event details
Section titled “Event details”onChange
Section titled “onChange”this onChange(SignalListener<? super T> listener) fluent
Adds a listener that runs with the value after each change, on the render thread, then returns this signal.
Inside a batch, the listener runs once when the batch ends; inside silent(...), it does not run.
Adding the same listener twice keeps one; when a listener throws, the others still run and the first exception is thrown afterwards.
fluent returns the instance, typed for chaining · factory creates an instance · signal also accepts a value that follows signals, as a signal, an expression or a lambda (When does JOID compute this value again?) · hook called by JOID, override it · abstract you implement it · default interface method with a body · static called on the class · protected visible to subclasses
