Class PromiseAction<T>
- Type Parameters:
T- type the JS promise resolves with, decoded once on the server
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ReadFromClipboardAction,RequestFullscreenAction,WriteToClipboardAction
Many gesture-bound browser APIs are asynchronous — clipboard, fullscreen,
file picker, share, web payment, … — and follow the same shape: call the API,
then handle the resolved value or the rejection. This class collapses that
pattern into one place so subclasses only need to emit the promise-yielding
JS expression by overriding
appendPromiseExpression(JsBuilder, StringBuilder).
The type parameter T is the type of value the JS promise resolves
with: it gets Jackson-decoded once before onSuccess sees it, so
subclasses don't write per-action adapters. Use Void when the promise
resolves with no meaningful value (e.g. requestFullscreen).
Two construction modes:
- Fire-and-forget —
PromiseAction()— the rendered JS is just the promise expression; the server never sees the outcome. - With outcome handling —
PromiseAction(Class, SerializableConsumer, SerializableConsumer)— supply the payload type, anonSuccessconsumer receiving the decoded value (ornullif the promise resolved withundefined), and anonErrorconsumer receiving anPromiseAction.Errorrecord (all required; passv -> {}orerr -> {}to opt out). The handlers run on the UI thread after the client reports the outcome of the promise.
JsFunction that subscribes to the promise and pushes an
PromiseAction.Outcome through a lazily-registered ReturnChannelRegistration
on the trigger host node.
For internal use only. May be renamed or removed in a future release.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordInformation delivered to theonErrorconsumer after the promise rejects.Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a fire-and-forget action: the promise's outcome is not reported back to the server.protectedPromiseAction(Class<T> payloadType, SerializableConsumer<@Nullable T> onSuccess, SerializableConsumer<PromiseAction.Error> onError) Creates an action whose promise outcome is reported back to the server. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidappendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out) Subclasses append a JS expression that evaluates to aPromisewhen the trigger fires.protected final voidappendStatement(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out) Final by design — subclasses customise the rendered JS throughappendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder, java.lang.StringBuilder), never by overriding the wiring that subscribes to the promise.
-
Constructor Details
-
PromiseAction
protected PromiseAction()Creates a fire-and-forget action: the promise's outcome is not reported back to the server. -
PromiseAction
protected PromiseAction(Class<T> payloadType, SerializableConsumer<@Nullable T> onSuccess, SerializableConsumer<PromiseAction.Error> onError) Creates an action whose promise outcome is reported back to the server.onSuccessruns after the promise resolves and receives the decoded value (ornullif the promise resolved withundefined);onErrorruns after the promise rejects and receives anPromiseAction.Errorrecord with the browser's error name and message. Both run on the UI thread.- Parameters:
payloadType- type to deserialise the promise's resolved value to, notnull; useVoid.classwhen the promise has no meaningful valueonSuccess- invoked on the UI thread with the decoded value after the client reports the promise resolved, notnullonError- invoked on the UI thread with the browser's error after the client reports the promise rejected, notnull
-
-
Method Details
-
appendPromiseExpression
protected abstract void appendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out) Subclasses append a JS expression that evaluates to aPromisewhen the trigger fires. The value the promise resolves with is decoded toTon the server and handed toonSuccess. To deliver a typed value, subclasses can wrap their API call in an IIFE — for example, copying a string and resolving with it:((v) => navigator.clipboard.writeText(v).then(() => v))(<textExpr>)- Parameters:
builder- collects element parameter references, notnullout- buffer to append into, notnull
-
appendStatement
protected final void appendStatement(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out) Final by design — subclasses customise the rendered JS throughappendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder, java.lang.StringBuilder), never by overriding the wiring that subscribes to the promise. Keeping the.then/.catchglue identical across subclasses is what makes thePromiseAction.Outcomewire contract stable.- Specified by:
appendStatementin classAction- Parameters:
builder- collects element parameter references, notnullout- buffer to append into, notnull
-