Package com.vaadin.signals.impl
Class ComputedSignal<T>
java.lang.Object
com.vaadin.signals.AbstractSignal<T>
com.vaadin.signals.impl.ComputedSignal<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Signal<T>
A signal with a value that is computed based on the value of other signals.
The signal value will be lazily re-computed when needed after the value has
changed for any of the signals that were used when computing the previous
value. If the computation callback throws a
RuntimeException, then
that exception will be re-thrown when accessing the value of this signal. An
effect or computed signal that uses the value
from a computed signal will not be invalidated if the computation is run
again but produces the same value as before.-
Field Summary
Fields inherited from class com.vaadin.signals.AbstractSignal
ANYTHING_GOES -
Constructor Summary
ConstructorsConstructorDescriptionComputedSignal(Supplier<T> computation) Creates a new computed signal with the provided compute callback. -
Method Summary
Modifier and TypeMethodDescriptionasNode()Converts this signal into a node signal.protected UsageTracker.UsagecreateUsage(Transaction transaction) Creates a usage instance based on the current state of this signal.protected TextractValue(Node.Data data) Extracts the value for this signal from the given signal data node.peek()Reads the value without setting up any dependencies.Reads the confirmed value without setting up any dependencies.protected ObjectusageChangeValue(Node.Data data) Gets a reference value that will be used to determine whether a dependency based on previous usage should be invalidated.Methods inherited from class com.vaadin.signals.AbstractSignal
clear, data, data, fromJson, id, mergeValidators, nodeValue, remove, submit, submit, submit, submitInsert, submitVoidOperation, toJson, tree, validator, value
-
Constructor Details
-
ComputedSignal
Creates a new computed signal with the provided compute callback.- Parameters:
computation- a callback that returns the computed value, not null
-
-
Method Details
-
createUsage
Creates a usage instance based on the current state of this signal.The usage instance from the super implementation is wrapped to count how many active external listeners there are. The external listener might become inactive either by returning
falseor through explicit unregistering, so both those sources are wrapped to accurately keep track of when the listener is no longer active.- Overrides:
createUsagein classAbstractSignal<T>- Parameters:
transaction- the transaction for which the usage occurs, notnull- Returns:
- a usage instance, not
null
-
extractValue
Description copied from class:AbstractSignalExtracts the value for this signal from the given signal data node.- Specified by:
extractValuein classAbstractSignal<T>- Parameters:
data- the data node to extract the value from, ornullif the node doesn't exist in the tree- Returns:
- the signal value
-
usageChangeValue
Description copied from class:AbstractSignalGets a reference value that will be used to determine whether a dependency based on previous usage should be invalidated. This is done by getting one reference value when the dependency occurs and then comparing that to the current value to determine if the value has changed.The implementation should return an object that changes if and only if the
AbstractSignal.value()of this signal changes.- Specified by:
usageChangeValuein classAbstractSignal<T>- Parameters:
data- the data node to read from, notnull- Returns:
- a reference value to use for validity checks, may be
null
-
peekConfirmed
Description copied from class:AbstractSignalReads the confirmed value without setting up any dependencies. The confirmed value doesn't consider changes in the current transaction or changes that have been submitted but not yet confirmed in a cluster.- Overrides:
peekConfirmedin classAbstractSignal<T>- Returns:
- the confirmed signal value
-
peek
Description copied from class:AbstractSignalReads the value without setting up any dependencies. This method returns the same value asAbstractSignal.value()but without creating a dependency when used inside a transaction, effect or computed signal.- Overrides:
peekin classAbstractSignal<T>- Returns:
- the signal value
-
asNode
Description copied from class:AbstractSignalConverts this signal into a node signal. This allows further conversion into any specific signal type through the methods inNodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.- Overrides:
asNodein classAbstractSignal<T>- Returns:
- this signal as a node signal, not
null
-