Interface JavaScriptResult<T>

Type Parameters:
T - the type of the value of a successful execution
All Superinterfaces:
Serializable
All Known Subinterfaces:
PendingJavaScriptResult
All Known Implementing Classes:
PendingJavaScriptInvocation

public interface JavaScriptResult<T> extends Serializable
The result of a JavaScript execution, which hands the value that the JavaScript returns to the server as a value of the type T.

A method of a JavaScript definition declares this as its return type, with the type to convert the value to as the type argument:

 @JsDefinition
 public interface DeepThoughtJs {
     @JsExpression("return 42")
     JavaScriptResult<Integer> getAnswer();
 }

 element.executeJs(DeepThoughtJs.class).getAnswer()
         .then(answer -> Notification.show("The answer is " + answer));
 
The value is converted with Jackson, so every type that Jackson can deserialize works, including beans and generic types such as List<Person>.

A JavaScript null is handed over as a Java null, also when T is a JSON node type such as ObjectNode. The one exception is the then handlers of JavaScriptResult<JsonNode>, which get the JSON as the browser sent it, the same way as with PendingJavaScriptResult: there, a JavaScript null is a NullNode. Its toCompletableFuture() still completes with a Java null.

If any of the then or toCompletableFuture methods have been invoked before the JavaScript is sent to the browser, then there will be an additional round trip for sending the result back to any registered handler. If the JavaScript 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 execution has been sent to the browser since the original result would in that case already be discarded.

PendingJavaScriptResult is the variant where the value is the JSON that the browser sent back, and that can be converted to a type chosen when a handler is added.

Since:
25.4
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Cancel the javascript execution, if it was not yet sent to the browser for execution.
    boolean
    Checks whether the JavaScript execution has already been sent to the browser.
    default void
    then(SerializableConsumer<T> resultHandler)
    Adds a handler that will be run for a successful execution.
    void
    then(SerializableConsumer<T> resultHandler, SerializableConsumer<String> errorHandler)
    Adds a handler that will be run for a successful execution and a handler that will be run for a failed execution.
    Creates a 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.
      Returns:
      true if the execution was canceled, false if 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.
      Returns:
      true if the execution is sent to the browser, false if it's still pending
    • then

      void then(SerializableConsumer<T> resultHandler, SerializableConsumer<String> errorHandler)
      Adds a 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.

      Parameters:
      resultHandler - a handler for the value from a successful execution, not null
      errorHandler - a handler for an error message in case the execution failed or its value could not be converted to T, or null to ignore failed executions. Without an error handler, a value that can not be converted is logged as a warning
    • then

      default void then(SerializableConsumer<T> resultHandler)
      Adds a 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, the error is silently ignored: no handler is run and nothing is logged or thrown. Use then(SerializableConsumer, SerializableConsumer) to find out about a failed execution. A value that can not be converted to T is logged as a warning.

      A handler can only be added before the execution has been sent to the browser.

      Parameters:
      resultHandler - a handler for the value from a successful execution, not null
    • toCompletableFuture

      default CompletableFuture<T> toCompletableFuture()
      Creates a 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.

      If the execution fails, or its value can not be converted to T, 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.

      Returns:
      a completable future that will be completed based on the execution results, not null