Skip to content

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.

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.

ReturnsSignatureSummary
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.

ReturnsSignatureSummary
voidbatch(Runnable runnable) staticRuns runnable and holds back the notifications of every signal it changes until it ends, so each listener runs once, with the final value.
voidsilent(Runnable runnable) staticRuns runnable without calling the onChange listeners of the signals it changes.
thissilent(Consumer action) fluentRuns action with this signal, without calling the onChange listeners of the signals it changes, then returns this signal.
ReturnsSignatureSummary
Tget()Returns the value and records the read, so the node property, the ComputedSignal or the expression that is being computed follows this signal.
Tpeek()Returns the value without recording the read: nothing follows the signal through this call.
booleanisPresent()If true, the value is not null.
booleanisWritable()If true, set(...) and reset() change the value; a ComputedSignal and a TweenAnimator return false.
StringtoString()Returns the simple name of the class and the value, as in IntegerSignal{3}.
ReturnsSignatureSummary
ComputedSignal<T>from(T value) staticWraps the value of an expression in a read-only ComputedSignal that follows the signals the expression read.
ComputedSignal<T>from(Supplier<? extends T> supplier) staticCreates a read-only ComputedSignal whose value is the result of supplier.
thisset(T value) fluentSets the value, then returns this signal.
thisreset() fluentSets the value back to the default value given to the constructor, then returns this signal.
thisremoveOnChange(SignalListener<? super T> listener) fluentRemoves 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.
ReturnsSignatureSummary
thisfireChange() fluentNotifies the listeners and the derived signals with the current value, without changing it, then returns this signal.
Stringrefuse(String action) hook protectedReturns the message of the exception that set(...) and reset() throw when the signal is read-only; action describes the refused write.
Tseal(T value) hook protectedReturns the value to store for each write; the signal stores value itself.
voidrefresh() hook protectedBrings the value up to date before a read; a plain signal does nothing.
voiddispatch() hook protectedRuns once per change when the notifications are delivered, and calls the listeners with the value.
intgetVersion() protectedReturns a counter that grows on each change; derived signals compare it to know whether they are outdated.
booleanisObserved() hook protectedIf true, a derived signal or a listener follows this signal.
voidaddObserver(ComputedSignal<?> observer) hook protectedRegisters a derived signal to mark as outdated when this signal changes.
voidremoveObserver(ComputedSignal<?> observer) hook protectedRemoves a derived signal registered by addObserver(...).
thiswrite(Runnable task) fluent protectedRuns task now on the render thread, or queues it for the render thread when it is called from another thread, then returns this signal.
Stransform(UnaryOperator operator) protectedWrites the result of operator applied to the value, then returns this signal.
booleanhasListeners() protectedIf true, at least one onChange listener is registered.
voidcopyListeners(Signal signal) protectedAdds the listeners of this signal to signal, for a subclass that copies itself, as TweenAnimator.copy does.
voidinvalidate(SignalContext context) protectedMarks every derived signal that follows this signal as outdated.
voidnextVersion() protectedIncreases the version counter without notifying anyone.
voidfireChange(T value) protectedCalls every listener with value; when a listener throws, the others still run and the first exception is thrown at the end.
voidfire() protectedIncreases the version, marks the derived signals as outdated and schedules the listeners, as a change does.

EventListenerSummary
onChangeSignalListener<? super T>Adds a listener that runs with the value after each change, on the render thread, then returns this 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.

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.

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.

boolean isPresent()

If true, the value is not null. The read is recorded like get().

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}.

<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.

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.

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.

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.

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.

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.

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. Every write of a subclass goes through it.

<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.

boolean hasListeners() protected

If true, at least one onChange listener is registered.

void copyListeners(Signal<T> signal) protected

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 fire() protected

Increases the version, marks the derived signals as outdated and schedules the listeners, as a change does.

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

Report a problem with this page