Class ElementEffect
- All Implemented Interfaces:
Serializable
It ultimately creates a Signal effect, i.e. a call to
Signal.unboundEffect(EffectAction), that is automatically enabled
when an element is attached and disabled when the element is detached.
Additionally, it provides methods to bind signals to element according to a
given value setting function.
For internal use only. May be renamed or removed in a future release.
- Since:
- 25.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends @Nullable Object>
Registrationbind(Element owner, Signal<T> signal, SerializableBiConsumer<Element, T> setter) Binds asignal's value to a given owner element in a way defined insetterfunction and creates a Signal effect function executing the setter whenever the signal value changes.static <T extends @Nullable Object,S extends Signal<T>>
RegistrationbindChildren(Element parentElement, Signal<List<S>> list, SerializableFunction<S, Element> childFactory) Binds a list signal containing child signals to a parent component using a child component factory.voidclose()static Registrationeffect(Element owner, EffectAction effectFunction) Creates a Signal effect that is owned by a given element.
-
Constructor Details
-
ElementEffect
-
-
Method Details
-
effect
Creates a Signal effect that is owned by a given element. The effect is enabled when the element is attached and automatically disabled when it is detached.Example of usage:
Registration effect = ElementEffect.effect(myElement, () -> { Notification.show("Element is attached and signal value is " + someSignal.get()); }); effect.remove(); // to remove the effect when no longer needed- Parameters:
owner- the owner element for which the effect is applied, must not benulleffectFunction- the effect function to be executed when any dependency is changed, must not benull- Returns:
- a
Registrationthat can be used to remove the effect function - See Also:
-
bind
public static <T extends @Nullable Object> Registration bind(Element owner, Signal<T> signal, SerializableBiConsumer<Element, T> setter) Binds asignal's value to a given owner element in a way defined insetterfunction and creates a Signal effect function executing the setter whenever the signal value changes.Example of usage:
Element mySpan = new Element("span"); Registration effect = ElementEffect.bind(mySpan, stringSignal, Element::setText); effect.remove(); // to remove the effect when no longer needed ElementEffect.bind(mySpan, stringSignal.map(value -> !value.isEmpty()), Element::setVisible);- Type Parameters:
T- the type of the signal value- Parameters:
owner- the owner element for which the effect is applied, must not benullsignal- the signal whose value is to be bound to the element, must not benullsetter- the setter function that defines how the signal value is applied to the element, must not benull- Returns:
- a
Registrationthat can be used to remove the effect function - See Also:
-
close
public void close() -
bindChildren
public static <T extends @Nullable Object,S extends Signal<T>> Registration bindChildren(Element parentElement, Signal<List<S>> list, SerializableFunction<S, Element> 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
IllegalStateExceptionwill be thrown.New child components are created using the provided
childFactoryfunction. This function takes a signal from the list and returns a correspondingComponent. It shouldn't returnnull. The signal can be further bound to the returned component as needed. Note thatchildFactoryis run inside aEffect, and thereforeSignal.get()calls makes effect re-run automatically on signal value change.- Type Parameters:
T- the value type of theSignals in the listS- the type of theSignals in the list- Parameters:
parentElement- target parent element, must not benulllist- list signal to bind to the parent, must not benullchildFactory- factory to create new element, must not benull- Throws:
IllegalStateException- thrown if parent element isn't empty
-