Class MappedWritableSignal<P,C>

java.lang.Object
com.vaadin.flow.signals.impl.MappedWritableSignal<P,C>
Type Parameters:
P - the parent signal value type
C - the child (this signal's) value type
All Implemented Interfaces:
Signal<C>, WritableSignal<C>, Serializable

public class MappedWritableSignal<P,C> extends Object implements WritableSignal<C>
A writable signal that provides a two-way mapped view of another writable signal. Reading the signal applies a getter function to extract a child value from the parent. Writing to the signal uses a setter function to update the parent signal with the new child value.

This enables patterns like mapping a single field from a record or bean to a separate writable signal that can be used with two-way bindings.

See Also:
  • Constructor Details

    • MappedWritableSignal

      public MappedWritableSignal(WritableSignal<P> parent, SignalMapper<P,C> getter, ValueMerger<P,C> merger)
      Creates a new mapped writable signal.
      Parameters:
      parent - the parent signal to map, not null
      getter - the function to extract the child value from the parent, not null
      merger - the function to create a new parent value given the current parent and new child value, not null
  • Method Details

    • get

      public C get()
      Description copied from interface: Signal
      Gets the current value of this signal. The value is read in a way that takes the current transaction into account and in the case of clustering also changes that have been submitted to the cluster but not yet confirmed.

      If the signal implementation supports transactions, then reading the value in a regular (i.e. Transaction.Type.STAGED) transaction makes the transaction depend on the value so that the transaction fails in case the signal value is changed concurrently.

      Reading the value inside an Signal.effect(EffectAction) or Signal.computed(SignalComputation) callback sets up that effect or computed signal to depend on the signal.

      Specified by:
      get in interface Signal<P>
      Returns:
      the signal value
    • peek

      public C peek()
      Description copied from interface: Signal
      Reads the value without setting up any dependencies. This method returns the same value as Signal.get() but without creating a dependency when used inside a transaction, effect or computed signal.
      Specified by:
      peek in interface Signal<P>
      Returns:
      the signal value
    • set

      public SignalOperation<C> set(C newChildValue)
      Description copied from interface: WritableSignal
      Sets the value of this signal. The result of the returned operation will be resolved with the previous value at the time when this operation was confirmed.
      Specified by:
      set in interface WritableSignal<P>
      Parameters:
      newChildValue - the value to set
      Returns:
      an operation containing the eventual result
    • replace

      public SignalOperation<Void> replace(C expectedValue, C newValue)
      Description copied from interface: WritableSignal
      Sets the value of this signal if and only if the signal has the expected value at the time when the operation is confirmed. This is the signal counterpart to AtomicReference.compareAndSet(Object, Object). The result of the returned operation will be resolved as successful if the expected value was present and resolved as unsuccessful if any other value was present when the operation is processed.
      Specified by:
      replace in interface WritableSignal<P>
      Parameters:
      expectedValue - the expected value
      newValue - the new value
      Returns:
      an operation containing the eventual result
    • update

      public CancelableOperation<C> update(SignalUpdater<C> childUpdater)
      Description copied from interface: WritableSignal
      Updates the signal value based on the given callback. The callback receives the current signal value and returns the new value to use. If the original value has changed by the time this change is confirmed, then the returned value is ignored and the callback is run again with the new value as input. This process is repeated until cancelled or until the update succeeds without conflicting changes.

      The process can be cancelled through the returned operation instance. Note that canceling will only prevent further retries but the change will still be made if the currently running attempt succeeds.

      The result of the returned operation will be resolved with the previous value at the time when a successful update operation was confirmed.

      Update operations cannot participate in transactions since any retry would occur after the original transaction has already been committed. For this reason, the whole operation completely bypasses all transaction handling.

      Specified by:
      update in interface WritableSignal<P>
      Parameters:
      childUpdater - the value update callback, not null
      Returns:
      an operation containing the eventual result