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.
Description
Section titled “Description”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.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
ComputedSignal<T> | constant(T value) factory protected | Returns a signal whose value is value forever: it reads no signal and never notifies. |
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. |
Methods
Section titled “Methods”Queries
Section titled “Queries”| Returns | Signature | Summary |
|---|---|---|
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. |
Actions
Section titled “Actions”| Returns | Signature | Summary |
|---|---|---|
this | removeOnChange(SignalListener<? super T> listener) fluent | Removes 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. |
For subclasses
Section titled “For subclasses”| Returns | Signature | Summary |
|---|---|---|
String | refuse(String action) hook protected | Returns the message of the exception that set(...) and reset() throw. |
void | refresh() hook protected | Computes the value again when a signal it read changed since the last computation. |
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. |
Events
Section titled “Events”| Event | Listener | Summary |
|---|---|---|
| onChange | SignalListener<? super T> | Adds a listener that runs with the result after each change of the result, on the render thread, then returns this signal. |
Creation details
Section titled “Creation details”constant
Section titled “constant”<T> ComputedSignal<T> constant(T value) factory protected
Returns a signal whose value is value forever: it reads no signal and never notifies.
computed
Section titled “computed”<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.
Method details
Section titled “Method details”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.
isWritable
Section titled “isWritable”boolean isWritable()
Returns false: the value comes from the function.
isConstant
Section titled “isConstant”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.
removeOnChange
Section titled “removeOnChange”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.
refuse
Section titled “refuse”String refuse(String action) hook protected
Returns the message of the exception that set(...) and reset() throw.
refresh
Section titled “refresh”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.
dispatch
Section titled “dispatch”void dispatch() hook protected
Notifies the listeners when the result changed since the last notification.
addObserver
Section titled “addObserver”void addObserver(ComputedSignal<?> observer) hook protected
Registers a derived signal and starts following the signals this one reads.
removeObserver
Section titled “removeObserver”void removeObserver(ComputedSignal<?> observer) hook protected
Removes a derived signal and stops following its signals when nothing else follows this one.
markStale
Section titled “markStale”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.
depend
Section titled “depend”void depend(Signal<?> signal) hook protected
Records signal as read by the computation in progress.
Event details
Section titled “Event details”onChange
Section titled “onChange”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
