use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.
the class Page method executeJs.
// When updating JavaDocs here, keep in sync with Element.executeJavaScript
/**
* Asynchronously runs the given JavaScript expression in the browser.
* <p>
* The returned <code>PendingJavaScriptResult</code> can be used to retrieve
* any <code>return</code> value from the JavaScript expression. If no
* return value handler is registered, the return value will be ignored.
* <p>
* The given parameters will be available to the expression as variables
* named <code>$0</code>, <code>$1</code>, and so on. Supported parameter
* types are:
* <ul>
* <li>{@link String}
* <li>{@link Integer}
* <li>{@link Double}
* <li>{@link Boolean}
* <li>{@link JsonValue}
* <li>{@link Element} (will be sent as <code>null</code> if the server-side
* element instance is not attached when the invocation is sent to the
* client)
* </ul>
* Note that the parameter variables can only be used in contexts where a
* JavaScript variable can be used. You should for instance do
* <code>'prefix' + $0</code> instead of <code>'prefix$0'</code> and
* <code>value[$0]</code> instead of <code>value.$0</code> since JavaScript
* variables aren't evaluated inside strings or property names.
*
* @param expression
* the JavaScript expression to invoke
* @param parameters
* parameters to pass to the expression
* @return a pending result that can be used to get a value returned from
* the expression
*/
public PendingJavaScriptResult executeJs(String expression, Serializable... parameters) {
JavaScriptInvocation invocation = new JavaScriptInvocation(expression, parameters);
PendingJavaScriptInvocation execution = new PendingJavaScriptInvocation(ui.getInternals().getStateTree().getRootNode(), invocation);
ui.getInternals().addJavaScriptInvocation(execution);
return execution;
}
use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.
the class ElementTest method assertPendingJs.
private void assertPendingJs(UI ui, String js, Serializable... arguments) {
List<PendingJavaScriptInvocation> pendingJs = ui.getInternals().dumpPendingJavaScriptInvocations();
JavaScriptInvocation expected = new JavaScriptInvocation(js, arguments);
Assert.assertEquals(1, pendingJs.size());
assertEquals(expected, pendingJs.get(0).getInvocation());
}
use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.
the class ElementTest method callFunctionBeforeDetach.
@Test
public void callFunctionBeforeDetach() {
UI ui = new MockUI();
Element element = ElementFactory.createDiv();
ui.getElement().appendChild(element);
element.callJsFunction("noArgsMethod");
ui.getElement().removeAllChildren();
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
List<PendingJavaScriptInvocation> invocations = ui.getInternals().dumpPendingJavaScriptInvocations();
Assert.assertTrue(invocations.isEmpty());
}
use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.
the class UidlWriterTest method testEncodeExecuteJavaScript_npmMode.
@Test
public void testEncodeExecuteJavaScript_npmMode() {
Element element = ElementFactory.createDiv();
JavaScriptInvocation invocation1 = new JavaScriptInvocation("$0.focus()", element);
JavaScriptInvocation invocation2 = new JavaScriptInvocation("console.log($0, $1)", "Lives remaining:", Integer.valueOf(3));
List<PendingJavaScriptInvocation> executeJavaScriptList = Stream.of(invocation1, invocation2).map(invocation -> new PendingJavaScriptInvocation(element.getNode(), invocation)).collect(Collectors.toList());
JsonArray json = UidlWriter.encodeExecuteJavaScriptList(executeJavaScriptList);
JsonArray expectedJson = JsonUtils.createArray(JsonUtils.createArray(// Null since element is not attached
Json.createNull(), Json.create("$0.focus()")), JsonUtils.createArray(Json.create("Lives remaining:"), Json.create(3), Json.create("console.log($0, $1)")));
assertTrue(JsonUtils.jsonEquals(expectedJson, json));
}
use of com.vaadin.flow.component.internal.PendingJavaScriptInvocation in project flow by vaadin.
the class PublishedServerEventHandlerRpcHandlerTest method promiseFailure.
@Test
public void promiseFailure() {
int promiseId = 4;
JsonArray args = Json.createArray();
args.set(0, -36);
ComponentWithCompute component = new ComponentWithCompute();
UI ui = new UI();
ui.getInternals().setSession(session);
ui.add(component);
// Get rid of attach invocations
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
ui.getInternals().dumpPendingJavaScriptInvocations();
try {
PublishedServerEventHandlerRpcHandler.invokeMethod(component, component.getClass(), "compute", args, promiseId);
Assert.fail("Exception should be thrown");
} catch (RuntimeException e) {
Assert.assertTrue(e.getCause() instanceof ArithmeticException);
}
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
List<PendingJavaScriptInvocation> pendingJavaScriptInvocations = ui.getInternals().dumpPendingJavaScriptInvocations();
Assert.assertEquals(1, pendingJavaScriptInvocations.size());
JavaScriptInvocation invocation = pendingJavaScriptInvocations.get(0).getInvocation();
Assert.assertTrue("Invocation does not look like a promise callback", invocation.getExpression().contains(JsonConstants.RPC_PROMISE_CALLBACK_NAME));
List<Object> parameters = invocation.getParameters();
Assert.assertEquals("Expected two paramters: promiseId, target", 2, parameters.size());
Assert.assertEquals("Promise id should match the value passed to invokeMethod", Integer.valueOf(promiseId), parameters.get(0));
Assert.assertEquals("Target should be the component's element", component.getElement(), parameters.get(1));
}
Aggregations