From 88613338099f78642e645c27bb975188773c1a80 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Fri, 14 Nov 2025 10:20:37 -0300 Subject: [PATCH] feat: implement PendingJavaScriptResult methods --- .../ElementalPendingJavaScriptResult.java | 92 ++++++++++++++++ .../vaadin/jsonmigration/JsonCodec.java | 104 ++++++++++++++++++ .../jsonmigration/JsonMigrationHelper25.java | 28 +++++ 3 files changed, 224 insertions(+) create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/JsonCodec.java diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalPendingJavaScriptResult.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalPendingJavaScriptResult.java index e2c80c6..93ff64e 100644 --- a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalPendingJavaScriptResult.java +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalPendingJavaScriptResult.java @@ -36,8 +36,10 @@ import com.vaadin.flow.component.page.PendingJavaScriptResult; import com.vaadin.flow.function.SerializableConsumer; +import com.vaadin.flow.internal.JsonCodec; import elemental.json.JsonValue; import java.io.Serializable; +import java.util.concurrent.CompletableFuture; /** * A pending result from a JavaScript snippet sent to the browser for evaluation. This interface @@ -87,4 +89,94 @@ default void then(SerializableConsumer resultHandler) { then(resultHandler, null); } + /** + * Adds a typed 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. + *

+ * Handlers can only be added before the execution has been sent to the + * browser. + * + * @param targetType + * the type to convert the JavaScript return value to, not + * null + * @param resultHandler + * a handler for the return value from a successful execution, + * not null + * @param errorHandler + * a handler for an error message in case the execution failed, + * or null to ignore errors + */ + default void then(Class targetType, + SerializableConsumer resultHandler, + SerializableConsumer errorHandler) { + if (targetType == null) { + throw new IllegalArgumentException("Target type cannot be null"); + } + if (resultHandler == null) { + throw new IllegalArgumentException("Result handler cannot be null"); + } + + SerializableConsumer convertingResultHandler = value -> resultHandler + .accept(JsonCodec.decodeAs(value, targetType)); + + then(convertingResultHandler, errorHandler); + } + + /** + * Adds a typed handler that will be run for a successful execution. The + * handler will be invoked asynchronously if the execution was successful. + * In case of a failure, no handler will be run. + *

+ * A handler can only be added before the execution has been sent to the + * browser. + * + * @param targetType + * the type to convert the JavaScript return value to, not + * null + * @param resultHandler + * a handler for the return value from a successful execution, + * not null + */ + default void then(Class targetType, + SerializableConsumer resultHandler) { + then(targetType, resultHandler, null); + } + + /** + * Creates a typed 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. + *

+ * A completable future can only be created before the execution has been + * sent to the browser. + * + * @param targetType + * the type to convert the JavaScript return value to, not + * null + * + * @return a completable future that will be completed based on the + * execution results, not null + */ + CompletableFuture toCompletableFuture(Class targetType); + + /** + * Creates an untyped 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. + *

+ * A completable future can only be created before the execution has been + * sent to the browser. + * + * @return a completable future that will be completed based on the + * execution results, not null + */ + default CompletableFuture toCompletableFuture() { + return toCompletableFuture(JsonValue.class); + } + } diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonCodec.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonCodec.java new file mode 100644 index 0000000..0decf7a --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonCodec.java @@ -0,0 +1,104 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; +/* + * Copyright 2000-2020 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +import com.vaadin.flow.component.Component; +import com.vaadin.flow.dom.Element; +import com.vaadin.flow.internal.ReflectTools; +import elemental.json.JsonType; +import elemental.json.JsonValue; + +/** + * Utility for encoding objects to and from JSON. + *

+ * Supported types are + *

+ * + *

+ * @author Vaadin Ltd + */ +class JsonCodec { + + /** + * Decodes the given JSON value as the given type. + *

+ * Supported types are {@link String}, {@link Boolean}, {@link Integer}, + * {@link Double} and primitives boolean, int, double + * + * @param + * the decoded type + * @param json + * the JSON value + * @param type + * the type to decode as + * @return the value decoded as the given type + * @throws IllegalArgumentException + * if the type was unsupported + */ + @SuppressWarnings("unchecked") + public static T decodeAs(JsonValue json, Class type) { + assert json != null; + if (json.getType() == JsonType.NULL && !type.isPrimitive()) { + return null; + } + Class convertedType = ReflectTools.convertPrimitiveType(type); + if (type == String.class) { + return type.cast(json.asString()); + } else if (convertedType == Boolean.class) { + return (T) convertedType.cast(Boolean.valueOf(json.asBoolean())); + } else if (convertedType == Double.class) { + return (T) convertedType.cast(Double.valueOf(json.asNumber())); + } else if (convertedType == Integer.class) { + return (T) convertedType + .cast(Integer.valueOf((int) json.asNumber())); + } else if (JsonValue.class.isAssignableFrom(type)) { + return type.cast(json); + } else { + throw new IllegalArgumentException( + "Unknown type " + type.getName()); + } + + } + +} diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java index 3079ee2..cbd36aa 100644 --- a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java @@ -27,6 +27,7 @@ import elemental.json.JsonArray; import elemental.json.JsonObject; import elemental.json.JsonValue; +import java.util.concurrent.CompletableFuture; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import lombok.SneakyThrows; @@ -156,6 +157,10 @@ private static SerializableConsumer wrap(SerializableConsumer resultH return (SerializableConsumer) node -> resultHandler.accept(convertToJsonValue(node)); }; + private static T decodeAs(JsonNode node, Class type) { + return JsonCodec.decodeAs(convertToJsonValue(node), type); + } + @Override @SuppressWarnings("unchecked") public void then(SerializableConsumer resultHandler, @@ -163,6 +168,29 @@ public void then(SerializableConsumer resultHandler, delegate.then(wrap(resultHandler), errorHandler); } + @Override + @SuppressWarnings("unchecked") + public void then(Class targetType, SerializableConsumer resultHandler, + SerializableConsumer errorHandler) { + if (targetType != null && JsonValue.class.isAssignableFrom(targetType)) { + delegate.then(JsonNode.class, wrap(value->{ + resultHandler.accept(JsonCodec.decodeAs(value, targetType)); + }), errorHandler); + } else { + delegate.then(targetType, resultHandler, errorHandler); + } + } + + @Override + public CompletableFuture toCompletableFuture(Class targetType) { + if (JsonValue.class.isAssignableFrom(targetType)) { + return delegate.toCompletableFuture(JsonNode.class) + .thenApply(node -> decodeAs(node, targetType)); + } else { + return delegate.toCompletableFuture(targetType); + } + } + } }