Class ComponentEffect

java.lang.Object
com.vaadin.flow.component.ComponentEffect

public final class ComponentEffect extends Object
The utility class that provides helper methods for using Signal effects in a context of a given component's life-cycle.

It ultimately creates a Signal effect, i.e. a call to Signal.effect(Runnable), that is automatically enabled when a component is attached and disabled when the component is detached. Additionally it provides methods to bind signals to component according to a given value setting function.

Since:
24.8
  • Method Details

    • effect

      public static <C extends Component> Registration effect(C owner, Runnable effectFunction)
      Creates a Signal effect that is owned by a given component. The effect is enabled when the component is attached and automatically disabled when it is detached.

      Example of usage:

       Registration effect = ComponentEffect.effect(myComponent, () -> {
           Notification.show("Component is attached and signal value is "
                   + someSignal.value());
       });
       effect.remove(); // to remove the effect when no longer needed
       
      Type Parameters:
      C - the type of the component
      Parameters:
      owner - the owner component for which the effect is applied, must not be null
      effectFunction - the effect function to be executed when any dependency is changed, must not be null
      Returns:
      a Registration that can be used to remove the effect function
      See Also:
      • Signal.effect(Runnable)
    • bind

      public static <C extends Component, T> Registration bind(C owner, com.vaadin.signals.Signal<T> signal, SerializableBiConsumer<C,T> setter)
      Binds a signal's value to a given owner component in a way defined in setter function and creates a Signal effect function executing the setter whenever the signal value changes.

      Example of usage:

       Registration effect = ComponentEffect.bind(mySpan, stringSignal,
               Span::setText);
       effect.remove(); // to remove the effect when no longer needed
      
       ComponentEffect.bind(mySpan, stringSignal.map(value -> !value.isEmpty()),
               Span::setVisible);
       
      Type Parameters:
      C - the type of the component
      T - the type of the signal value
      Parameters:
      owner - the owner component for which the effect is applied, must not be null
      signal - the signal whose value is to be bound to the component, must not be null
      setter - the setter function that defines how the signal value is applied to the component, must not be null
      Returns:
      a Registration that can be used to remove the effect function
      See Also:
      • Signal.effect(Runnable)
    • bindChildren

      public static <T, PARENT extends Component & HasComponents> void bindChildren(PARENT parent, com.vaadin.signals.ListSignal<T> list, SerializableFunction<com.vaadin.signals.ValueSignal<T>,Component> childFactory)
      Binds a ListSignal to a parent component using a child component factory. Each ValueSignal in the list corresponds to a child component within the parent.

      The parent component is automatically updated to reflect the structure of the ListSignal. Changes to the list, such as additions, removals, or reordering, will update the parent's children accordingly.

      The parent component must not contain any children that are not part of the ListSignal. If the parent has existing children when this method is called, or if it contains unrelated children after the list changes, an IllegalStateException will be thrown.

      New child components are created using the provided childFactory function. This function takes a ValueSignal from the ListSignal and returns a corresponding Component. It shouldn't return null. The ValueSignal can be further bound to the returned component as needed. Note that childFactory is run inside a Effect, and therefore AbstractSignal.value() calls makes effect re-run automatically on signal value change.

      Example of usage:

       ListSignal<String> taskList = new ListSignal<>(String.class);
      
       UnorderedList component = new UnorderedList();
      
       ComponentEffect.bindChildren(component, taskList, taskValueSignal -> {
           var listItem = new ListItem();
           ComponentEffect.bind(listItem, taskValueSignal, HasString::setText);
           return listItem;
       });
       
      Type Parameters:
      T - the value type of the ValueSignals in the ListSignal
      PARENT - the type of the parent component
      Parameters:
      parent - target parent component, must not be null
      list - list signal to bind to the parent, must not be null
      childFactory - factory to create new component, must not be null
      Throws:
      IllegalStateException - thrown if parent component isn't empty