Package com.vaadin.flow.signals.impl
Class ComputedSignal<T extends @Nullable Object>
java.lang.Object
com.vaadin.flow.signals.shared.AbstractSignal<T>
com.vaadin.flow.signals.impl.ComputedSignal<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Signal<T>,Serializable
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.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.signals.shared.AbstractSignal
AbstractSignal.ChildSignalFactory<I extends AbstractSignal<?>>, AbstractSignal.ResultConverter<T extends @Nullable Object> -
Field Summary
Fields inherited from class com.vaadin.flow.signals.shared.AbstractSignal
ANYTHING_GOES -
Constructor Summary
ConstructorsConstructorDescriptionComputedSignal(SignalComputation<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 @Nullable TextractValue(@Nullable Node.Data data) Extracts the value for this signal from the given signal data node.Reads the confirmed value without setting up any dependencies.protected @Nullable 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.flow.signals.shared.AbstractSignal
clear, data, data, fromJson, get, id, mergeValidators, nodeValue, peek, remove, submit, submit, submit, submitInsert, submitVoidOperation, toJson, tree, validator
-
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 extends @Nullable Object>- 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 extends @Nullable Object>- 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.get()of this signal changes.- Specified by:
usageChangeValuein classAbstractSignal<T extends @Nullable Object>- 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 extends @Nullable Object>- Returns:
- the confirmed 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 inSharedNodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.- Overrides:
asNodein classAbstractSignal<T extends @Nullable Object>- Returns:
- this signal as a node signal, not
null
-