Module jpro.webapi

Class JSVariable

java.lang.Object
com.jpro.webapi.JSVariable
Direct Known Subclasses:
PromiseJSVariable

public class JSVariable extends Object
Represents a JavaScript variable managed within a Java environment. This class provides mechanisms to interact with JavaScript variables via a WebAPI and manage their lifecycle, including automatic cleanup using a cleanup function upon garbage collection.
  • Constructor Details

    • JSVariable

      public JSVariable(WebAPI webAPI, String name)
      Constructs a new JSVariable with the specified WebAPI and variable name.
      Parameters:
      webAPI - the WebAPI object through which the JavaScript will be executed
      name - the name of the JavaScript variable
    • JSVariable

      public JSVariable(WebAPI webAPI, String name, String cleanup)
      Constructs a new JSVariable with the specified WebAPI, variable name, and cleanup script.
      Parameters:
      webAPI - the WebAPI object through which the JavaScript will be executed
      name - the name of the JavaScript variable
      cleanup - JavaScript code to be executed for cleaning up the variable
    • JSVariable

      public JSVariable(WebAPI webAPI, String name, String cleanup, CompletableFuture<Void> future)
      Constructs a JSVariable with the specified WebAPI, variable name, cleanup script, and future representing the completion of the variable.
      Parameters:
      webAPI - the WebAPI object through which the JavaScript will be executed
      name - the name of the JavaScript variable
      cleanup - JavaScript code to be executed for cleaning up the variable
      future - a CompletableFuture which will be completed when the variable is set to a value. If an error occurs, the future will be completed exceptionally.
  • Method Details

    • getString

      public CompletableFuture<String> getString()
      Executes a script to get the string representation of the JavaScript variable.
      Returns:
      a CompletableFuture.
    • onComplete

      public CompletableFuture<Void> onComplete()
      Returns a Future, which will be completed when the JSVariable is set to a value. The Future doesn't contain the value itself. But if something went wrong, the Future will be completed exceptionally.
      Returns:
      A Future, which will be completed when the JSVariable is set to a value.
    • onError

      public void onError(Consumer<Throwable> onError)
      This method registers an error handler which will be called when an error occurs.
      Parameters:
      onError - The error handler which will be called when an error occurs.
    • getWebAPI

      public WebAPI getWebAPI()
      Gets the WebAPI associated with this JSVariable.
      Returns:
      the WebAPI object
    • getName

      public String getName()
      Gets the code to access the JavaScript variable. This name can be used in JavaScript code to refer to the variable.
      Returns:
      the code to access this variable
    • futureToPromise

      public static JSVariable futureToPromise(WebAPI webAPI, CompletableFuture<JSVariable> future)
      This method converts a CompletableFuture<JSVariable> to a Promise which can be used in JS. The Promise will be resolved when the CompletableFuture is resolved.
      Parameters:
      webAPI - The WebAPI to use
      future - The CompletableFuture to convert
      Returns:
      a JSVariable containing a Promise which will be resolved when the CompletableFuture is resolved.
      Since:
      2023.3.2
    • isPromise

      public CompletableFuture<Boolean> isPromise()
      This method checks if the JSVariable is a Promise.
      Returns:
      a CompletableFuture which will be resolved with true if the JSVariable is a Promise and false if not.
      Since:
      2023.3.4
    • promiseToFuture

      public static CompletableFuture<JSVariable> promiseToFuture(WebAPI webAPI, JSVariable myvar)
      This method converts a JSVariable of a promise to a CompletableFuture<JSVariable> which will be resolved when the promise is resolved.
      Parameters:
      webAPI - The WebAPI to use
      myvar - The JSVariable to convert
      Returns:
      A CompletableFuture which will be resolved when the promise is resolved
      Since:
      2023.3.2
    • toPromise

      public PromiseJSVariable toPromise()
      Wraps the JSVariable in a PromiseJSVariable, which provides additional promise-specific functionalities.
      Returns:
      a PromiseJSVariable object