Interface PendingJavaScriptResult
- All Superinterfaces:
JavaScriptResult<tools.jackson.databind.JsonNode>,Serializable
- All Known Implementing Classes:
PendingJavaScriptInvocation
If any of the then or toCompletableFuture methods
have been invoked before the snippet is sent to the browser, then there will
be an additional round trip for sending the results of the evaluation back to
any registered handler. If the JavaScript execution returns a
Promise, then the result will be sent to the server only when it
is resolved.
It is not possible to add handlers after the invocation has been sent to the browser since the original result would in that case already be discarded.
- Since:
- 2.0
- Author:
- Vaadin Ltd
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classException used when aCompletableFuturereturned fromPendingJavaScriptResultor anotherJavaScriptResultis completed exceptionally because of a client-side error, or because the result can not be converted to the requested type. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCancel the javascript execution, if it was not yet sent to the browser for execution.booleanChecks whether the JavaScript execution has already been sent to the browser.default voidthen(SerializableConsumer<tools.jackson.databind.JsonNode> resultHandler) Adds an untyped handler that will be run for a successful execution.voidthen(SerializableConsumer<tools.jackson.databind.JsonNode> resultHandler, SerializableConsumer<String> errorHandler) Adds an untyped handler that will be run for a successful execution and a handler that will be run for a failed execution.default <T> voidthen(Class<T> targetType, SerializableConsumer<T> resultHandler) Adds a typed handler that will be run for a successful execution.default <T> voidthen(Class<T> targetType, SerializableConsumer<T> resultHandler, SerializableConsumer<String> errorHandler) Adds a typed handler that will be run for a successful execution and a handler that will be run for a failed execution or a return value that can not be converted.default <T> voidthen(tools.jackson.core.type.TypeReference<T> typeReference, SerializableConsumer<T> resultHandler) Adds a typed handler that will be run for a successful execution.default <T> voidthen(tools.jackson.core.type.TypeReference<T> typeReference, SerializableConsumer<T> resultHandler, SerializableConsumer<String> errorHandler) Adds a typed handler that will be run for a successful execution and a handler that will be run for a failed execution or a return value that can not be converted.default CompletableFuture<tools.jackson.databind.JsonNode> Creates an untyped completable future that will be completed with the result of the execution.default <T> CompletableFuture<T> toCompletableFuture(Class<T> targetType) Creates a typed completable future that will be completed with the result of the execution.default <T> CompletableFuture<T> toCompletableFuture(tools.jackson.core.type.TypeReference<T> typeReference) Creates a typed completable future that will be completed with the result of the execution.
-
Method Details
-
cancelExecution
boolean cancelExecution()Cancel the javascript execution, if it was not yet sent to the browser for execution.- Specified by:
cancelExecutionin interfaceJavaScriptResult<tools.jackson.databind.JsonNode>- Returns:
trueif the execution was canceled,falseif not
-
isSentToBrowser
boolean isSentToBrowser()Checks whether the JavaScript execution has already been sent to the browser. It is no longer possible to cancel the execution or add a handler for the result after the execution has been sent.- Specified by:
isSentToBrowserin interfaceJavaScriptResult<tools.jackson.databind.JsonNode>- Returns:
trueif the execution is sent to the browser,falseif it's still pending
-
then
default <T> void then(Class<T> targetType, SerializableConsumer<T> resultHandler, SerializableConsumer<String> errorHandler) Adds a typed handler that will be run for a successful execution and a handler that will be run for a failed execution or a return value that can not be converted. One of the handlers will be invoked asynchronously when the result of the execution is sent back to the server.The JavaScript return value will be automatically converted to the specified target type. All types supported by Jackson for JSON deserialization are supported, including custom bean classes.
Handlers can only be added before the execution has been sent to the browser.
- Type Parameters:
T- the type to convert the result to- Parameters:
targetType- the type to convert the JavaScript return value to, notnullresultHandler- a handler for the return value from a successful execution, notnullerrorHandler- a handler for an error message in case the execution failed or its return value can not be converted, ornullto ignore failed executions. Without an error handler, a return value that can not be converted is logged as a warning
-
then
Adds a typed handler that will be run for a successful execution. The handler will be invoked asynchronously if the execution was successful. In case the execution fails, no handler will be run. A return value that can not be converted is logged as a warning, and no handler will be run.The JavaScript return value will be automatically converted to the specified target type. All types supported by Jackson for JSON deserialization are supported, including custom bean classes.
A handler can only be added before the execution has been sent to the browser.
- Type Parameters:
T- the type to convert the result to- Parameters:
targetType- the type to convert the JavaScript return value to, notnullresultHandler- a handler for the return value from a successful execution, notnull
-
toCompletableFuture
Creates a typed completable future that will be completed with the result of the execution. It will be completed asynchronously when the result of the execution is sent back to the server. It is not possible to synchronously wait for the result of the execution while holding the session lock since the request handling thread that makes the result available will also need to lock the session.The JavaScript return value will be automatically converted to the specified target type. All types supported by Jackson for JSON deserialization are supported, including custom bean classes.
If the execution fails, or its return value can not be converted to the target type, the future is completed exceptionally with a
PendingJavaScriptResult.JavaScriptException.A completable future can only be created before the execution has been sent to the browser.
- Type Parameters:
T- the type to convert the result to- Parameters:
targetType- the type to convert the JavaScript return value to, notnull- Returns:
- a completable future that will be completed based on the
execution results, not
null
-
then
default <T> void then(tools.jackson.core.type.TypeReference<T> typeReference, SerializableConsumer<T> resultHandler, SerializableConsumer<String> errorHandler) Adds a typed handler that will be run for a successful execution and a handler that will be run for a failed execution or a return value that can not be converted. One of the handlers will be invoked asynchronously when the result of the execution is sent back to the server.The JavaScript return value will be automatically converted to the type specified by the
TypeReference. This method supports generic types such asList<MyBean>andMap<String, MyBean>.Example usage:
element.executeJs("return [{name: 'Alice', age: 30}]") .then(new TypeReference<List<Person>>() { }, list -> System.out.println(list.get(0).name));Handlers can only be added before the execution has been sent to the browser.
- Type Parameters:
T- the type to convert the result to- Parameters:
typeReference- the type reference describing the target type, notnullresultHandler- a handler for the return value from a successful execution, notnullerrorHandler- a handler for an error message in case the execution failed or its return value can not be converted, ornullto ignore failed executions. Without an error handler, a return value that can not be converted is logged as a warning- Since:
- 25.0
-
then
default <T> void then(tools.jackson.core.type.TypeReference<T> typeReference, SerializableConsumer<T> resultHandler) Adds a typed handler that will be run for a successful execution. The handler will be invoked asynchronously if the execution was successful. In case the execution fails, no handler will be run. A return value that can not be converted is logged as a warning, and no handler will be run.The JavaScript return value will be automatically converted to the type specified by the
TypeReference. This method supports generic types such asList<MyBean>andMap<String, MyBean>.A handler can only be added before the execution has been sent to the browser.
- Type Parameters:
T- the type to convert the result to- Parameters:
typeReference- the type reference describing the target type, notnullresultHandler- a handler for the return value from a successful execution, notnull- Since:
- 25.0
-
toCompletableFuture
default <T> CompletableFuture<T> toCompletableFuture(tools.jackson.core.type.TypeReference<T> typeReference) Creates a typed completable future that will be completed with the result of the execution. It will be completed asynchronously when the result of the execution is sent back to the server.The JavaScript return value will be automatically converted to the type specified by the
TypeReference. This method supports generic types such asList<MyBean>andMap<String, MyBean>.If the execution fails, or its return value can not be converted to the target type, the future is completed exceptionally with a
PendingJavaScriptResult.JavaScriptException.A completable future can only be created before the execution has been sent to the browser.
- Type Parameters:
T- the type to convert the result to- Parameters:
typeReference- the type reference describing the target type, notnull- Returns:
- a completable future that will be completed based on the
execution results, not
null - Since:
- 25.0
-
then
void then(SerializableConsumer<tools.jackson.databind.JsonNode> resultHandler, SerializableConsumer<String> errorHandler) Adds an untyped handler that will be run for a successful execution and a handler that will be run for a failed execution. One of the handlers will be invoked asynchronously when the result of the execution is sent back to the server. It is not possible to synchronously wait for the result of the execution while holding the session lock since the request handling thread that makes the result available will also need to lock the session.Handlers can only be added before the execution has been sent to the browser.
- Specified by:
thenin interfaceJavaScriptResult<tools.jackson.databind.JsonNode>- Parameters:
resultHandler- a handler for the JSON representation of the value from a successful execution, notnullerrorHandler- a handler for an error message in case the execution failed, ornullto ignore errors
-
then
Adds an untyped handler that will be run for a successful execution. The handler will be invoked asynchronously if the execution was successful. In case of a failure, no handler will be run.A handler can only be added before the execution has been sent to the browser.
- Specified by:
thenin interfaceJavaScriptResult<tools.jackson.databind.JsonNode>- Parameters:
resultHandler- a handler for the JSON representation of the return value from a successful execution, notnull
-
toCompletableFuture
Creates an untyped completable future that will be completed with the result of the execution. It will be completed asynchronously when the result of the execution is sent back to the server.A completable future can only be created before the execution has been sent to the browser.
- Specified by:
toCompletableFuturein interfaceJavaScriptResult<tools.jackson.databind.JsonNode>- Returns:
- a completable future that will be completed based on the
execution results, not
null
-