Interface JsFunction

All Superinterfaces:
JsObject

public interface JsFunction extends JsObject
Represents a JavaScript function that can be passed between Java and JavaScript as a method argument or a return value. The function lifetime is bound to the lifetime of the frame this function belongs to. When the owner frame is unloaded, all the JavaScript objects are automatically disposed. An attempt to access a disposed JavaScript object will result in IllegalStateException.
Since:
7.7
  • Method Details

    • invoke

      <T> T invoke(@Nullable JsObject object, Object... args)
      Invokes this function on the given object with the specified args. This method blocks the current thread until the function finishes its execution. If the function raises an exception, then a JsException with an error message that describes the exception reason will be thrown. The same error message will be printed to the JavaScript console.

      The rules to convert Java types into JavaScript types and vice versa are the same as for the JsObject.call(String, Object...) method.

      Type Parameters:
      T - the result of the JavaScript function execution
      Parameters:
      object - the JavaScript object to invoke this function on. Pass null to invoke the function as a global function.
      args - the list of input arguments
      Throws:
      JsException - when an error occurs during the function execution
      IllegalArgumentException - when args contains an unsupported type
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • invokeAsync

      <T> CompletableFuture<T> invokeAsync(@Nullable JsObject object, Object... args)
      Invokes this function on the given object with the specified args.

      This method does not block the current thread execution and executes the function asynchronously.

      Returns a new CompletableFuture that is completed when the function finishes its execution. If the function throws an exception, the future is completed with JsException with the same error message that will be printed to the JavaScript console. If the browser is closed during the function execution, the future will be canceled.

      Type Parameters:
      T - the result of the JavaScript function execution
      Parameters:
      object - the JavaScript object to invoke this function on. Pass null to invoke the function as a global function.
      args - the list of input arguments
      Throws:
      IllegalArgumentException - when args contains an unsupported type
      ObjectClosedException - when the JavaScript object is already disposed or invalid
      Since:
      7.31