Class PromiseAction<T>
java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.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
Base class for actions that run a JS expression yielding a Promise
and optionally report the outcome back to the server.
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 function by overriding toPromiseJs(Trigger).
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.
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 final JsFunctionFinal by design — subclasses customise the rendered JS throughtoPromiseJs(com.vaadin.flow.component.trigger.internal.Trigger), never by overriding the wiring that subscribes to the promise.protected abstract JsFunctiontoPromiseJs(Trigger trigger) Subclasses return aJsFunctionthat, when invoked with the trigger's event, evaluates to aPromise.
-
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
-
toPromiseJs
Subclasses return aJsFunctionthat, when invoked with the trigger's event, evaluates to aPromise. 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 inside the function body — for example, copying a string and resolving with it:return JsFunction.of( "return ((v) => navigator.clipboard.writeText(v).then(() => v))($0(event))", textInput.toJs(trigger)).withArguments("event");- Parameters:
trigger- the surrounding trigger this render is for, notnull- Returns:
- the promise-yielding JS function, not
null
-
toJs
Final by design — subclasses customise the rendered JS throughtoPromiseJs(com.vaadin.flow.component.trigger.internal.Trigger), never by overriding the wiring that subscribes to the promise. Keeping the.then/.catchglue identical across subclasses is what makes thePromiseAction.Outcomewire contract stable.
-