Class EffectContext

java.lang.Object
com.vaadin.flow.signals.EffectContext
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
BindingContext

public class EffectContext extends Object implements Serializable
Provides context information about why a signal effect is running. This allows effect callbacks to distinguish between the initial execution, updates triggered by the effect owner's requests, and updates triggered by background changes (such as a background thread or another user modifying a shared signal).

Typical usage:

 Signal.effect(this, ctx -> {
     span.getElement().setText("$" + price.get());
     if (ctx.isBackgroundChange()) {
         span.getElement().flashClass("highlight");
     }
 });
 
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    EffectContext(boolean initialRun, boolean backgroundChange)
    Creates a new effect context.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns whether this execution was triggered by a signal change that happened outside the effect owner's request context.
    boolean
    Returns whether this is the very first execution of the effect.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EffectContext

      public EffectContext(boolean initialRun, boolean backgroundChange)
      Creates a new effect context.
      Parameters:
      initialRun - whether this is the first execution of the effect
      backgroundChange - whether the invalidation that triggered this execution happened outside a user request context
  • Method Details

    • isInitialRun

      public boolean isInitialRun()
      Returns whether this is the very first execution of the effect. The initial run happens when the effect is first created (or when a bound component is first attached).
      Returns:
      true if this is the first execution, false otherwise
    • isBackgroundChange

      public boolean isBackgroundChange()
      Returns whether this execution was triggered by a signal change that happened outside the effect owner's request context. A background change occurs when a signal is modified from a background thread or by another user modifying a shared signal.

      This is false during the initial run (even if there is no active request) and false when the signal was changed during a request from the user who owns the effect. It is true when the signal change originated from outside any request context for this user.

      Note: This flag is a hint and may not be fully accurate when multiple signal changes from different sources are batched into a single effect invocation. The flag is guaranteed to not be set if none of the changes are from a background context and guaranteed to be set if all changes are from a background context. When changes come from mixed sources, the state of the flag is arbitrary.

      Returns:
      true if triggered by a background change, false otherwise