Class CallbackAction<T>

java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.CallbackAction<T>
Type Parameters:
T - the type the JSON value is decoded to and the callback receives
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
SetSignalAction

public class CallbackAction<T> extends Action
Forwards a value from the trigger's handler scope back to the server and hands it to a SerializableConsumer on the UI thread. The generic counterpart to client-side actions like SetPropertyAction — same input/source story, except the destination is a Java callback instead of a JS assignment.

Use this as the bridge from a trigger to arbitrary server-side state — log a value, fire a custom event, push to a queue, etc. The SetSignalAction subclass is the named convenience for the common case of forwarding into a ValueSignal.

Example — log the screen-X coordinate of each click:


 ClickTrigger click = new ClickTrigger(button);
 click.triggers(new CallbackAction<>(Integer.class,
         x -> logger.info("clicked at {}", x), click.screenX()));
 

The rendered JS calls a ReturnChannelRegistration that, on the server, decodes the JSON value as T and invokes the callback. A null value on the wire (a JSON null or a missing argument) is rejected with IllegalStateException — callbacks see decoded, non-null values only. A separate channel is registered per host node, so the same action instance can be wired to triggers hosted on different elements without leaking registrations across them.

For internal use only. May be renamed or removed in a future release.

See Also:
  • Constructor Details

    • CallbackAction

      public CallbackAction(Class<T> valueType, SerializableConsumer<T> callback, Action.Input<? extends T> source)
      Creates an action that, when the trigger fires, evaluates source on the client, sends the value to the server, decodes it as valueType, and hands it to callback on the UI thread.
      Parameters:
      valueType - runtime type the JSON value is decoded to before being passed to callback, not null
      callback - invoked on the UI thread with the decoded value, not null
      source - input that produces the value on the client when the trigger fires, not null
  • Method Details

    • toJs

      protected final JsFunction toJs(Trigger trigger)
      Description copied from class: Action
      Builds the JsFunction that runs this action when the surrounding trigger fires. The returned function takes one runtime argument named event (declared by the framework when it composes the trigger handler); subclasses do not declare argument names themselves.

      The body is one statement. To embed a value produced on the client, capture an Action.Input's JsFunction as a capture and invoke it inside the body as $N(event).

      Specified by:
      toJs in class Action
      Parameters:
      trigger - the surrounding trigger this render is for, not null
      Returns:
      the action's JS function, not null