Class CachedSignal<T extends @Nullable Object>

java.lang.Object
com.vaadin.flow.signals.shared.AbstractSharedSignal<T>
com.vaadin.flow.signals.impl.CachedSignal<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
Signal<T>, Serializable

public class CachedSignal<T extends @Nullable Object> extends AbstractSharedSignal<T>
A signal that caches the value of an inner signal. The inner signal is typically a computed signal performing an expensive computation that should be run again only if any of its dependencies has changed.

If a RuntimeException is thrown when getting the value of the inner signal, then that exception will be re-thrown when accessing the value of this signal. An effect or outer cached signal that uses the value from a cached signal will not be invalidated if the computation is run again but produces the same value as before.

See Also:
  • Constructor Details

    • CachedSignal

      public CachedSignal(Signal<T> inner)
      Creates a new cached signal with the provided inner signal.
      Parameters:
      inner - a signal to wrap, not null
  • Method Details

    • createUsage

      protected UsageTracker.Usage createUsage(Transaction transaction)
      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 false or through explicit unregistering, so both those sources are wrapped to accurately keep track of when the listener is no longer active.

      Overrides:
      createUsage in class AbstractSharedSignal<T extends @Nullable Object>
      Parameters:
      transaction - the transaction for which the usage occurs, not null
      Returns:
      a usage instance, not null
    • extractValue

      protected @Nullable T extractValue(@Nullable Node.Data data)
      Description copied from class: AbstractSharedSignal
      Extracts the value for this signal from the given signal data node.
      Specified by:
      extractValue in class AbstractSharedSignal<T extends @Nullable Object>
      Parameters:
      data - the data node to extract the value from, or null if the node doesn't exist in the tree
      Returns:
      the signal value
    • usageChangeValue

      protected @Nullable Object usageChangeValue(Node.Data data)
      Description copied from class: AbstractSharedSignal
      Gets 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 AbstractSharedSignal.get() of this signal changes.

      Specified by:
      usageChangeValue in class AbstractSharedSignal<T extends @Nullable Object>
      Parameters:
      data - the data node to read from, not null
      Returns:
      a reference value to use for validity checks, may be null
    • peekConfirmed

      public T peekConfirmed()
      Description copied from class: AbstractSharedSignal
      Reads 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:
      peekConfirmed in class AbstractSharedSignal<T extends @Nullable Object>
      Returns:
      the confirmed signal value
    • asNode

      public SharedNodeSignal asNode()
      Description copied from class: AbstractSharedSignal
      Converts this signal into a node signal. This allows further conversion into any specific signal type through the methods in SharedNodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.
      Overrides:
      asNode in class AbstractSharedSignal<T extends @Nullable Object>
      Returns:
      this signal as a node signal, not null