Package com.vaadin.flow.signals.impl
Class Effect
java.lang.Object
com.vaadin.flow.signals.impl.Effect
- All Implemented Interfaces:
Serializable
Applies a side effect based on signal value changes. An effect is a callback
that is initially run when the effect is created, and subsequently run again
whenever any dependency changes. Dependencies are automatically registered
for all signals that are read from the callback. The callback is run again
whenever there's a change to any dependency. Dependencies are always updated
based the signals read during the most recent invocation.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionEffect(EffectAction action) Creates a signal effect with the given action and the default dispatcher.Effect(EffectAction action, SerializableExecutor dispatcher) Creates a signal effect with the given action and a custom dispatcher. -
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()Disposes this effect by unregistering all current dependencies and preventing the action from running again.static <C extends Component>
Registrationeffect(C owner, SerializableRunnable effectFunction) Creates a Signal effect that is owned by a given component.
-
Constructor Details
-
Effect
Creates a signal effect with the given action and the default dispatcher. The action is run when the effect is created and is subsequently run again whenever there's a change to any signal value that was read during the last invocation.- Parameters:
action- the action to use, notnull- See Also:
-
Effect
Creates a signal effect with the given action and a custom dispatcher. The action is run when the effect is created and is subsequently run again whenever there's a change to any signal value that was read during the last invocation. The dispatcher can be used to make sure changes are evaluated asynchronously or with some specific context available. The action itself needs to be synchronous to be able to track changes.- Parameters:
action- the action to use, notnulldispatcher- the dispatcher to use when handling changes, notnull
-
-
Method Details
-
dispose
public void dispose()Disposes this effect by unregistering all current dependencies and preventing the action from running again. -
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 = Effect.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 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
-