java.lang.Object
com.jpro.webapi.JSVariable
- Direct Known Subclasses:
PromiseJSVariable
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 Summary
ConstructorsConstructorDescriptionJSVariable
(WebAPI webAPI, String name) Constructs a new JSVariable with the specified WebAPI and variable name.JSVariable
(WebAPI webAPI, String name, String cleanup) Constructs a new JSVariable with the specified WebAPI, variable name, and cleanup script.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. -
Method Summary
Modifier and TypeMethodDescriptionstatic JSVariable
futureToPromise
(WebAPI webAPI, CompletableFuture<JSVariable> future) This method converts a CompletableFuture<JSVariable> to a Promise which can be used in JS.getName()
Gets the code to access the JavaScript variable.Executes a script to get the string representation of the JavaScript variable.Gets the WebAPI associated with this JSVariable.This method checks if the JSVariable is a Promise.Returns a Future, which will be completed when the JSVariable is set to a value.void
This method registers an error handler which will be called when an error occurs.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.Wraps the JSVariable in a PromiseJSVariable, which provides additional promise-specific functionalities.
-
Constructor Details
-
JSVariable
Constructs a new JSVariable with the specified WebAPI and variable name.- Parameters:
webAPI
- theWebAPI
object through which the JavaScript will be executedname
- the name of the JavaScript variable
-
JSVariable
Constructs a new JSVariable with the specified WebAPI, variable name, and cleanup script.- Parameters:
webAPI
- theWebAPI
object through which the JavaScript will be executedname
- the name of the JavaScript variablecleanup
- JavaScript code to be executed for cleaning up the variable
-
JSVariable
Constructs a JSVariable with the specified WebAPI, variable name, cleanup script, and future representing the completion of the variable.- Parameters:
webAPI
- theWebAPI
object through which the JavaScript will be executedname
- the name of the JavaScript variablecleanup
- JavaScript code to be executed for cleaning up the variablefuture
- aCompletableFuture
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
Executes a script to get the string representation of the JavaScript variable.- Returns:
- a
CompletableFuture
.
-
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
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
Gets the WebAPI associated with this JSVariable.- Returns:
- the
WebAPI
object
-
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
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 usefuture
- The CompletableFuture to convert- Returns:
- a
JSVariable
containing a Promise which will be resolved when the CompletableFuture is resolved. - Since:
- 2023.3.2
-
isPromise
This method checks if the JSVariable is a Promise.- Returns:
- a
CompletableFuture
which will be resolved withtrue
if the JSVariable is a Promise andfalse
if not. - Since:
- 2023.3.4
-
promiseToFuture
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 usemyvar
- The JSVariable to convert- Returns:
- A CompletableFuture which will be resolved when the promise is resolved
- Since:
- 2023.3.2
-
toPromise
Wraps the JSVariable in a PromiseJSVariable, which provides additional promise-specific functionalities.- Returns:
- a
PromiseJSVariable
object
-