Skip to content

ComputedSignal

class ComputedSignal<T>
Extends: Signal<T>
Package: dev.joid.lib.signal · Module: core · Source: ComputedSignal.java

Read-only signal whose value comes from a function of other signals, computed again only after one of them changed.

A ComputedSignal<T> comes from Signal.from(...) or map(...). It computes its value on the first read, records the signals that the function reads with get(), and computes again only when one of them changed. While nothing follows it, it computes on demand; once a listener or another derived signal follows it, it follows its own signals and notifies its listeners when its result changes (by equals). set(...) and reset() throw an IllegalStateException: set the signals it reads instead.

ReturnsSignatureSummary
ComputedSignal<T>constant(T value) factory protectedReturns a signal whose value is value forever: it reads no signal and never notifies.
ComputedSignal<T>computed(Supplier<? extends T> supplier) factory protectedReturns a signal computed by supplier, for the signals of JOID; create yours with Signal.from.

ReturnsSignatureSummary
Tget()Returns the value, computed first when a signal it read changed, and records the read like Signal.get.
Tpeek()Returns the value, computed first when needed, without recording the read.
booleanisWritable()Returns false: the value comes from the function.
booleanisConstant()If true, the value never changes: the signal comes from Signal.from(value) with an expression that read no signal.
ReturnsSignatureSummary
thisremoveOnChange(SignalListener<? super T> listener) fluentRemoves a listener, then returns this signal; without listeners nor followers, it stops following its signals.
ComputedSignal<R>map(Function<? super T,? extends R> function)Returns a signal computed by function from this one; on a constant signal, the result is a constant too.
ReturnsSignatureSummary
Stringrefuse(String action) hook protectedReturns the message of the exception that set(...) and reset() throw.
voidrefresh() hook protectedComputes the value again when a signal it read changed since the last computation.
voiddispatch() hook protectedNotifies the listeners when the result changed since the last notification.
voidaddObserver(ComputedSignal<?> observer) hook protectedRegisters a derived signal and starts following the signals this one reads.
voidremoveObserver(ComputedSignal<?> observer) hook protectedRemoves a derived signal and stops following its signals when nothing else follows this one.
voidmarkStale(SignalContext context) hook protectedMarks the value as outdated after a change of a signal it read, schedules its listeners and passes the mark to its own followers.
voiddepend(Signal<?> signal) hook protectedRecords signal as read by the computation in progress.
Inherited from Signal (7)

batch · silent · isPresent · toString · from · set · reset

EventListenerSummary
onChangeSignalListener<? super T>Adds a listener that runs with the result after each change of the result, on the render thread, then returns this signal.

<T> ComputedSignal<T> constant(T value) factory protected

Returns a signal whose value is value forever: it reads no signal and never notifies.

<T> ComputedSignal<T> computed(Supplier<? extends T> supplier) factory protected

Returns a signal computed by supplier, for the signals of JOID; create yours with Signal.from.

T get()

Returns the value, computed first when a signal it read changed, and records the read like Signal.get.

T peek()

Returns the value, computed first when needed, without recording the read.

boolean isWritable()

Returns false: the value comes from the function.

boolean isConstant()

If true, the value never changes: the signal comes from Signal.from(value) with an expression that read no signal. A node keeps a constant value as a plain value and does not follow it.

this removeOnChange(SignalListener<? super T> listener) fluent

Removes a listener, then returns this signal; without listeners nor followers, it stops following its signals.

<R> ComputedSignal<R> map(Function<? super T,? extends R> function)

Returns a signal computed by function from this one; on a constant signal, the result is a constant too.

String refuse(String action) hook protected

Returns the message of the exception that set(...) and reset() throw.

void refresh() hook protected

Throws:

  • IllegalStateException: A ComputedSignal cannot read itself while it computes its value, remove the read of this signal from its function

Computes the value again when a signal it read changed since the last computation. Throws an IllegalStateException when the function reads the signal that it computes. When the function throws, the exception goes to the caller and the next read computes again.

void dispatch() hook protected

Notifies the listeners when the result changed since the last notification.

void addObserver(ComputedSignal<?> observer) hook protected

Registers a derived signal and starts following the signals this one reads.

void removeObserver(ComputedSignal<?> observer) hook protected

Removes a derived signal and stops following its signals when nothing else follows this one.

void markStale(SignalContext context) hook protected

Marks the value as outdated after a change of a signal it read, schedules its listeners and passes the mark to its own followers.

void depend(Signal<?> signal) hook protected

Records signal as read by the computation in progress.

this onChange(SignalListener<? super T> listener) fluent

Adds a listener that runs with the result after each change of the result, on the render thread, then returns this signal. From the first listener on, the signal follows the signals it reads instead of computing on demand.


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