com.vaadin.signals.impl.

Class ComputedSignal<T>

java.lang.Object
com.vaadin.signals.Signal<T>
com.vaadin.signals.impl.ComputedSignal<T>

Type Parameters:

T - the value type

public class ComputedSignal<T> extends 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. 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.

  • Constructor Details

    • ComputedSignal

      public ComputedSignal(Supplier<T> computation)

      Creates a new computed signal with the provided compute callback.

      Parameters:

      computation - a callback that returns the computed value, 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 Signal<T>

      Parameters:

      transaction - the transaction for which the usage occurs, not null

      Returns:

      a usage instance, not null

    • extractValue

      protected T extractValue(Node.Data data)

      Description copied from class: Signal

      Extracts the value for this signal from the given signal data node.

      Specified by:

      extractValue in class Signal<T>

      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 Object usageChangeValue(Node.Data data)

      Description copied from class: Signal

      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 Signal.value() of this signal changes.

      Specified by:

      usageChangeValue in class Signal<T>

      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: Signal

      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 Signal<T>

      Returns:

      the confirmed signal value

    • peek

      public T peek()

      Description copied from class: Signal

      Reads the value without setting up any dependencies. This method returns the same value as Signal.value() but without creating a dependency when used inside a transaction, effect or computed signal.

      Overrides:

      peek in class Signal<T>

      Returns:

      the signal value

    • asNode

      public NodeSignal asNode()

      Description copied from class: Signal

      Converts this signal into a node signal. This allows further conversion into any specific signal type through the methods in NodeSignal. The converted signal is backed by the same underlying data and uses the same validator as this signal.

      Overrides:

      asNode in class Signal<T>

      Returns:

      this signal as a node signal, not null