|
18 | 18 | * #L% |
19 | 19 | */ |
20 | 20 | package com.flowingcode.vaadin.jsonmigration; |
| 21 | + |
21 | 22 | /* |
22 | 23 | * Copyright 2000-2025 Vaadin Ltd. |
23 | 24 | * |
|
51 | 52 | */ |
52 | 53 | public interface ElementalPendingJavaScriptResult extends Serializable { |
53 | 54 |
|
54 | | - /** |
55 | | - * Adds an untyped handler that will be run for a successful execution and a |
56 | | - * handler that will be run for a failed execution. One of the handlers will |
57 | | - * be invoked asynchronously when the result of the execution is sent back |
58 | | - * to the server. It is not possible to synchronously wait for the result of |
59 | | - * the execution while holding the session lock since the request handling |
60 | | - * thread that makes the result available will also need to lock the |
61 | | - * session. |
62 | | - * <p> |
63 | | - * Handlers can only be added before the execution has been sent to the |
64 | | - * browser. |
65 | | - * |
66 | | - * @param resultHandler |
67 | | - * a handler for the JSON representation of the value from a |
68 | | - * successful execution, not <code>null</code> |
69 | | - * @param errorHandler |
70 | | - * a handler for an error message in case the execution failed, |
71 | | - * or <code>null</code> to ignore errors |
72 | | - */ |
73 | | - void then(SerializableConsumer<JsonValue> resultHandler, |
74 | | - SerializableConsumer<String> errorHandler); |
75 | | - |
76 | | - /** |
77 | | - * Adds an untyped handler that will be run for a successful execution. The |
78 | | - * handler will be invoked asynchronously if the execution was successful. |
79 | | - * In case of a failure, no handler will be run. |
80 | | - * <p> |
81 | | - * A handler can only be added before the execution has been sent to the |
82 | | - * browser. |
83 | | - * |
84 | | - * @param resultHandler |
85 | | - * a handler for the JSON representation of the return value from |
86 | | - * a successful execution, not <code>null</code> |
87 | | - */ |
88 | | - default void then(SerializableConsumer<JsonValue> resultHandler) { |
89 | | - then(resultHandler, null); |
90 | | - } |
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 | | - } |
| 55 | + /** |
| 56 | + * Adds an untyped handler that will be run for a successful execution and a handler that will be |
| 57 | + * run for a failed execution. One of the handlers will be invoked asynchronously when the result |
| 58 | + * of the execution is sent back to the server. It is not possible to synchronously wait for the |
| 59 | + * result of the execution while holding the session lock since the request handling thread that |
| 60 | + * makes the result available will also need to lock the session. |
| 61 | + * |
| 62 | + * <p>Handlers can only be added before the execution has been sent to the browser. |
| 63 | + * |
| 64 | + * @param resultHandler a handler for the JSON representation of the value from a successful |
| 65 | + * execution, not <code>null</code> |
| 66 | + * @param errorHandler a handler for an error message in case the execution failed, or <code>null |
| 67 | + * </code> to ignore errors |
| 68 | + */ |
| 69 | + void then( |
| 70 | + SerializableConsumer<JsonValue> resultHandler, SerializableConsumer<String> errorHandler); |
120 | 71 |
|
121 | | - SerializableConsumer<JsonValue> convertingResultHandler = value -> resultHandler |
122 | | - .accept(JsonCodec.decodeAs(value, targetType)); |
| 72 | + /** |
| 73 | + * Adds an untyped handler that will be run for a successful execution. The handler will be |
| 74 | + * invoked asynchronously if the execution was successful. In case of a failure, no handler will |
| 75 | + * be run. |
| 76 | + * |
| 77 | + * <p>A handler can only be added before the execution has been sent to the browser. |
| 78 | + * |
| 79 | + * @param resultHandler a handler for the JSON representation of the return value from a |
| 80 | + * successful execution, not <code>null</code> |
| 81 | + */ |
| 82 | + default void then(SerializableConsumer<JsonValue> resultHandler) { |
| 83 | + then(resultHandler, null); |
| 84 | + } |
123 | 85 |
|
124 | | - then(convertingResultHandler, errorHandler); |
| 86 | + /** |
| 87 | + * Adds a typed handler that will be run for a successful execution and a handler that will be run |
| 88 | + * for a failed execution. One of the handlers will be invoked asynchronously when the result of |
| 89 | + * the execution is sent back to the server. |
| 90 | + * |
| 91 | + * <p>Handlers can only be added before the execution has been sent to the browser. |
| 92 | + * |
| 93 | + * @param targetType the type to convert the JavaScript return value to, not <code>null</code> |
| 94 | + * @param resultHandler a handler for the return value from a successful execution, not <code>null |
| 95 | + * </code> |
| 96 | + * @param errorHandler a handler for an error message in case the execution failed, or <code>null |
| 97 | + * </code> to ignore errors |
| 98 | + */ |
| 99 | + default <T> void then( |
| 100 | + Class<T> targetType, |
| 101 | + SerializableConsumer<T> resultHandler, |
| 102 | + SerializableConsumer<String> errorHandler) { |
| 103 | + if (targetType == null) { |
| 104 | + throw new IllegalArgumentException("Target type cannot be null"); |
125 | 105 | } |
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); |
| 106 | + if (resultHandler == null) { |
| 107 | + throw new IllegalArgumentException("Result handler cannot be null"); |
145 | 108 | } |
146 | 109 |
|
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); |
| 110 | + SerializableConsumer<JsonValue> convertingResultHandler = |
| 111 | + value -> resultHandler.accept(JsonCodec.decodeAs(value, targetType)); |
166 | 112 |
|
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 | | - } |
| 113 | + then(convertingResultHandler, errorHandler); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Adds a typed handler that will be run for a successful execution. The handler will be invoked |
| 118 | + * asynchronously if the execution was successful. In case of a failure, no handler will be run. |
| 119 | + * |
| 120 | + * <p>A handler can only be added before the execution has been sent to the browser. |
| 121 | + * |
| 122 | + * @param targetType the type to convert the JavaScript return value to, not <code>null</code> |
| 123 | + * @param resultHandler a handler for the return value from a successful execution, not <code>null |
| 124 | + * </code> |
| 125 | + */ |
| 126 | + default <T> void then(Class<T> targetType, SerializableConsumer<T> resultHandler) { |
| 127 | + then(targetType, resultHandler, null); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Creates a typed completable future that will be completed with the result of the execution. It |
| 132 | + * will be completed asynchronously when the result of the execution is sent back to the server. |
| 133 | + * It is not possible to synchronously wait for the result of the execution while holding the |
| 134 | + * session lock since the request handling thread that makes the result available will also need |
| 135 | + * to lock the session. |
| 136 | + * |
| 137 | + * <p>A completable future can only be created before the execution has been sent to the browser. |
| 138 | + * |
| 139 | + * @param targetType the type to convert the JavaScript return value to, not <code>null</code> |
| 140 | + * @return a completable future that will be completed based on the execution results, not <code> |
| 141 | + * null</code> |
| 142 | + */ |
| 143 | + <T> CompletableFuture<T> toCompletableFuture(Class<T> targetType); |
181 | 144 |
|
| 145 | + /** |
| 146 | + * Creates an untyped completable future that will be completed with the result of the execution. |
| 147 | + * It will be completed asynchronously when the result of the execution is sent back to the |
| 148 | + * server. |
| 149 | + * |
| 150 | + * <p>A completable future can only be created before the execution has been sent to the browser. |
| 151 | + * |
| 152 | + * @return a completable future that will be completed based on the execution results, not <code> |
| 153 | + * null</code> |
| 154 | + */ |
| 155 | + default CompletableFuture<JsonValue> toCompletableFuture() { |
| 156 | + return toCompletableFuture(JsonValue.class); |
| 157 | + } |
182 | 158 | } |
0 commit comments