Class ComponentEffect

java.lang.Object
com.vaadin.flow.component.ComponentEffect
All Implemented Interfaces:
Serializable

public final class ComponentEffect extends Object implements Serializable
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(EffectAction), 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
See Also:
  • Method Details

    • effect

      public static <C extends Component> Registration effect(C owner, SerializableRunnable 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.get());
       });
       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:
    • bind

      public static <C extends Component, T> Registration bind(C owner, 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:
    • bindChildren

      public static <T, S extends Signal<T>, P extends Component & HasComponents> Registration bindChildren(P parent, Signal<List<S>> list, SerializableFunction<S,Component> childFactory)
      Binds a list signal containing child signals to a parent component using a child component factory. Each signal in the list corresponds to a child component within the parent.

      The parent component is automatically updated to reflect the structure of the list signal. 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 list signal. 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 signal from the list and returns a corresponding Component. It shouldn't return null. The signal can be further bound to the returned component as needed. Note that childFactory is run inside a Effect, and therefore Signal.get() calls makes effect re-run automatically on signal value change.

      Example of usage:

       SharedListSignal<String> taskList = new SharedListSignal<>(String.class);
      
       UnorderedList component = new UnorderedList();
      
       ComponentEffect.bindChildren(component, taskList, ListItem::new);
       
      Type Parameters:
      T - the value type of the Signals in the list
      S - the type of the Signals in the list
      P - 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