|
36 | 36 |
|
37 | 37 | import com.vaadin.flow.component.page.PendingJavaScriptResult; |
38 | 38 | import com.vaadin.flow.function.SerializableConsumer; |
| 39 | +import com.vaadin.flow.internal.JsonCodec; |
39 | 40 | import elemental.json.JsonValue; |
40 | 41 | import java.io.Serializable; |
| 42 | +import java.util.concurrent.CompletableFuture; |
41 | 43 |
|
42 | 44 | /** |
43 | 45 | * A pending result from a JavaScript snippet sent to the browser for evaluation. This interface |
@@ -87,4 +89,94 @@ default void then(SerializableConsumer<JsonValue> resultHandler) { |
87 | 89 | then(resultHandler, null); |
88 | 90 | } |
89 | 91 |
|
| 92 | + /** |
| 93 | + * Adds a typed handler that will be run for a successful execution and a |
| 94 | + * handler that will be run for a failed execution. One of the handlers will |
| 95 | + * be invoked asynchronously when the result of the execution is sent back |
| 96 | + * to the server. |
| 97 | + * <p> |
| 98 | + * Handlers can only be added before the execution has been sent to the |
| 99 | + * browser. |
| 100 | + * |
| 101 | + * @param targetType |
| 102 | + * the type to convert the JavaScript return value to, not |
| 103 | + * <code>null</code> |
| 104 | + * @param resultHandler |
| 105 | + * a handler for the return value from a successful execution, |
| 106 | + * not <code>null</code> |
| 107 | + * @param errorHandler |
| 108 | + * a handler for an error message in case the execution failed, |
| 109 | + * or <code>null</code> to ignore errors |
| 110 | + */ |
| 111 | + default <T> void then(Class<T> targetType, |
| 112 | + SerializableConsumer<T> resultHandler, |
| 113 | + SerializableConsumer<String> errorHandler) { |
| 114 | + if (targetType == null) { |
| 115 | + throw new IllegalArgumentException("Target type cannot be null"); |
| 116 | + } |
| 117 | + if (resultHandler == null) { |
| 118 | + throw new IllegalArgumentException("Result handler cannot be null"); |
| 119 | + } |
| 120 | + |
| 121 | + SerializableConsumer<JsonValue> convertingResultHandler = value -> resultHandler |
| 122 | + .accept(JsonCodec.decodeAs(value, targetType)); |
| 123 | + |
| 124 | + then(convertingResultHandler, errorHandler); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Adds a typed handler that will be run for a successful execution. The |
| 129 | + * handler will be invoked asynchronously if the execution was successful. |
| 130 | + * In case of a failure, no handler will be run. |
| 131 | + * <p> |
| 132 | + * A handler can only be added before the execution has been sent to the |
| 133 | + * browser. |
| 134 | + * |
| 135 | + * @param targetType |
| 136 | + * the type to convert the JavaScript return value to, not |
| 137 | + * <code>null</code> |
| 138 | + * @param resultHandler |
| 139 | + * a handler for the return value from a successful execution, |
| 140 | + * not <code>null</code> |
| 141 | + */ |
| 142 | + default <T> void then(Class<T> targetType, |
| 143 | + SerializableConsumer<T> resultHandler) { |
| 144 | + then(targetType, resultHandler, null); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates a typed completable future that will be completed with the result |
| 149 | + * of the execution. It will be completed asynchronously when the result of |
| 150 | + * the execution is sent back to the server. It is not possible to |
| 151 | + * synchronously wait for the result of the execution while holding the |
| 152 | + * session lock since the request handling thread that makes the result |
| 153 | + * available will also need to lock the session. |
| 154 | + * <p> |
| 155 | + * A completable future can only be created before the execution has been |
| 156 | + * sent to the browser. |
| 157 | + * |
| 158 | + * @param targetType |
| 159 | + * the type to convert the JavaScript return value to, not |
| 160 | + * <code>null</code> |
| 161 | + * |
| 162 | + * @return a completable future that will be completed based on the |
| 163 | + * execution results, not <code>null</code> |
| 164 | + */ |
| 165 | + <T> CompletableFuture<T> toCompletableFuture(Class<T> targetType); |
| 166 | + |
| 167 | + /** |
| 168 | + * Creates an untyped completable future that will be completed with the |
| 169 | + * result of the execution. It will be completed asynchronously when the |
| 170 | + * result of the execution is sent back to the server. |
| 171 | + * <p> |
| 172 | + * A completable future can only be created before the execution has been |
| 173 | + * sent to the browser. |
| 174 | + * |
| 175 | + * @return a completable future that will be completed based on the |
| 176 | + * execution results, not <code>null</code> |
| 177 | + */ |
| 178 | + default CompletableFuture<JsonValue> toCompletableFuture() { |
| 179 | + return toCompletableFuture(JsonValue.class); |
| 180 | + } |
| 181 | + |
90 | 182 | } |
0 commit comments